News & Updates

How to Opt Out of Google Analytics Using Site Kit

By Victoria Shaw 7 min read 1800 views

How to Opt Out of Google Analytics Using Site Kit

If you’ve set up Google Analytics through WordPress’s Site Kit plugin, you might wonder how to give your visitors a clean break from tracking. The good news is that opting out isn’t a cliff‑jump; it’s a handful of settings and a tiny snippet of code. Below, we walk through the why, the where, and the how, so you can respect privacy without breaking your site’s functionality.

Why Consider an Opt‑Out?

Even if you love the insights that Analytics provides—traffic sources, bounce rates, user flow—some visitors prefer to stay invisible. Offering an opt‑out builds trust and can even keep you on the right side of emerging data‑privacy laws. It’s not about scrapping data altogether; it’s about giving control back to the user.

Understanding Site Kit’s Role

Site Kit streamlines the connection between your WordPress site and Google services. Once you authenticate, it injects the Analytics tracking code automatically. That convenience means you also need a clear path to disable that code when a user asks for it.

  • Built‑in integration: Site Kit adds the gtag.js script in the site’s header.
  • Central control: All Google service configurations sit under one dashboard.
  • Flexibility: With a few filters or a plugin, you can conditionally remove the script.

Step‑by‑Step Guide to Implement the Opt‑Out

1. Create an Opt‑Out Cookie

First, you need a cookie that signals a user’s preference. Add the following JavaScript to a custom functions.php snippet or a small dedicated plugin:

function ga_opt_out() {

if (isset($_GET['ga_opt_out'])) {

setcookie('ga_opt_out', '1', time()+31536000, '/');

wp_redirect(remove_query_arg('ga_opt_out'));

exit;

}

}

add_action('init', 'ga_opt_out');

This code checks for a ?ga_opt_out query parameter, sets a one‑year cookie, and then redirects to a clean URL.

2. Adjust Site Kit’s Tracking Injection

Site Kit offers a filter called sitekit_google_analytics_script. Hook into it to suppress the script when the cookie is present:

function disable_ga_if_opted_out($script) {

if (isset($_COOKIE['ga_opt_out'])) {

return ''; // Return empty string to stop output

}

return $script;

}

add_filter('sitekit_google_analytics_script', 'disable_ga_if_opted_out');

When the cookie exists, the filter returns an empty string, effectively removing the gtag.js tag from the page.

3. Add a User‑Facing Opt‑Out Link

Now give visitors a way to trigger the process. Somewhere in your footer or privacy page, insert:

<a href="">Turn off Google Analytics</a>

Clicking that link sets the cookie, reloads the page, and the tracking script disappears.

4. Verify It Works

Open your site in a private browser window, click the opt‑out link, then inspect the page source. You shouldn’t see any gtag.js reference. Clear the cookie or use a different browser to re‑enable tracking for testing.

Optional Enhancements

  • Custom banner: Show a banner that explains the opt‑out and offers the link.
  • Localized text: Use __() functions to translate the link for multilingual sites.
  • GDPR compliance: Pair the opt‑out with a consent management platform for broader coverage.

Common Pitfalls to Avoid

Don’t rely solely on JavaScript. If a visitor disables JS, the script never runs, but the cookie won’t be set either. That’s why the PHP check on init is essential—it runs before any client‑side code.

Beware of caching. Page‑cache plugins may serve a version of the page that already contains the Analytics script, even after the user opted out. Configure your cache to bypass logged‑in users or to vary by the ga_opt_out cookie.

Wrapping It Up

Offering a simple opt‑out through Site Kit shows respect for visitor privacy while keeping your analytics intact for those who consent. With just a few lines of code and a clear link, you strike a balance between data insight and user choice—something every modern website should aim for.

The Top 7 Best Google Analytics Plugins for WordPress
Google Site Kit Installation - PODTech
Your Quick Guide to Google Site Kit - ManageWP
How To Connect Google Analytics Using Google Site Kit WordPress Plugin ...

Written by Victoria Shaw

Victoria Shaw is a Chief Correspondent with over a decade of experience covering breaking trends, in-depth analysis, and exclusive insights.