Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google Analytics asynchronous code from external JS file

I'm trying to add the asynchronous version of the Google Analytics tracking code to a website.

I'd like to keep the JavaScript in a separate file, and call it from there.

Here's what I've currently got in my .js file:

function addLoadEvent(func) {     var oldonload = window.onload;     if (typeof window.onload != 'function') {         window.onload = func;     } else {         window.onload = function() {             oldonload();             func();         }     } }  function loadtracking() {     var _gaq = _gaq || [];         _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);         _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);         })(); }  addLoadEvent(loadtracking); 

And here's what I've got in the <head> tag of my Master page:

<script type="text/javascript" src="js/google-analytics.js" ></script> 

However, there's obviously a problem as after a few days, I'm not getting stats through!

Any ideas what I need to change?

Thanks, Neil


EDIT: Ok... After some feedback below, I'm going to add the new current contents of my .js file. I'll keep it updated so that if/when this gets solved, it will hopefully help other people trying to do similar things.

var _gaq = _gaq || [];  function loadtracking() {         window._gaq.push(['_setAccount', 'UA-XXXXXXX-X']);         window._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);         })(); }  loadtracking(); 
like image 442
NeilD Avatar asked Jul 16 '10 10:07

NeilD


People also ask

How do I integrate Google Analytics into JavaScript?

Install the Google tag Replace GA_TRACKING_ID with the ID of the Google Analytics property to which you want to send data. You need only one snippet per page. This snippet loads the gtag. js library, establishes GA_TRACKING_ID as the default Google Analytics property ID, and sends a pageview hit to Google Analytics.

How does Google Analytics work with JavaScript?

How does Google Analytics work? Google Analytics acquires user data from each website visitor through the use of page tags. A JavaScript page tag is inserted into the code of each page. This tag runs in the web browser of each visitor, collecting data and sending it to one of Google's data collection servers.

What is more appropriate way to include JavaScript as an external file?

Create external JavaScript file with the extension . js. After creating, add it to the HTML file in the script tag. The src attribute is used to include that external JavaScript file.

Should I use GTAG or Analytics js?

If you are also running Google Ads and you have linked Universal Analytics with it, then I would recommend migration to gtag. js.


2 Answers

Your variable definition var _gaq is inside a function. That means it's locally scoped inside that function and won't exist globally. Google Analytics depends on the global variable _gaq. If you want to keep it inside a function like that, reference it as window._gaq.

like image 158
Brian Avatar answered Sep 17 '22 15:09

Brian


You completely miss the point of the asynchronous tracking code. Don't put it in an external file because that's exactly like including the old synchronous GA.

And most importantly don't defer the tracking code to window.onload as it may fire too late.

If you use the asynchronous GA just put it in the top of you document in an inline script tag. This is the recommendation on Google Analytics website as well.

Insert the asynchronous snippet at the bottom of the <head> section of your pages, after any other scripts your page or template might use.

like image 42
25 revs, 4 users 83% Avatar answered Sep 16 '22 15:09

25 revs, 4 users 83%