How do you integrate Universal Analytics in to Chrome Extensions?

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

    Regarding new analytics.js (as opposite to old ga.js) this example works for me:

    function setupGoogleAnalytics() {
      if (!window.ga) {
        (function(){
          window.ga = function() {
            (window.ga.q = window.ga.q || []).push(arguments);
          }, window.ga.l = 1 * new Date();
          var tag = 'script';
          var a = document.createElement(tag);
          var m = document.getElementsByTagName(tag)[0];
          a.async = 1;
          a.src = 'https://www.google-analytics.com/analytics.js';
          m.parentNode.insertBefore(a, m);
        })();
        ga('create', 'UA-XXXXXXX-Y', 'auto');
        ga('set', 'checkProtocolTask', null);
      }
    }
    

    Please note that you need to add following content_security_policy snippet to the manifest.json:

    {
    ...
      "content_security_policy": "script-src 'self' https://www.google-analytics.com; object-src 'self'"
    ...
    }
    

提交回复
热议问题