• 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/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author dFactory

    (@dfactory)

    It that code located in your theme or functions.php file?

    Thread Starter kayak-adv

    (@kayak-adv)

    is in the footer.php. should be in the function.php?

    try

    <?php if( function_exists( 'cn_cookies_accepted' ) && cn_cookies_accepted() ): ?>
    
    // analytics code here
    
    <?php endif; ?>
    Plugin Author dFactory

    (@dfactory)

    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

    (@kayak-adv)

    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

    thank you

    Plugin Author dFactory

    (@dfactory)

    Your code is ok and it does not work. I’ll get back to you.

    Plugin Author dFactory

    (@dfactory)

    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

    (@kayak-adv)

    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.