Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Google Analytics to a Chrome Extension

In order to add google analytics to a chrome extension the official docs provide the following snippet:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_trackPageview']);

(function() {   var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;   ga.src = 'https://ssl.google-analytics.com/ga.js';   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();

But google also recommends to migrate from ga.js to analytics.js.

ga.js is a legacy library. If you are starting a new implementation, we recommend you use the latest version of this library, analytics.js. For existing implementations, learn how to migrate from ga.js to analytics.js.

After following carefully the migration guide and upgrading the content security policy with the new script ( from https://ssl.google-analytics.com/ga.js to https://www.google-analytics.com/analytics.js ), it simply didn't work, without showing any error message.

Any suggestion welcome,

like image 917
locropulenton Avatar asked Feb 01 '18 10:02

locropulenton


People also ask

Can you use Google Analytics in Chrome extension?

Analytics users can download it from the Chrome web store. After you install the extension for your Chrome browser, you can load a page that you are tracking with Analytics and see the following information: Metrics: Pageviews, Unique Pageviews, Avg. Time on Page, Bounce Rate, % Exit.

Which Chrome extension has Google Analytics?

Google Tag Assistant Chrome Extension Once you enable this extension, it tells you which codes have fired on your page, including Google Analytics, Adwords Conversion Tracking, Google Tag Manager, and more.

How do I find Google Analytics in Chrome?

You can sign in to your Analytics account from http://www.google.com/analytics. Click Sign in (at top right), and select Analytics. If you are already signed in to Google (e.g. you are signed in to your Gmail account), you'll be taken directly to the Analytics user interface.


1 Answers

This will work great for those of you using react: https://github.com/lxieyang/chrome-extension-boilerplate-react

// manifest.json

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

// AnalyticsProvider.jsx

import { useEffect } from 'react';

export default function AnalyticsProvider() {
  const initAnalytics = () => {
    setTimeout(() => {
      const gaPlugin = _gaq || [];
      gaPlugin.push(['_setAccount', 'XX-XXXXXXXXXX-X']);
      gaPlugin.push(['_trackPageview']);
    }, 2000);
  };

  useEffect(() => {
    (function() {
      var ga = document.createElement('script');
      ga.type = 'text/javascript';
      ga.async = true;
      ga.src = 'https://ssl.google-analytics.com/ga.js';
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(ga, s);
      initAnalytics();
    })();
  }, []);

  return null;
}

// Popup.jsx (or any other page you choose)

import AnalyticsProvider from './AnalyticsProvider';

...

return (
    <div className={classes.root}>
    ...
      <AnalyticsProvider />
    ...
    </div>
  );
like image 115
Baba Ganoush Avatar answered Sep 26 '22 00:09

Baba Ganoush