Title: Blocking google analytics script &#8211; errors
Last modified: July 12, 2018

---

# Blocking google analytics script – errors

 *  [waywrdsmeansgns](https://wordpress.org/support/users/waywrdsmeansgns/)
 * (@waywrdsmeansgns)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/blocking-google-analytics-script-errors/)
 * Has anyone successfully set up Cookie Notice to run the GA script only after 
   the user accepts the notice? How did you do it?
 * I would like Google Analytics to run only when the user “accepts” the cookie 
   notice; if they “reject”, then I don’t want GA running. As of right now, GA is
   not running whether they accept or reject.
 * 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?
    -  This topic was modified 7 years, 8 months ago by [waywrdsmeansgns](https://wordpress.org/support/users/waywrdsmeansgns/).

The topic ‘Blocking google analytics script – errors’ is closed to new replies.

 * ![](https://ps.w.org/cookie-notice/assets/icon-256x256.png?rev=2466385)
 * [Cookie Notice & Compliance for GDPR / CCPA](https://wordpress.org/plugins/cookie-notice/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/cookie-notice/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/cookie-notice/)
 * [Active Topics](https://wordpress.org/support/plugin/cookie-notice/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/cookie-notice/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/cookie-notice/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [waywrdsmeansgns](https://wordpress.org/support/users/waywrdsmeansgns/)
 * Last activity: [7 years, 8 months ago](https://wordpress.org/support/topic/blocking-google-analytics-script-errors/)
 * Status: not resolved