• Resolved Brendan Shanahan

    (@brendanshanahan)


    Is there a way to configure sitekit analytics to exclude by user role. The existing settings ‘users that can write posts’ is too broad for me as I use Hivepress which grants users the Contributor role in order to post listings. I would prefer to only exclude administrators from Analytics.

    Thanks,
    Brendan

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support James Osborne

    (@jamesosborne)

    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.

    Plugin Support James Osborne

    (@jamesosborne)

    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.

    Thread Starter Brendan Shanahan

    (@brendanshanahan)

    Thanks James!

    For my requirement, I changed the capability to current_user_can(‘administrator’)

    Plugin Support James Osborne

    (@jamesosborne)

    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.