Title: Blocking Google Analytics
Last modified: June 8, 2018

---

# Blocking Google Analytics

 *  Resolved [allartsoftworks](https://wordpress.org/support/users/allartsoftworks/)
 * (@allartsoftworks)
 * [8 years ago](https://wordpress.org/support/topic/blocking-google-analytics-3/)
 * Hi, I need to block _gat,_gid,_ga cookies BEFORE consent, because if I scan my
   website with [https://www.cookiemetrix.com/it/display-report/www.filoteigroup.it/7086bca8eed383e909ad5ea87869513c](https://www.cookiemetrix.com/it/display-report/www.filoteigroup.it/7086bca8eed383e909ad5ea87869513c)
   it does seems that my website has permanent cookies set before consent.
 * Is it possible to do so?
 * Thank you in advance.
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fblocking-google-analytics-3%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Plugin Author [Fernando Claussen](https://wordpress.org/support/users/fclaussen/)
 * (@fclaussen)
 * [8 years ago](https://wordpress.org/support/topic/blocking-google-analytics-3/#post-10379665)
 * Yes. We have instructions on how to do just that here: [https://gdpr-wp.com/knowledge-base/enabling-or-disabling-functionality-based-on-consent-and-cookies/](https://gdpr-wp.com/knowledge-base/enabling-or-disabling-functionality-based-on-consent-and-cookies/)
 *  Thread Starter [allartsoftworks](https://wordpress.org/support/users/allartsoftworks/)
 * (@allartsoftworks)
 * [8 years ago](https://wordpress.org/support/topic/blocking-google-analytics-3/#post-10379714)
 * Yes, thank you: ten minutes after my post I’ve found this:
 * [https://wordpress.org/support/topic/how-to-set-up-fb-pixel-and-google-analytics-opt-out/#post-10170416](https://wordpress.org/support/topic/how-to-set-up-fb-pixel-and-google-analytics-opt-out/#post-10170416)
 * Sorry, didn’t RTFM, lol.
 * Thank you for the great product, keep up the good work. I’d submit italian translation
   asap.
 *  [bsthp](https://wordpress.org/support/users/bsthp/)
 * (@bsthp)
 * [8 years ago](https://wordpress.org/support/topic/blocking-google-analytics-3/#post-10387597)
 * Fernando, wonder if you can offer a little help on this. I have set my categories
   in the GDPR settings for neccessary, Analytics and AddThis (Social Sharing), 
   and confirmed the relevent cookies in each category. However, I am not clear 
   how to tie this up with the javascript code in the article, mentioned, above (
   since I am using SuperCache, I assume I will need the javascript code);
 *  if ( ! is_allowed_cookie( ‘_ga’ ) ) {
    window[‘ga-disable-UA-XXXXXXXX-X’] = 
   true; // Don’t forget to replace X for your actual UA numbers. }
 * Can you please confirm where I insert this code, including any javascript tags,
   and how I match it to my category settings?
 * For analytics, my code is (inserted in Head) and I want to set it to “Checked”
   i.e. activated, once accepted by teh visitor);
 *     ```
       <!-- Global site tag (gtag.js) - Google Analytics -->
       <script async src="https://www.googletagmanager.com/gtag/js?id=UA-384975-1"></script>
       <script>
         window.dataLayer = window.dataLayer || [];
         function gtag(){dataLayer.push(arguments);}
         gtag('js', new Date());
   
         gtag('config', 'UA-384975-1');
       </script>
       ```
   
 * For AddThis, my code is and I want to set it to “unchecked” i.e. not activated,
   once accepted by the visitor);
 * The code to insert before the closing body tag is;
 *     ```
       <!-- Go to www.addthis.com/dashboard to customize your tools -->
       <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5b1e6471881b140b"></script>
       ```
   
 * The code to insert the Social Share Inline Buttons is;
 *     ```
       <!-- Go to www.addthis.com/dashboard to customize your tools -->
       <div class="addthis_inline_share_toolbox"></div>
       ```
   
 * Hope you can help.
    -  This reply was modified 8 years ago by [bsthp](https://wordpress.org/support/users/bsthp/).
 *  Plugin Author [Fernando Claussen](https://wordpress.org/support/users/fclaussen/)
 * (@fclaussen)
 * [8 years ago](https://wordpress.org/support/topic/blocking-google-analytics-3/#post-10387789)
 * See if this works with caching, I haven’t tried it.
 *     ```
       add_action( 'wp_head', function() {
           <script>
               if ( ! window.is_allowed_cookie( '_ga' ) ) {
                   window[‘ga-disable-UA-384975-1’] = true;
               }
           </script>
       } );
       ```
   
 * Same thing could be done for addthis, replacing _ga for the correct cookie and
   replacing window[‘ga-disable…’] with their script tag.
 * If that doesn’t work, try adding this to a separate javascript file.
 *  [bsthp](https://wordpress.org/support/users/bsthp/)
 * (@bsthp)
 * [8 years ago](https://wordpress.org/support/topic/blocking-google-analytics-3/#post-10387865)
 * Fernando, thanks for getting back. You mean put the above code in my functions.
   php file and remove the existing analytics code from the head tag.
 *  Plugin Author [Fernando Claussen](https://wordpress.org/support/users/fclaussen/)
 * (@fclaussen)
 * [8 years ago](https://wordpress.org/support/topic/blocking-google-analytics-3/#post-10388210)
 * You don’t need to remove the analytics code.
 *  [bsthp](https://wordpress.org/support/users/bsthp/)
 * (@bsthp)
 * [8 years ago](https://wordpress.org/support/topic/blocking-google-analytics-3/#post-10392219)
 * I tried that, by inserting in the functions.php file but, unfortunately, it broke
   the website i.e. white screen. However, i tried the code via your other forum
   post, again by inserting the code in functions file, and it nearly works. The
   code was;
 *     ```
       add_action( 'wp_head', 'my_google_opt_out' );
           function my_google_opt_out() {
             if ( ! is_allowed_cookie( '_ga', 'gid', '_gat_gtag_UA_384975_1' ) ) {
               ?>
               <script>
                 window['ga-disable-UA-384975-1'] = true; // Don't forget to replace X for your actual UA numbers.
               </script>
               <?php
             }
           }
       ```
   
 * When visiting the site for the first time, it works as it should by disabling
   analytic cookies. However, when you accept i.e. turn analytics on, the system
   activates the cookies as it should. However, when you then go back into preferences
   and switch them off again, the analytic cookies are still displaying.
 *  [dlx](https://wordpress.org/support/users/deeluuxe/)
 * (@deeluuxe)
 * [7 years, 12 months ago](https://wordpress.org/support/topic/blocking-google-analytics-3/#post-10397473)
 * Hi [@bsthp](https://wordpress.org/support/users/bsthp/)
 * > However, when you then go back into preferences and switch them off again, 
   > the analytic cookies are still displaying.
 * Did you go back to the page by e.x. the back button of the browser? This does
   not work, try to reload the page and see if it´s fine then.
 * Otherwise it might be a caching problem (if you have any caching plugin).
 *  [bsthp](https://wordpress.org/support/users/bsthp/)
 * (@bsthp)
 * [7 years, 12 months ago](https://wordpress.org/support/topic/blocking-google-analytics-3/#post-10398407)
 * No it was tested on a clean basic install of WP, Caching cleared etc. I have 
   now decided to ditch SpamShield, since, it was causing the conflict with my existing
   GDPR script and install a new Spam Plugin. This new plugin has no issues with
   my GDPR script ie. it does not need cookies to work, so will remain with my existing
   GDPR script. Thank you to everyone who responded.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Blocking Google Analytics’ is closed to new replies.

 * ![](https://ps.w.org/gdpr/assets/icon.svg?rev=1837142)
 * [GDPR](https://wordpress.org/plugins/gdpr/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/gdpr/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/gdpr/)
 * [Active Topics](https://wordpress.org/support/plugin/gdpr/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/gdpr/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/gdpr/reviews/)

## Tags

 * [cookies](https://wordpress.org/support/topic-tag/cookies/)

 * 9 replies
 * 4 participants
 * Last reply from: [bsthp](https://wordpress.org/support/users/bsthp/)
 * Last activity: [7 years, 12 months ago](https://wordpress.org/support/topic/blocking-google-analytics-3/#post-10398407)
 * Status: resolved