We have an extension written in Typescript working on Chrome, Firefox and Edge which we are adding Google Analytics to. We have done this by creating a simple file to load the google analytics:
import { environment } from "../environment";
(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().getTime(); 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', environment.googleAnalyticsTrackingCode, 'auto');
ga('set', 'checkProtocolTask', function () { }); // Removes failing protocol check. @see: http://stackoverflow.com/a/22152353/1958200
ga('require', 'displayfeatures');
We then use webpack to build this and reference the file in the popup.html we have. We have added a reference to the NPM package:
"@types/google.analytics": "^0.0.39"
From the popup js file we are then able to make a call to the ga function like so:
ga('send', 'pageview', url);
We have also modified the manifest to include:
"content_security_policy": "script-src 'self' https://www.google-analytics.com; object-src 'self'"
Everything works without issue in Chrome and we see the page views logged in Google Analaytics but in Firefox and Edge there are no requests to Google Analytics but there are no errors of any kind either, it hits the ga function but nothing happens.
Is there anything else we would have to do in Firefox and Edge to make this work?
The structure of our application is that the content script creates an iframe on the page and loads the popup.html as the src of that iframe. Is there a restriction in Firefox/Edge but not on Chrome that would affect Google Analytics on an extension structured in this way?
Comparing the network requests, Chrome on the left, Firefox on the right:
I have also modified so it logs a page view from the background. Again this is working fine in Chrome but does not work in Edge/Firefox but no errors.