Roland
Roland

Reputation: 9731

Google Analytics Issue

I was looking over a friends script and he used the Google Analytics tracking code :

var _gaq = [
        ['_setAccount', 'UA-XXXXXXXX-X'],
        ['_trackPageview']
    ];
    (function(d, t) {
        var g = d.createElement(t),
            s = d.getElementsByTagName(t)[0];
        g.src = ('https:' == location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js';
        s.parentNode.insertBefore(g, s)
    }(document, 'script'));

in this way ( or something similar ) :

var SOMEOBJECT = {
    _gaq : [],
    account_code : "",

    ...

    init : function() {
        ...
    }

    ...

    _gaq.push(SOMEOBJECT.account_code);

    ...
}

and some more code to create the same tracking code but in a different way ( because it needs to be reused on many other pages and for various stuff ).

The idea is that the scope variable _gaq didn't seem to be present in the console, so Analytics didn't received any data. So what could be going wrong ? Sorry for not having more code, but this is from what I remember and I was very curios why it didn't work (:

Upvotes: 0

Views: 147

Answers (1)

mike
mike

Reputation: 7177

The first part of your code looks good... just a refactoring of the normal Google Analytics async code.

I'm not sure sure about the second part of your code... normally _gaq is a global object initialized something like

var _gaq = _gaq || [];

which initializes a global _gaq as an array if it hasn't been initialized already. Once the Google Analytics code is loaded, the array is replaced with an object containing a push method that executes commands.

Take a look at the docs for the _gaq Global Object and the push method.

Upvotes: 1

Related Questions