0

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 (:

asked Sep 11, 2012 at 9:11
2
  • I don't understand the question. What are you trying to do and what isn't working? Commented Sep 11, 2012 at 9:36
  • @roel ~ At this moment I don't have a lot of details on what is happening, I will have to tell my friend to send me the code so I can be more specific. But what I got is that the _gaq variable wasn't present in the DOM, if I tried to type in the console ._gaq it seemed like it wasn't declared even though it is. If I had an object instead of an array it worked fine, but the _gaq variable needs to be an array as it says in the google analytics tracking code Commented Sep 11, 2012 at 13:41

1 Answer 1

1

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.

answered Sep 11, 2012 at 15:10
Sign up to request clarification or add additional context in comments.

4 Comments

~ precisely, the variable it's initializes like that var _gaq = _gaq || []; . But I'm not quite sure why he had problems with that, I suppose doing this _gaq : _gaq || [] it's not possible or semantically correct ?
The problem is you end up with two different _gaqs -- the global _gaq used by the analytics code & a second _gaq that's defined as a property of SOMEOBJECT.
What if you remove the _gaq initialization from the SOMEOBJECT code, and init it outside... something like: var _gaq = _gaq || []; var SOMEOBJECT = { ...
That could work :) Though I'd prefer keeping it inside the object ... I'll try that to see what happens as soon as I get my hands on the whole code ( it's a big .net project so I can only run locally from the TFS )

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.