When I realized that I cannot track directly in content script. I start to work with background HTML tracking my data. via Content Script tracking with Google Analytics
When I set up my background script, I figured out that it doesn't support inline script. So I put the code in a js file and use "src=filename.js" to include that. via chrome extension insert content script on browser action
But finally there is a trouble: I cannot load ga.js at all because it still violates the rule. Here is what I got:
Refused to load the script 'https://ssl.google-analytics.com/ga.js' because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".
My extension structure:
- background.html
- script.js
- tracker.js
More information about this issue:
background.html:
<html>
<script src="tracker.js"></script>
<body></body>
</html>
tracker.js: (I hide my ID)
var _gaq = _gaq || [];
_gaq.push(['_setAccount', _gaID]);
_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);
})();
Thanks for any kind of help!