Thanks for reaching out @brendanshanahan. There is a filter that can be used to exclude users by their capability (and not their role as this can be unreliable, see this reference). I have an example below which will exclude ads for editors:
add_filter( 'googlesitekit_analytics-4_blocked' , 'restrict_analytics_snippet_role' );
function restrict_analytics_snippet_role() {
// restrict GA snippet tracking for editors programmatically
if ( current_user_can('edit_posts')) {
return true;
}
return false;
}
The above will restrict the Analytics snippet placement via Site Kit, so logged in editors won’t be counted in Google Analytics traffic. This would be ideally added to a custom snippets plugin, an individual plugin with this snippet, or to a child themes functions.php file. Adding to your parent themes functions.php file should work, although any themes updates will result in this being overwritten.
Let me know if you have any questions with the above.
Just a quick update on the above, the filter is actually googlesitekit_analytics-4_tag_blocked and not googlesitekit_analytics-4_blocked. The snippet that will work to restrict the Google Analytics snippet placement by Site Kit, for those with edit access is below:
add_filter( 'googlesitekit_analytics-4_tag_blocked' , 'restrict_analytics_snippet_role' );
function restrict_analytics_snippet_role() {
// restrict GA snippet tracking for editors programmatically
if ( current_user_can('edit_posts')) {
return true;
}
return false;
}
Let me know if you have any questions with the above.
Thanks James!
For my requirement, I changed the capability to current_user_can(‘administrator’)
Good to know @brendanshanahan. Using that filter for administrators along is indeed more restrictive that “Users who can write posts”. Be sure to reach out if you have any further questions.