Google Analytics in Atom Shell


#1

Hey all,

I managed to figure out an Evil Hack to get Google Analytics to work in the renderer process of Atom Shell, despite it being a file:// URL. Hope it’s useful for everyone:


#2

Looks cool, but doesn’t this prevent anyone else from using the same URL for GA? Can the shell.local href be changed (sorry haven’t done much atom shell yet)


#3

I don’t believe so because they don’t share the same client token. If you’re worried about it though, it doesn’t actually matter, you can set that URL to any valid (i.e. parses properly) HTTP / HTTPS URL. The important part is that it appears to GA as not a file:// URL.


#4

This is super helpful! I modified your script to support persistence of cookies in localStorage, so analytics providers don’t think every request is from a new user.


#5

Nice, that’s awesome


#6

Hello @hstove,

Can you please explain how to use Google analytics along with electron-cookies? I have included electron-cookies and added default GA tracking code and its reporting a new visitor every time I open the app.

Also, what should I set as default URL in analytics dashboard?

Thanks


#7

Slap me on the wrist for bumping a dead thread (if such a thing exists any more), but I thought I’d share my solution here, as I’ve been having trouble with this recently too (I almost dove into the Measurement Protocol). I found a few bits lying around in the analytics.js documentation indicating how to both swap out cookies for local storage and ignore file:

This is all entirely ripped from Google’s own tomes, the one change is specifically setting a.src to include https:

(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='https://www.google-analytics.com/analytics.js';m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

var GA_LOCAL_STORAGE_KEY = 'ga:clientId';

ga('create', _id, 'auto', {
   'storage': 'none',
   'clientId': localStorage.getItem(GA_LOCAL_STORAGE_KEY)
});

ga(function(tracker) {
   localStorage.setItem(GA_LOCAL_STORAGE_KEY, tracker.get('clientId'));
});

ga('set', 'checkProtocolTask', null);

#8

Hey, thanks for this! I’ve been searching for a working solution for this also.

Pardon the dumb question, but I’m guessing _id is a local var that represented your actual tracking id? (i.e. UA-12345678-6)


#9

Correct, I’m building an app across both Electron and Cordova, so I have an Angular module handling all my analytics stuff with a function passing in my tracking ID, so if I ever have to change it, I only have to change it in one place.


#10

In google analytics, ‘real time’ still gives me nothing, so I have added this line
ga(‘set’, ‘location’, ‘http://awebsite.com’);
now it works well :smile:


#11

Ah, yes. My code snippet sets it all up, but doesn’t do any page view or event calls.

My app is an Angular SPA, so I’m only ever running that snippet once, then doing page calls for each view separately.


#12

I created a library for this purpose nwjs-analytics, any help is greatly appreciated :smile:


#13

hi everyone, I’m one of the devs for Neutrino Metrics, an analytics solution for electron apps.
It is in beta right now. Would love to hear your thoughts on it!


#14

@meng.foo.bar Wonderful timing. I am trying to release a public beta of an app I’ve been working on and this seems worth a try. I’ll drop a line with any feedback or question I have.


#15

Hi all!
For those looking for an alternative to GA and the now defunct Neutrino, I built a simple analytics solution for Electron, Nucleus. Along with basic analytics it supports offline tracking and custom events. Feedback welcome :slight_smile: