Google Analytics in Chrome Extension not working

老子叫甜甜 提交于 2021-01-28 10:29:55

问题


I have created an account on Google analytics, created a property, and a data stream for web. In my popup.js for the Chrome extension I have the following code:

// 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'); 

    ga('create', 'G-XXXXXXXXXX', 'auto'); 
    ga('set', 'checkProtocolTask', null); 
    ga('send', 'pageview', '/popup.html'); 


 function trackButtonClick(e) {
    ga('send', 'event', 'Button', 'Click', e.target.id);
  };

  var buttons = document.querySelectorAll('button');
  for (var i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener('click', trackButtonClick);
  }

I can observer in dev tools the request to https://www.google-analytics.com/collect. But in the google analytics console, I'm not showing any data. It's been several days since I have first used the tracking code. What am I doing wrong? Is it something with the new Google Analytics 4?


回答1:


You are using a Google Analytics 4 Property ID (G-XXXXXXXX) with analytics.js library (ga) for Universal Analytics. To use your snippet I suggest you create a Universal Analytics Property (as shown in following image) and use the relative identifier UA-XXXXXXX-X:



来源:https://stackoverflow.com/questions/64793124/google-analytics-in-chrome-extension-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!