views:

73

answers:

2

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();
+1  A: 

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.

galambalazs
I take your point on the AddLoadEvent() code, and I've removed all of the window.onload parts... :) Do you mind if I ask you to go into some more depth on how the external file will make it "exactly like including the old synchronous GA". Why would splitting it into a separate file cause it to execute differently? I have read the recommendation, but I have a need in this project for the GA code to be separate from the master page.
NeilD
The big benefit of having the async tracking is that you can start pushing events into GA before the code has loaded. As soon as your page can start pushing elements into the _gaq array, you can start storing events. On the contrary, when you force the variable outside of your main script, you're forcing the tracking to wait until after your external script has downloaded. That's how it's exactly like the old script.
Chase
likewise, forcing your events to wait until the onload function is fired via window.onload <--- will force you to wait even longer... until every image is done downloading; What if some retarded file get's hung up?
Chase
Ok, thanks Chase... I've already removed the Window.Onload bits, and I'm now going to try rescoping the _gaq variable like Brian suggests in the other answer.
NeilD
+2  A: 

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

Brian