Exclusion is one of its essential features to exclude specific types of visits from being tracked, ensuring your analytics focus on genuine user interactions. The plugin offers a variety of default exclusion methods and provides a filter hook wp_statistics_exclusion
to add custom exclusions.
Adding Custom Exclusions
To add custom exclusions, you can directly handle the logic within the wp_statistics_exclusion
filter. Here’s how you can add various custom exclusions:
Exclude Visits from a Specific User-Agent
add_filter('wp_statistics_exclusion', function($exclude, $visitorProfile) {
if ($exclude['exclusion_match'] === false) {
$userAgent = $visitorProfile->getHttpUserAgent();
if (strpos($userAgent, 'CustomBot') !== false) {
$exclude = array('exclusion_match' => true, 'exclusion_reason' => 'Custom User-Agent');
}
}
return $exclude;
}, 10, 2);
Exclude Visits from a Specific User ID
add_filter('wp_statistics_exclusion', function($exclude, $visitorProfile) {
if ($exclude['exclusion_match'] === false && is_user_logged_in()) {
$currentUserId = get_current_user_id();
$excludedUserIds = array(1, 2, 3); // Add user IDs you want to exclude
if (in_array($currentUserId, $excludedUserIds)) {
$exclude = array('exclusion_match' => true, 'exclusion_reason' => 'Excluded User ID');
}
}
return $exclude;
}, 10, 2);
Exclude Visits from Users with a Specific Meta Key
add_filter('wp_statistics_exclusion', function($exclude, $visitorProfile) {
if ($exclude['exclusion_match'] === false && is_user_logged_in()) {
$currentUserId = get_current_user_id();
$customMetaKey = get_user_meta($currentUserId, 'exclude_from_stats', true);
if ($customMetaKey) {
$exclude = array('exclusion_match' => true, 'exclusion_reason' => 'Custom Meta Key');
}
}
return $exclude;
}, 10, 2);
Exclude Visits from a Specific IP Address
add_filter('wp_statistics_exclusion', function($exclude, $visitorProfile) {
if ($exclude['exclusion_match'] === false) {
$visitorIp = $visitorProfile->getIp();
$excludedIps = array('192.168.1.1', '127.0.0.1'); // Add IPs you want to exclude
if (in_array($visitorIp, $excludedIps)) {
$exclude = array('exclusion_match' => true, 'exclusion_reason' => 'Excluded IP');
}
}
return $exclude;
}, 10, 2);
Exclude Visits Based on a Custom Condition
add_filter('wp_statistics_exclusion', function($exclude, $visitorProfile) {
if ($exclude['exclusion_match'] === false) {
// Custom condition logic
$customCondition = some_custom_condition_check(); // Replace with actual condition check
if ($customCondition) {
$exclude = array('exclusion_match' => true, 'exclusion_reason' => 'Custom Condition');
}
}
return $exclude;
}, 10, 2);
function some_custom_condition_check() {
// Example custom condition
return true; // Replace with actual condition logic
}
By adding custom exclusions directly within the wp_statistics_exclusion
filter, you can tailor WP Statistics to meet your specific needs better. This ensures that your analytics data is clean and focused on actual user behaviour, leading to more accurate insights and better decision-making.