Request a Demo Start Trial
JavaScript Analytics
Download JavaScript Analytics SDK
version 2.2.0

Electron Apps

This document covers integrating the DeskMetrics analytics SDK into an Electron App.

Get the Library

Install the DeskMetrics JavaScript SDK using npm:

npm install deskmetrics

Integrate into the Main Process

We’ll integrate DeskMetrics into the main process. This will allow us to capture events that occur in the main process (EG: install) as well as handle one or more render processes.

const deskmetrics = require('deskmetrics')
const pkg = require('../package.json')

deskmetrics.start({ appId: '<-- YOUR APP ID -->' }).then(function() {

    // Set properties
    deskmetrics.setProperty('version', pkg.version)
})

Renderer Processes

To send events from a renderer processes you could do something like this:

ipcRenderer.send('analytics', 'my_event', { event: 'body' })

And add this to your main process:

ipcMain.on('analytics', (event, body) => {
    deskmetrics.send(event, body)
})

Squirrel Installer

If you’re using Squirrel, DeskMetrics can easily capture install, uninstall and update events. Additional information on Squirrel is available from the electron-winstaller project.

/* ****************************************************************************/
// DeskMetrics
/* ****************************************************************************/

const deskmetrics = require('deskmetrics')

const path = require('path');
const appFolder = path.resolve(process.execPath, '..');
const rootAtomFolder = path.resolve(appFolder, '..');

// Specify propertyFileName - the installer, uninstaller and app run in different working dirs
const propsFile = path.resolve(path.join(rootAtomFolder, 'properties'));

deskmetrics.start({ appId: '<-- YOUR APP ID -->', propertyFileName: propsFile }).then(function() {

  // Set properties
  deskmetrics.setProperty('version', pkg.version)
})

/* ****************************************************************************/
// Squirrel
/* ****************************************************************************/

if (handleSquirrelEvent()) {
  // squirrel event handled and app will exit in 1000ms, so don't do anything else
  return;
}

function handleSquirrelEvent() {
  if (process.argv.length === 1) {
    return false;
  }

  const ChildProcess = require('child_process');

  const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
  const exeName = path.basename(process.execPath);

  const spawn = function(command, args) {
    let spawnedProcess, error;

    try {
      spawnedProcess = ChildProcess.spawn(command, args, {detached: true});
    } catch (error) {}

    return spawnedProcess;
  };

  const spawnUpdate = function(args) {
    return spawn(updateDotExe, args);
  };

  const squirrelEvent = process.argv[1];
  switch (squirrelEvent) {
    case '--squirrel-install':
    case '--squirrel-updated':
      // Optionally do things such as:
      // - Add your .exe to the PATH
      // - Write to the registry for things like file associations and
      //   explorer context menus


      // Install desktop and start menu shortcuts
      spawnUpdate(['--createShortcut', exeName]);

      // DeskMetrics
      deskmetrics.send(squirrelEvent === '--squirrel-install' ? 'install' : 'update', {}).then(function() {
        setTimeout(app.quit, 1000);
      })

      return true;

    case '--squirrel-uninstall':
      // Undo anything you did in the --squirrel-install and
      // --squirrel-updated handlers

      // Remove desktop and start menu shortcuts
      spawnUpdate(['--removeShortcut', exeName]);

      // DeskMetrics
      deskmetrics.send('uninstall', {}).then(function() {
        setTimeout(app.quit, 1000);
      })

      return true;

    case '--squirrel-obsolete':
      // This is called on the outgoing version of your app before
      // we update to the new version - it's the opposite of
      // --squirrel-updated

      app.quit();
      return true;
  }
};