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' : 'https://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!
- The topic ‘Exclude footer with Google Analytics cookie check?’ is closed to new replies.