I have created an extension for Chrome that uses a content script to interact with certain pages, and now I want to add Google Analytics tracking. I have copied the standard extension code from google (putting my correct account id) and added the required permissions in the manifest:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-x']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
When running in the background page, this code works great, but it doesn't help me much as it will run just once when the browser starts. I want it to run whenever my extension interacts with a page, so I put it on the content script, but it doesn't seem to do anything. I don't see any error on the log, and the google analytics debugger doesn't seem to be aware of my tracking code at all.