• Has anyone successfully set up Cookie Notice to run the GA script only after the user accepts the notice? How did you do it?

    As of right now, GA is not running on my site whether they accept or reject the cookie notice.

    Here are the steps I have taken:

    I removed the GA script from my header.php file. I then pasted the GA script into the Script blocking field, making sure to “Enable to give to the user the possibility to refuse third party non functional cookies” first. After saving the plugin, clearing my caches, etc., I clear all browser cookies & history, and re-visited my website. While viewing the website on Chrome with DevTools, I accept the cookie notice, and the cookie_notice_accepted value is then true. I would expect the GA cookies to load after that, but they are not.

    Here is the script I pasted into the script blocking field (replacing xxxx for my actual ID):

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxx"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-xxxxxx');
    </script>

    Now here is the interesting thing – perhaps this could be the cause of the problem? When I save the settings in Cookie Notice, the script changes to this:

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <a href="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxx">https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxx</a>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-xxxxxx');
    </script>

    Notice the <a href="..."> etc. If I manually change this back to the original <script async src=....> it reverts to <a href="..."> upon saving. Could this be the cause of the problem? and do others experience this / has anyone found a solution?

Viewing 15 replies - 1 through 15 (of 16 total)
  • I can’t answer about the second part of the message, but I use the plugin on many sites blocking Analytics code “manually” using the suggested

    if ( function_exists(‘cn_cookies_accepted’) && cn_cookies_accepted() ) {
    // Your third-party non functional code here
    }

    It’s really a piece of cake, just edit the header php file of your theme/installation and add the Gtag code inside the if statement.
    It involves editing the source, I know, but it’s really an easy task (use a child theme, if possible).

    Thread Starter waywrdsmeansgns

    (@waywrdsmeansgns)

    thanks @neroavorio

    This doesn’t seem to work for me either – now the GA cookies are there regardless of whether I “accept” or “reject” or do nothing. Maybe I have entered the code incorrectly? here’s what’s in my header.php, just before the </head> tag :

    if ( function_exists('cn_cookies_accepted') && cn_cookies_accepted() ) {
    <!-- Global Site Tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=xxxxxxxx"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments)};
      gtag('js', new Date());
    
      gtag('config', 'UA-xxxxxxx');
    </script>
    }

    Do you have anything in the “script blocking” field in the Cookie Notice setting/configuration page?

    No, I leave the “script blocking” field empty, I never tried to use the built-in function.
    Your code seems to be ok.
    Did you remove ALL the cookies (including analytics and cookie_notice_accepted for your site before testing?

    Thread Starter waywrdsmeansgns

    (@waywrdsmeansgns)

    @neroavorio after making the changes to the header.php file, I “purged all caches” via the W3 Total plugin. Then I cleared browsing data from all time on chrome, including “cookies and other site data” – I assume that’s what you mean? after that, I revisit the page and check the cookies in DevTools > Applications > Cookies.

    Having just done this again, now I am literally seeing the message if ( function_exists('cn_cookies_accepted') && cn_cookies_accepted() ) { } at the top of the site.

    Wait wait. Now I saw the code you wrote above… it’s NOT ok ??
    you mixed PHP and Javascript!

    The snippet should be like this

    <?php
    if ( function_exists(‘cn_cookies_accepted’) && cn_cookies_accepted() ) {
    ?>
    <!– Global Site Tag (gtag.js) – Google Analytics –>
    <script async src=”https://www.googletagmanager.com/gtag/js?id=xxxxxxxx”></script&gt;
    <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments)};
    gtag(‘js’, new Date());

    gtag(‘config’, ‘UA-xxxxxxx’);
    </script>
    <?php
    }
    ?>

    Thread Starter waywrdsmeansgns

    (@waywrdsmeansgns)

    well that won’t do, will it!

    But alas, it’s still not working for me – the GA cookies aren’t loading when I click “accept”.

    Here is the final 23 lines of my header.php file – including the extra few at the top to make sure there is no competing unclosed tags, etc

    	<link rel="profile" href="https://gmpg.org/xfn/11" />
    	<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
    	<link rel="shortcut icon" type="image/x-icon" href="<?php echo esc_url($qode_options_proya['favicon_image']); ?>">
    	<link rel="apple-touch-icon" href="<?php echo esc_url($qode_options_proya['favicon_image']); ?>"/>
    	<?php wp_head(); ?>
    
    <?php
    if ( function_exists(‘cn_cookies_accepted’) && cn_cookies_accepted() ) {
    ?>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxx"></script>
    <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    
    gtag('config', 'UA-xxxxxxx');
    </script>
    <?php
    }
    ?>
    </head>

    That’s weird. I assume you enabled the RELOADING option. If you see the cookie but you don’t see the analytics script nor the ga-gat-gid cookies, I’m out of suggestions ??

    Thread Starter waywrdsmeansgns

    (@waywrdsmeansgns)

    Yeah, reloading is enabled and right – once I accept, I get the cookie_notice_accepted with value as true but no ga cookies!

    Well, thank you so much for trying – i really appreciate it!!

    One thing I haven’t tried is disabling other plugins, to see if there is some issue cross-plugins.. or maybe someone else has another solution, fingers crossed!

    Thread Starter waywrdsmeansgns

    (@waywrdsmeansgns)

    Thanks @brianspurling
    The post you link is great for explaining the concept, but doesn’t help me execute it.
    At this point, I’m hoping to hear from people who have successfully blocked the Google Analytics script via the plugin’s script blocking field. Or else people who have an alternate method of blocking the script, i.e. via header.php file.

    Yes, sorry, not very helpful of me!

    Here’s what seemed to work for me:

    – I removed (“turned off”) the existing mechanism that was being used for embedding the GA script into my pages (it was a plugin built into my WP theme)
    – I pasted the GA script into the “Script blocking” > “Head” section of Cookie Notice’s settings page

    And that, I think, was it!

    My understanding is that if you want Cookie Notice to control when GA cookies are created, you must let Cookie Notice take care of the injection of the script.

    But this is all just guess work. It seems to work, but perhaps there’s something else going that I missed.

    Thread Starter waywrdsmeansgns

    (@waywrdsmeansgns)

    Thanks @brianspurling
    You pasted just the script, straight from how GA gives it to you, yeah?
    Would you mind sending me a link to your website so I can take a look?

    Sorry, bad timing. I’ve just switched the site back to soft-opt in! So it’s not going to help you.

    But in answer to your question, yes, I pasted the scrip straight from GA.

    I.e.

    <!– Global site tag (gtag.js) – Google Analytics –>
    <script async src=”…”>

    </script>

    Thread Starter waywrdsmeansgns

    (@waywrdsmeansgns)

    Ah, well thank you.
    That method hasn’t been working for me, unfortunately. Possibly because the script in the script blocking field changes from <script async src= to <a href= when I save the settings…

    Thread Starter waywrdsmeansgns

    (@waywrdsmeansgns)

    If I may ask, why did you switch back to soft opt-in?

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Has anyone successfully blocked google analytics script?’ is closed to new replies.