5

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.

drorw
  • 677
  • 1
  • 8
  • 18
  • 1
    possible duplicate of [Chrome extension adding external javascript to current page's html](http://stackoverflow.com/questions/10285886/chrome-extension-adding-external-javascript-to-current-pages-html) – Rob W Jul 03 '13 at 14:58
  • 1
    Fantastic! the solution in http://stackoverflow.com/questions/10285886/chrome-extension-adding-external-javascript-to-current-pages-html works! How bad is it to include ga.js in the extension itself? Is it likely to break? The extension updates automatically once I publish a new version. Is it – drorw Jul 03 '13 at 21:03

0 Answers0