Google Analytics tracking code does not work in content of Chrome script extension

I created a Chrome extension that uses content script to interact with specific pages, and now I want to add Google Analytics tracking. I copied the standard extension code from google (putting my correct account id) and adding the necessary permissions to 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 working on the source page, this code works fine, but it doesn’t help me much, since it only starts once when the browser starts. I want it to run whenever my extension interacts with the page, so I put it in the script content, but it does nothing. I see no errors in the log, and the Google Analytics debugger does not seem to know about my tracking code at all.

+4
source share

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


All Articles