Skip to content

Instantly share code, notes, and snippets.

@anaisbetts
Created January 7, 2015 20:47
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save anaisbetts/2d2de55d137a1cf9d1ac to your computer and use it in GitHub Desktop.
Save anaisbetts/2d2de55d137a1cf9d1ac to your computer and use it in GitHub Desktop.
Google Analytics in Atom Shell
// Pretend that cookies work
(function (document) {
var cookies = {};
document.__defineGetter__('cookie', function () {
var output = [];
for (var cookieName in cookies) {
output.push(cookieName + "=" + cookies[cookieName]);
}
return output.join(";");
});
document.__defineSetter__('cookie', function (s) {
var indexOfSeparator = s.indexOf("=");
var key = s.substr(0, indexOfSeparator);
var value = s.substring(indexOfSeparator + 1);
cookies[key] = value;
return key + "=" + value;
});
document.clearCookies = function () {
cookies = {};
};
// Pretend that we're hosted on an Internet Website
document.__defineGetter__('location', function() {
return {
href: 'http://atom-shell.local/',
protocol: 'http:',
host: 'atom-shell.local',
port: '',
pathname: '/',
search: '',
hash: '',
username: '',
password: '',
origin: 'http://atom-shell.local'
};
});
// Nobody sets location
document.__defineSetter__('location', function() {})
})(document);
(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', '****** YOUR TOKEN HERE*****', 'auto');
ga('send', 'pageview');
@dansomething
Copy link

Interesting solution. Thanks for posting this. Is the cookie replacement required? Also, the hostname property is missing. I recommend adding a wrapper for location.reload() too or livereload won't work among other things.

// Pretend that we're hosted on an Internet Website
originalLocation = document.location;
document.__defineGetter__('location', function() {
  return {
     ...
     reload: function() {
       return originalLocation.reload.apply(originalLocation, arguments);
    }
  };
});

@yoshuawuyts
Copy link

The cookie approach is formalized as a module here: https://github.com/hstove/electron-cookies

@yoshuawuyts
Copy link

So far I haven't been able to get cookies working in electron, I was wondering if you did.

See Also

@eanticev
Copy link

There's a different approach to this that might work slightly better for you

// the window.isRunningAsElectron is something you have to configure
if (window.isRunningAsElectron) {
    // grab a uuid to identify the session
    var uuid = window.erequire('node-uuid');
    // set the uuid to local storage
    if (!localStorage["gaClientId"])
        localStorage.setItem("gaClientId",uuid.v4())
    // set the cookie storage for google analytics to none, and provide your own client id
    var clientId = localStorage["gaClientId"];
    ga('create', "<ga-id>",{
        'storage': 'none',
        'clientId': clientId
    });
    // finally, disable the protocol check to allow file://
    ga('set', 'checkProtocolTask', function(){}); // Disable file protocol checking.
} else {
    ga('create', "<ga-id>", 'auto');
}
ga('send', 'pageview');

@manojdcoder
Copy link

Does this solution work with Electron 0.30.4? Because I'm still getting the actual location (file://...) even after define getter.

@stilliard
Copy link

I'm also getting file:// in the location object, any ideas? :)

@daaru00
Copy link

daaru00 commented Dec 3, 2016

I created a library for this purpose nwjs-analytics, if you would like to contribute

@ThibaultJanBeyer
Copy link

I’m using this together with couchdb. When I do a call to _session and login I get a session cookie back. This seems to be saved since all recurring request with 'withCredentials' : true headers have the cookie. However, why/how can I access the cookie directly in the debugger? document.cookie returns an empty string: ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment