SUMMARIZE:
I misunderstood the usage of content script. And that leads to this issue. Here is a quote from Google's official doc:
Execution environment
Content scripts execute in a special environment called an isolated world. They have access to the DOM of the page they are injected into, but not to any JavaScript variables or functions created by the page. It looks to each content script as if there is no other JavaScript executing on the page it is running on. The same is true in reverse: JavaScript running on the page cannot call any functions or access any variables defined by content scripts.
More details: Content Script tracking with Google Analytics
--
I am adding some tracking code into a Chrome Extension. But when I test it, the _gaq.push returns number 3. As I tested, I figured that if I call _gaq.push in the extension, the number will keep rising. But when I call it in the console, it's all fine (all the same code).
I am wondering how I can do to track deeper into this error?
Thanks very much for every answer!
UPDATE:
When I trace the return value of _gaq, it is still an array. That means ga.js is not loaded, right?
But it seems ga.js is never loaded in my script. Unless I manually type _gaq in the console, it's an object. This is very weird.
I checked the DOM and found that tag is already added.
I reviewed the Network panel, find that ga.js is not loaded at all in the queue. But why I can still use console to access the _gaq object?
UPDATE2:
I use console.log to track the value of "window._gaq" and found that the return value of console.log(window._gaq) is DIFFERENT than directly type "window._gaq" in the console. It's totally two different objects. I even use a setInterval function to log the results and it's keeping returning the array, not the expected object.