How do you integrate Universal Analytics in to Chrome Extensions?

前端 未结 6 1924
故里飘歌
故里飘歌 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:28

    I wrote up a blog post on this - How to add Google’s Universal Analytics tracking to a Chrome extension

    Here's the guts of it:

    // Standard Google Universal Analytics code
    (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','https://www.google-analytics.com/analytics.js','ga'); // Note: https protocol here
    
    ga('create', 'UA-XXXXX-YY', 'auto');
    ga('set', 'checkProtocolTask', function(){}); 
    ga('send', 'pageview', '/options.html');
    

    There are 3 points I’d particularly like to highlight:

    • Specify “https” at the start of the script address to match with the listing in the manifest.json file
    • Override checkProtocolTask with an empty function
    • Send a virtual pageview by specifying the path – /options.html – otherwise Google Analytics will reject a URL in the format chrome-extension://gdocgfhmbfbbbmhnhmmejncjdcbjkhfc/options.html

提交回复
热议问题