Installing the Nucleus Electron library

Configuring Nucleus support is easy since all the required APIs are published as a single Node.js library. Follow these steps to install the library:

  1. In the root folder of your project, run the following command to install the electron-nucleus library from NPM:
npm i electron-nucleus
  1. To integrate the library, you need to have the tracking ID number that you retrieved from the Nucleus website earlier. All you need to do to get your tracking ID number is run the following code somewhere in the main.js file:
const Nucleus = require('electron-nucleus')('Your App ID', {
onlyMainProcess: true
});

You can also raise a custom tracking event, as follows:

Nucleus.track(<NAME>, <DATA>);

NAME is the name of your event and can be anything that gives you meaning when you're inspecting analytics. DATA is an optional JSON object that you can use to pass details about the event; this can be error details or tracking details, for example.

  1. Update the main.js file according to the following code:
const { app, BrowserWindow } = require('electron');

const Nucleus = require('electron-nucleus')('Your App ID', {
onlyMainProcess: true
});

function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
},
resizable: false
});

win.loadFile('index.html');

// Optional: report an event
Nucleus.track('APP_LAUNCHED');
}

app.on('ready', createWindow);
Don't forget to use your tracking ID instead of the one shown in this example. You can find your tracking ID value online at any time.

Note that, besides Nucleus library initialization, we can also raise an APP_LAUNCHED event within the createWindow function. This event is invoked every time the application starts and is a simple example of how we can use custom events in our applications. We are going to see how this works shortly.

  1. Run the following command to test your application:
npm start

Nothing extra should happen from a visual standpoint. You should still see the blank Electron window with the Electron Analytics title. This time, however, your application should send tracking data to the Nucleus server.

At this point, you have an Electron application that has the Nucleus library installed. Each time the application starts, the Nucleus library sends a notification to the server. We also send an extra event called APP_LAUNCHED so that we can view the default and custom events in action.

Leave the application running for now. In the next section, we are going to see what the analytics data looks like in real life.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.149.32