How do you integrate Universal Analytics in to Chrome Extensions?

前端 未结 6 1927
故里飘歌
故里飘歌 2020-11-29 18:36

The chrome extension guide has a tutorial for the old analytics install: https://developer.chrome.com/extensions/tut_analytics.html

The instructions just say to link

6条回答
  •  囚心锁ツ
    2020-11-29 19:21

    I just encountered this and seem to have hacked my way through. This might break at some point or not be fully functional, but here goes:

    • Download the GA uglified+minified source code from here: https://www.google-analytics.com/analytics.js, put in your chrome extension folder, where it could be later loaded by the background page.

    • In it, find a function that looks something like this:

    function Oa(){var a=M[B][E];if("http:"!=a&&"https:"!=a)throw"abort";}. 
    

    This is the "point of failure" since our "protocol" is "chrome-extension:" and not either of the two.

    • So.. change this function to be something like:
    function Oa(){var a=M[B][E];if("chrome-extension:"!=a&&"http:"!=a&&"https:"!=a)throw"abort";}
    
    • add a "Content Security Policy" of this sort to your manifest file, make sure it points to YOUR LOCAL version of analytics.js you have just modified:
    "content_security_policy": "script-src 'self'  chrome-extension://EXTENSIONID/path/to/analytics.js;  object-src 'self'",
    
    • Change the GA snippet to ALSO point to that same file, something like this:
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','chrome-extension://EXTENSIONID/path/to/analytics.js','ga');
    

    hope this helps.

提交回复
热议问题