Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if my google analytics is working on a chrome extension?

I'm trying to track a chrome extension on Google Analytics.

It looks like it's configured correctly but I can't see the data on the dashboard. Not sure if it's a delay, if it's being ignored because I'm running the extension on development mode or if I need to configure something else.

I'm using the following code:

const gaScript = document.createElement('script');
    gaScript.type = 'text/javascript';
    gaScript.async = true;
    gaScript.src = 'https://ssl.google-analytics.com/analytics.js';
    gaScript.onload = function() {
      ga.l = +new Date;
      ga('create', 'MY_CODE', 'auto');
      ga('set', 'checkProtocolTask', null); // Disables file protocol checking.
      ga('send', 'pageview');
    }
    const s = document.getElementsByTagName('script')[0];
    s.parentNode?.insertBefore(gaScript, s);

When I check the background console, on the tab "network" I can see:

  • a request for "analytics.js" and I can see the file is being loaded correctly
  • a request for a URL "collect?v=..." which seems to indicate the extension is sending the data correctly to google servers. When I inspect this URL I can see it's sending MY_CODE as a parameter for the tracking id

But I still can't see ana logged data on the analytics dashboard. Any idea why is it happening?

Update:

Thanks to Michele I was able to understand the problem. Google created a new form of tracking: Global Analytics and the code is different from the standard Universal Analytics.

Whoever creates an analytics project today will get the Global Analytics by default. I studied it and I think the standard works better for extensions for now. I would wait Google update their documentation to include this case to start using it

like image 947
Felipe César Avatar asked Nov 07 '22 04:11

Felipe César


1 Answers

Why not use explicit 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', 'UA-XXXXXXX-1', 'auto');
ga('set', 'checkProtocolTask', function(){});
ga('send', 'pageview', '/chrome-extension/popup.html');

I have tried it in my extension and it works fine.

Here is my complete working sample code: https://www.michelepisani.it/articoli/creare-un-estensione-di-chrome-e-tracciarla-con-google-analytics/

like image 57
Michele Pisani Avatar answered Nov 12 '22 14:11

Michele Pisani