Provides document.cookie support for Electron
Latest commit d038fce Jul 29, 2016 @ide ide [npm] Update deps
Failed to load latest commit information.
.gitignore Initial commit Mar 29, 2016
LICENSE Initial commit Mar 29, 2016 Update Jul 8, 2016
cookies.js Get "remote" from the electron module for Electron 1.0 May 19, 2016
package.json [npm] Update deps Jul 29, 2016


Provides document.cookie support for Electron. For use with Amplitude and Google Analytics in Electron.

The underlying cookie implementation is Salesforce's tough-cookie library and Exponent's WebStorageCookieStore for persistence.


npm install @exponent/electron-cookies --save


import ElectronCookies from '@exponent/electron-cookies';

// Add support for document.cookie, using the given origin (protocol, host, and
// port)
  origin: '',

// Remove support for document.cookie. Cookies are not cleared from the
// underlying storage.


When getting or setting a cookie, the browser needs to know the current URL of the page.

In Electron, HTML files are usually served from the local filesystem, which has no domain. electron-cookies provides you a way to specify the origin of the URL to use:

ElectronCookies.enable({ origin: '' });

This tells electron-cookies the origin of the URL to use when accessing cookies. The path of the URL is the relative path from the app's root to the path of the HTML file. So if the app is at /Users/you/Desktop/ and the HTML file is at /Users/you/Desktop/web/index.html, the synthesized URL will be

Alternatively, if you omit an origin, the full file: URL of the HTML file is used instead.

Google Analytics

To use Google Analytics in Electron, we use code like this:

  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

  (() => {
    'use strict';
    let ElectronCookies = require('@exponent/electron-cookies');
    ElectronCookies.enable({ origin: '' });

    ga('create', GOOGLE_ANALYTICS_ID, 'auto');
    ga('set', 'location', '');
    ga('set', 'checkProtocolTask', null);
    ga('send', 'pageview');

Notable changes to the standard code include:

  • The analytics script URL is prefixed with https: instead of inheriting the page's protocol
  • We enable electron-cookies
  • We set the location field in Google Analytics
  • The checkProtocolTask field is set to null to disable checking for a file: protocol