• Hey,

    Thanks for the plugin.

    Just want to make sure that I am doing the right thing. I am using google analytics and hotjar on my website. I have set up the cookies using the plugins settings.

    I am using functions.php to right the code in my header section.

    For google analytics the code I am using is:
    (Note: This is pre-checked cookie that the user can uncheck.)

    add_action( 'wp_head', 'google_opt_check');
      function google_opt_check(){
       if ( ! has_consent( 'analytics' ) || ! is_allowed_cookie( '_ga' ) ) {
        ?>
      <script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', 'UA-XXXXXXX-X', 'auto');
    ga('set', 'anonymizeIp', true);
    ga('send', 'pageview');
    </script>
    <script async src='https://www.google-analytics.com/analytics.js'></script>
    <?php }};

    For hotjar (note: this is unchecked cookie that the visitor can check)

    add_action('wp_head', 'hotjar_opt_check');
    function hotjar_opt_check(){
    if ( ! has_consent( 'behaviour_analytics' ) ) {
    ?>
    <script>
        (function(h,o,t,j,a,r){
            h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
            h._hjSettings={hjid:XXXXX,hjsv:X};
            a=o.getElementsByTagName('head')[0];
            r=o.createElement('script');r.async=1;
            r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
            a.appendChild(r);
        })(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
    </script>
    <?php }};

    Let me know if it makes sense.

    Thanks in advance.

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 15 total)
  • Thread Starter jd1234

    (@jd1234)

    Is nobody there?

    Will really appreciate if somebody can point me in the right direction.

    Plugin Author Fernando Claussen

    (@fclaussen)

    Hi

    It seems off for me. Your conditionals have a ! in it.

    So for example, for hotjar, what you are saying is

    If I don’t have consent for behaviour_analytics, set my script.

    I think it should be the opposite, no?

    Also, double check your analytics code. You may want to remove the ! there too.

    You can also ignore the has_consent function and work solely from the cookies. If cookie _ga is enable it means that user gave consent to analytics. You don’t need to check it twice.

    Thread Starter jd1234

    (@jd1234)

    I am checking the google analytics twice as I have set google analytics ‘on’ by default. So it checks if it is ‘allowed cookie’ and if the consent is given. Then it deploys.

    For hotjar what I am trying to say that check for consent and if you have consent then set the cookies?

    Also it seems as though this code is not working.

    Can you help me edit the code?

    Plugin Author Fernando Claussen

    (@fclaussen)

    Like I’ve said, try removing the ! in your conditions and see if it works.

    Thread Starter jd1234

    (@jd1234)

    okay so this works for google analytics.

    add_action( 'wp_head', 'google_opt_check');
      function google_opt_check(){
       if ( is_allowed_cookie( '_ga' ) ) {
        ?>
      <script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', 'UA-XXXXXXXX-X', 'auto');
    ga('set', 'anonymizeIp', true);
    ga('send', 'pageview');
    </script>
    <script async src='https://www.google-analytics.com/analytics.js'></script>
    <?php }
    	else{
    ?>
    	<script>
    		window['ga-disable-UA-XXXXXXX-X'] = true;
    	</script>
    <?php
    }
    }

    The only problem is it is interfering with the cache, I have to clean the cache for the button to work. But this is not a plugin issue so all good there. Will try to find a work around.

    But the hotjar cookie is not working.

    /*code for hotjat with cookie consent*/
    add_action('wp_head', 'hotjar_opt_check');
    function hotjar_opt_check(){
    if ( has_consent( 'behaviour_analytics' ) ) {
    ?>
    <script>
        (function(h,o,t,j,a,r){
            h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
            h._hjSettings={hjid:XXXXX,hjsv:X};
            a=o.getElementsByTagName('head')[0];
            r=o.createElement('script');r.async=1;
            r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
            a.appendChild(r);
        })(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
    </script>
    <?php }
    }

    Can you see whats wrong here?

    • This reply was modified 6 years, 4 months ago by jd1234.
    Plugin Author Fernando Claussen

    (@fclaussen)

    Glad you got it working.

    Hotjar code looks ok to me. It may be caching related too.

    I’m currently rebuilding this components to work from JS instead of PHP to get rid of all these caching problems.

    Thread Starter jd1234

    (@jd1234)

    That would be awesome, if you can get it to work with JS. Thanks Fernando.

    There was one small mistake I think.

    I had ‘$consent_id’ as ‘behaviour_analytics’, I think that was the mistake, it was suppose to me ‘behaviour-analytics’. I just checked the id. But still it doesn’t seem to work.

    I was able to sort the caching issue out for google analytics, which should have resolved hotjar issue too.

    I am using ‘has_consent( $consent_id )’, I think I will try it with is_allowed_cookie( $cookie ). Can I use is_allowed_cookie( $cookie ) here? Hotjar is unchecked to start with.

    Thread Starter jd1234

    (@jd1234)

    As we are on the subject of caching.

    Does this plugin have any issues with cloudflare?

    • This reply was modified 6 years, 4 months ago by jd1234.
    Thread Starter jd1234

    (@jd1234)

    One last question. When can we expect JS checking the cookies and doing the imports? Is it close or far?

    Plugin Author Fernando Claussen

    (@fclaussen)

    Cloudflare introduces caching as far as I know.

    It is close, I expect to have it ready by next week.

    Thread Starter jd1234

    (@jd1234)

    Any update on JS checking the cookies?

    Plugin Author Fernando Claussen

    (@fclaussen)

    Still working on this.
    I got a lot of balls in the air right now. Need to keep the lights on so there are a few things taking priority.

    I think I can get this out this month still. I’m almost done with my other tasks.

    Thread Starter jd1234

    (@jd1234)

    yeah I can understand.

    Good luck. I am looking forward to the plugin update.

    Thanks a lot for your efforts.

    Thread Starter jd1234

    (@jd1234)

    Hi Fernando,

    Hope you are doing well.

    Any update regarding the plugin update?

    Hi Fernando

    I’m also using G.A. and HotJar and wondering if you did something new about caching and JS?

    Are the PHP snippets gave jd1234 will stil the same?

    Thanks

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Google Analytics and Hotjar’ is closed to new replies.