Support » Plugin: W3 Total Cache » Exclude footer with Google Analytics cookie check?

  • Hi there,

    I have the following setup;
    – I Added a cookie accept bar with CookieCuttr to the head of my pages
    – In my functions.php I do a check if the cookie is set and is accepted and if so add the Google Analytics script to the footer of all the pages like this:

    // Include the Google Analytics Tracking Code (ga.js)
    function google_analytics_tracking_code(){
    	$propertyID = 'UA-XXXXXX-XX'; // GA Property ID
    	if ($propertyID) { ?>
    <!-- Google Analytics -->
    <script type="text/javascript">
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', '<?php echo $propertyID; ?>']);
      _gaq.push(['_trackPageview']);
    
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    </script>
      <?php }
    }
    // Add script to footer
    if (isset($_COOKIE['cc_cookie_accept']) && $_COOKIE['cc_cookie_accept'] == 'cc_cookie_accept') {
      add_action('wp_footer', 'google_analytics_tracking_code', 100);
    } ?>

    – Excluded my functions.php and footer.php files from the Page Cache like this:

    functions\.php
    footer\.php

    But this doesn’t work. If cookies aren’t accepted yet, the Google Analytics code isn’t in the footer, so that works. But when I accept the cookies, the page refreshes automatically but the GA code is still not in the footer. However, when I login in WP and empty the cache, the GA code is present.

    So my question is; how can I prevent this?
    What should I exclude exactly? Or is there an easier way to do this?

    Thanks!

    https://wordpress.org/plugins/w3-total-cache/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Disabling Page Cache results in this working properly?

    Thread Starter Twansparant

    (@twansparant)

    Disabling Page Cache results in this working properly?

    Correct, it works when I disable Page Cache

    Have you tried adding cc_cookie_accept to the Rejected cookies field of the Page Cache settings page?

    Thread Starter Twansparant

    (@twansparant)

    No, but I just tried it and that works!
    However, wouldn’t that mean in theory that it will almost never cache any pages since 95% will accept the cookies?

    It’s best if you enable debug mode for Page Cache at the bottom of the General Settings page and observe the results for yourself.

    Thread Starter Twansparant

    (@twansparant)

    As soon as cookies are accepted, page cache is disabled:

    <!-- W3 Total Cache: Page cache debug info:
    Engine:             disk: enhanced
    Caching:            disabled
    Reject reason:      Cookie is rejected
    -->

    It should be the other way around actually, no page cache if cookie isn’t present yet.
    Is that possible or do I have to set a second cookie on page load that will be deleted when cc_cookie_accept is set to ‘cc_cookie_accept’?

    […] or do I have to set a second cookie on page load that will be deleted when cc_cookie_accept is set to ‘cc_cookie_accept’?

    You might need to test this.

    Thread Starter Twansparant

    (@twansparant)

    This seems to work:

    // Check if cookies are accepted
    if ($.cookie('cc_cookie_accept') == 'cc_cookie_accept') {
      console.log('cookies accepted');
      $.removeCookie('cc_page_cache', {
        path    : '/',
        domain  : 'mydomain.com'
      });
    } else {
      console.log('cookies not accepted');
      $.cookie('cc_page_cache', 'no_page_cache', {
        path    : '/',
        domain  : 'mydomain.com'
      });
    }

    Then in the Page Cache settings I added:
    Rejected Cookies: cc_page_cache

    Only thing is, on the moment the cached pages were created and cookies weren’t accepted yet at that point, you will be served the cached pages without the Google Analytics in it although you did accepted the cookies by that time…

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Exclude footer with Google Analytics cookie check?’ is closed to new replies.