• geoggg19

    (@georgetotalize)


    I have been trying the past few days to get this working and can’t seem to. I can get it to block cookies but then not re-activate them when the relevant switch is selected.

    That or clicking the agree button redirects to an admin page.

    I have the js for google analytics in my header.php, am I supposed to wrap it as such?

    if ( is_allowed_cookie( '_ga' ) {
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-XXXXXXXX');
    }
    </script>
    

    And place this in my functions.php?

    add_action( ‘wp_head’, ‘my_google_opt_out’ );
    function my_google_opt_out() {
    if ( ! has_consent( ‘analytics’ ) || ! is_allowed_cookie( ‘_ga’ ) ) {
    ?>
    <script>
    window[‘ga-disable-UA-XXXXXXXX-X’] = true; // Don’t forget to replace X for your actual UA numbers.
    </script>
    <?php
    }
    }

    As certain variations of this seem to work for disabling but not enabling, and others flag up as errors in the code and I’ve been staring at this so long I’m going blind to it.

    I’m sure it’s simple but if anyone can help it would be much appreciated

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi geoggg19,

    You missed a ) on the first line, and the _ga cookie must been added in the plugin at the cookies used on this site section.
    You can use this code: (did not test this)

    add_action( 'wp_head', 'my_google_opt_out' );
    function my_google_opt_out() {
      if ( is_allowed_cookie( '_ga' ) ) {
        ?>
    <script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'GA_TRACKING_ID');
    </script>
        <?php
      }
    }
    Plugin Author Fernando Claussen

    (@fclaussen)

    This redirection to admin-post.php

    Do you see an error or just a blank page?

    Thread Starter geoggg19

    (@georgetotalize)

    @fclaussen

    “We could not verify the the security token. Please try again.”

    I think I may have seen something similar flagged while scanning the support forum

    Cheers

    Plugin Author Fernando Claussen

    (@fclaussen)

    Yea… I’m working on the next release that will hopefully get rid of this problem. Should come out today or tomorrow.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Am I Placing The Code Wrong?’ is closed to new replies.