Debugging Google Analytics Tracking Error (_gaq.push returns "3")

Summarize:

I misunderstood the use of content script. And that leads to this question. Here is a quote from a Google white paper:

Execution conditions

Content scripts are executed in a special environment called Isolated World. They have access to the DOM of the page to which they are entered, but not for any JavaScript variables or functions created by this page. It looks for each content script as if there was no other JavaScript running on the page on which it is running. The same is true in the opposite : JavaScript on a page cannot call any functions or variables defined by content scripts.

Read More: Content Tracking Script Using Google Analytics

-

I am adding tracking code to the Chrome extension. But when I test it, _gaq.push returns number 3. When I tested it, I decided that if I call _gaq.push in the extension, the number would continue to grow. But when I call it in the console, everything is fine (all the same code).

I am wondering how can I do this to more deeply trace this error?

Thanks so much for every answer!

UPDATE:

When I trace the return value of _gaq, it is still an array. That means ga.js is not loading, right?

But it seems ga.js never loads in my script. If I do not manually find _gaq in the console, this is an object. It is very strange.

I checked the DOM and found that the tag has already been added.

I looked at the Network panel and found that ga.js does not load into the queue at all. But why can I still use the console to access the _gaq object?

UPDATE2:

I use console.log to track the value of "window._gaq" and find that the return value of console.log (window._gaq) is DIFFERENT than directly enter "window._gaq" in the console. These are completely two different objects. I even use the setInterval function to register the results and save the returned array, not the expected object.

+4
source share
1 answer

When you went through the script _gaq , this is a regular Array. And like any array in JavaScript, it has a push method that inserts one or more elements into the array and returns the final length of the array.

eg:

 var _gaq = []; > undefined _gaq.push(['_setAccount', 'UA-XXXXX-X']); > 1 _gaq.push(['_setDomainName', 'mysite.com']); > 2 _gaq.push(['_trackPageview'], ['_trackEvent', 'cat', 'act']); > 4 

After loading the ga.js file, the _gaq array _gaq read, so the instructions inserted into it can be processed, and then the _gaq array _gaq replaced by the object. The object also implements the push function. But note that in this case, the push function will execute the instruction in the ga.js library, and then return 0.

eg:

 var _gaq = { push: function(){ // Do something here return 0; } }; > undefined _gaq.push(['_trackPageview']) > 0 

This is a very smart Google design for creating an API that can be used even before loading the library (ga.js).

That's why during your script _gaq.push there is an Array class method and returns increasing numbers, and on the console it always returns 0.

It's not a problem. It seems to be working as intended.

UPDATE

It seems that ga.js is already loaded. It may have been cached and does not appear on the network panel.

ga.js is loaded with this part of the tracking script:

  (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); })(); 

Loads asynchronously. Thus, after executing this fragment, it will be loaded, but the exact point of its loading is unknown.

If you need to know when the ga.js file is uploaded, you can push the callback on _gaq and it will be executed when the ga.js file is uploaded.

eg:

 _gaq.push(function(){ console.log('ga.js loaded!'); debugger; }); 
+7
source

Source: https://habr.com/ru/post/1440908/


All Articles