3

I'm trying to send an event to Google Analytics from background.js (the background script) of my chrome extension.

I put this code in my background.js file

var _gaq = _gaq || [];
_gaq.push(['_setAccount', _AnalyticsCode]);
_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';
  ga.checkProtocolTask = null;
  var s = document.getElementsByTagName('script')[0];
  s.parentNode.insertBefore(ga, s);
})();

and I try to send this event:

 _gaq.push(['_trackEvent', 'event_send', 'event_label');

but Im not seeing the event on Analytics dashboard. I also added to my manifest.json file this line: "content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'", What do I need to do to make it work from the background?

Dkova
  • 1,087
  • 4
  • 16
  • 28

1 Answers1

6

This is the code I use. You do need to set the checkProtocolTask for an extension. Not sure what you are doing with _gaq, but your _gaq.push line is missing a closing bracket. Also, you are using a deprecated version of analytics. See here: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingOverview

    // Standard Google Universal Analytics code
    // noinspection OverlyComplexFunctionJS
    (function(i, s, o, g, r, a, m) {
        i['GoogleAnalyticsObject'] = r;
        // noinspection CommaExpressionJS
        i[r] = i[r] || function() {
                (i[r].q = i[r].q || []).push(arguments);
            }, i[r].l = 1 * new Date();
        // noinspection CommaExpressionJS
        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', TRACKING_ID, 'auto');
    // see: http://stackoverflow.com/a/22152353/1958200
    ga('set', 'checkProtocolTask', function() { });
    ga('set', 'appName', 'Photo Screensaver');
    ga('set', 'appId', 'photo-screen-saver');
    ga('set', 'appVersion', '<version here>');
    ga('require', 'displayfeatures');

Also, don't forget to add https://www.google-analytics.com to the"content_security_policy" in your manifest.

Michael Updike
  • 644
  • 1
  • 6
  • 13
  • are you seeing this on the event page on analytics? I'm not seeing it anywhere what are you doing to send event? `ga('send', 'event', 'event_name', 'show', value);`? – Dkova Jun 01 '17 at 17:48
  • @Dkova I log events, pages, and exceptions and they all show up. I use the fields object like this: ` event: function(event, label=null, action=null) { if (event) { const ev = {}; ev.hitType = 'event'; ev.eventLabel = label ? label : ev.eventLabel; ev.eventAction = action ? action : ev.eventAction; ga('send', ev); } },` but what you are doing looks OK to me. – Michael Updike Jun 01 '17 at 18:01
  • and it all from the background.js file? and not popup.js? – Dkova Jun 01 '17 at 18:07
  • Yes. from background.html and other .html pages. I don't use a popup.html. – Michael Updike Jun 01 '17 at 18:10
  • The code is here: if you want to review it: [photo-screen-saver](https://github.com/opus1269/photo-screen-saver) – Michael Updike Jun 01 '17 at 18:15
  • I think the value in your call is wrong. There is a label item before that. – Michael Updike Jun 01 '17 at 18:20
  • still not working, do you want me to send my code and you can check it? it would be great :) – Dkova Jun 02 '17 at 05:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145675/discussion-between-michael-updike-and-dkova). – Michael Updike Jun 02 '17 at 05:34