Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Google Analytics after page load by appending script in head doesn't always work

In the EU, there's a cookie law that requires you to load third-party scripts after the user expresses consent, by clicking, scrolling or navigating for instance. So I load 3 scripts by executing a function that's called after document loads. This is the code:

enter image description here

The problem is that it doesn't always work, nor always fail. I see sessions and activity but I also know for a fact that there are visits that don't trigger the scripts because when I tested it myself on several other computers, not all activity was saved in analytics.

What should I fix in the function to make it work all the time?

like image 855
frenchie Avatar asked Aug 21 '17 10:08

frenchie


People also ask

How long does it take for Google Analytics to populate?

Processing latency is 24-48 hours. Standard accounts that send more than 200,000 sessions per day to Analytics will result in the reports being refreshed only once a day. This can delay updates to reports and metrics for up to two days.

Does Google Analytics slow down page loading?

"The appearance of your website will never be affected by your use of Google Analytics - we don't place any images or text on your pages. Likewise, the performance of your pages won't be impacted, with the possible exception of the very first page-load after you have added the tracking code.

Where do I put the Google Analytics code in my head?

You can put it anywhere you want on page, and ll run anywhere on the page whether it's in the head or the body. But according to Google support (Add the tracking code directly to your site) it is recommended to it in the head tag, paste it immediately before the closing </head> tag.


2 Answers

$(document).ajaxComplete(function() {         var _gaq = _gaq || [];       _gaq.push(['_setAccount', 'UA-XXXXX-X']);       _gaq.push(['_trackPageview']);         var loadGoogleAnalytics = 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);         }     }); 
like image 123
Farhad Bagherlo Avatar answered Sep 19 '22 13:09

Farhad Bagherlo


If you have a look at this post, you can modify it slightly to achieve what you want:

<script type="text/javascript">   var _gaq = _gaq || [];   _gaq.push(['_setAccount', 'UA-XXXXX-X']);   _gaq.push(['_trackPageview']);     var loadGoogleAnalytics = 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> 

Then just call loadGoogleAnalytics() whenever the user agrees with your Cookie Usage display

like image 30
wjvander Avatar answered Sep 19 '22 13:09

wjvander