Title: right code for google analytics
Last modified: August 30, 2016

---

# right code for google analytics

 *  [kayak-adv](https://wordpress.org/support/users/kayak-adv/)
 * (@kayak-adv)
 * [11 years ago](https://wordpress.org/support/topic/right-code-for-google-analytics/)
 * i’m trying to use your code for blocking analytics cookies until accepting the
   policy
    here is my code, but is not working… where am i wrong?
 * thank you
 *     ```
       <?php
       if ( function_exists('cn_cookies_accepted') && cn_cookies_accepted() ) {
           // Your third-party non functional code here
           ?>
           <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', 'UA-xxxxx-1', 'auto');
         ga('set', 'anonymizeIp', true);
         ga('send', 'pageview');
       </script>
   
           <?php
       }
       ?>
       ```
   
 * [https://wordpress.org/plugins/cookie-notice/](https://wordpress.org/plugins/cookie-notice/)

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

 *  Plugin Author [dFactory](https://wordpress.org/support/users/dfactory/)
 * (@dfactory)
 * [11 years ago](https://wordpress.org/support/topic/right-code-for-google-analytics/#post-6197449)
 * It that code located in your theme or functions.php file?
 *  Thread Starter [kayak-adv](https://wordpress.org/support/users/kayak-adv/)
 * (@kayak-adv)
 * [11 years ago](https://wordpress.org/support/topic/right-code-for-google-analytics/#post-6197495)
 * is in the footer.php. should be in the function.php?
 *  [leclisse](https://wordpress.org/support/users/leclisse/)
 * (@leclisse)
 * [11 years ago](https://wordpress.org/support/topic/right-code-for-google-analytics/#post-6197519)
 * try
 *     ```
       <?php if( function_exists( 'cn_cookies_accepted' ) && cn_cookies_accepted() ): ?>
   
       // analytics code here
   
       <?php endif; ?>
       ```
   
 *  Plugin Author [dFactory](https://wordpress.org/support/users/dfactory/)
 * (@dfactory)
 * [11 years ago](https://wordpress.org/support/topic/right-code-for-google-analytics/#post-6197521)
 * It should be working fine. How do you check if it’s woring or not? Can you post
   a link to your site?
 *  Thread Starter [kayak-adv](https://wordpress.org/support/users/kayak-adv/)
 * (@kayak-adv)
 * [11 years ago](https://wordpress.org/support/topic/right-code-for-google-analytics/#post-6197525)
 * the problem is not solved
    I am using ghostery to check the cookies loaded
 * now my code is this, as suggested by leclisse:
 *     ```
       <?php if( function_exists( 'cn_cookies_accepted' ) && cn_cookies_accepted() ): ?>
           <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', 'UA-xxxxx-1', 'auto');
         ga('set', 'anonymizeIp', true);
         ga('send', 'pageview');
       </script>
       <?php endif; ?>
       ```
   
 * my website: [http://www.kayak-adv.com](http://www.kayak-adv.com)
 * thank you
 *  Plugin Author [dFactory](https://wordpress.org/support/users/dfactory/)
 * (@dfactory)
 * [11 years ago](https://wordpress.org/support/topic/right-code-for-google-analytics/#post-6197543)
 * Your code is ok and it does not work. I’ll get back to you.
 *  Plugin Author [dFactory](https://wordpress.org/support/users/dfactory/)
 * (@dfactory)
 * [11 years ago](https://wordpress.org/support/topic/right-code-for-google-analytics/#post-6197546)
 * Tested again, on your code – everything works fine.
 * Are you 100% sure that the template part you pasted this code to is used in a
   theme?
 * If you do this, you’ll have a confirmation
 *     ```
       <?php if( function_exists( 'cn_cookies_accepted' ) && cn_cookies_accepted() ) { ?>
       	<?php echo 'Cookie accepted!'; ?>
       <?php } else { ?>
       	<?php echo 'Cookie is NOT accepted!'; ?>
       <?php } ?>
       ```
   
 *  Thread Starter [kayak-adv](https://wordpress.org/support/users/kayak-adv/)
 * (@kayak-adv)
 * [10 years, 12 months ago](https://wordpress.org/support/topic/right-code-for-google-analytics/#post-6197703)
 * adding your code doesn’t really work
    i have Cookie is NOT accepted! even when
   i press ok
 * the code is in the footer
 *     ```
       </body>
       <?php if( function_exists( 'cn_cookies_accepted' ) && cn_cookies_accepted() ) { ?>
       	<?php echo 'Cookie accepted!'; ?>
       <?php } else { ?>
       	<?php echo 'Cookie is NOT accepted!'; ?>
       <?php } ?>
       </html>
       ```
   

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

The topic ‘right code for google analytics’ is closed to new replies.

 * ![](https://ps.w.org/cookie-notice/assets/icon-256x256.png?rev=3549327)
 * [Compliance by Hu-manity.co](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/)

 * 8 replies
 * 3 participants
 * Last reply from: [kayak-adv](https://wordpress.org/support/users/kayak-adv/)
 * Last activity: [10 years, 12 months ago](https://wordpress.org/support/topic/right-code-for-google-analytics/#post-6197703)
 * Status: not resolved