Code Bytes
Keep your WordPress team's visits out of analytics
Exclude editorial users at the tracking integration, check caching and consent, and avoid reviving the retired Universal Analytics script.
Repeated editing and checking can distort a small site's analytics. The useful distinction is usually “people working on the website”, not “everyone who has an account”—customers and members may be logged in too.
Start with the integration that owns the tag
If an analytics plugin or consent manager inserts the tag, use its documented role-exclusion setting or filter. Do not add a second tracking script. The old analytics.js/UA-… example is for Universal Analytics, which has been replaced by GA4.
For a custom integration that already enqueues a script using the handle site-analytics, this site-plugin example excludes users who can edit posts:
<?php
add_action('wp_enqueue_scripts', static function (): void {
if (current_user_can('edit_posts')) {
wp_dequeue_script('site-analytics');
}
}, 100);
Use the actual handle registered by your code. Run this after that script is enqueued and before output. It will not remove a tag hard-coded in a template, an iframe, a dependent script that causes it to be re-enqueued, or tracking injected by another system. For code you own, checking the same capability before enqueueing is even clearer.
Verify the collection, not just the source
Inspect browser network requests as an editor and again as a logged-out visitor with the same consent choice. Confirm the intended requests disappear only for the editorial session. Ensure full-page caches do not serve one user's tracking decision to somebody else.
GA4's internal-traffic filters are another option for a team with known network addresses. Start in their testing state: activating an exclusion changes collected reporting data, not just the view you are looking at. Analytics exclusions do not replace consent handling.
References: WordPress capabilities, dequeueing scripts, GA4 internal traffic and Universal Analytics retirement.
Original version19 June 2013
Kept here for reference and earlier links. The updated guide above is the recommended starting point; older code may depend on retired services or different software versions.
As a Wordpress admin/blogger, you will no doubt spend a lot of time browsing throughout your own site. If you are using Google Analytics this can create false results, and although you can configure Google Analytics to ignore certain cookies/IPs etc, I find it easier to just not load the Google Analytics script for Admins/Editors.
To hide Google Analytics from Wordpress Editors, use the following:
<?php
if (!is_user_logged_in() ) {
echo "<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '# Change to your Google UA Code #', '# Change to your Domain #');
ga('send', 'pageview');
</script>";
}
?>
Just ensure to change the line with comments on so that the Analytics snippet has your UA and URL in it.
Keep exploring