How do I get the SDKs for an Electron game?

The Manalens JavaScript SDK for the renderer is open source (MIT) on GitHub. It is not on npm yet, so install it from GitHub. The Node SDK for the main process is not public yet: request it from the contact page, or send main-process events over HTTP with the Publishable key.

  1. Install the web SDK from GitHub, and request the Node SDK from the contact page.
  2. Serve the renderer under a host on your project domain with protocol.handle().
  3. Create the web SDK instance in the renderer with the app option.
  4. Send the renderer anonymous ID to the main process and start the Node SDK in app mode with it.
  5. Write a running flag at start, remove it on quit, and send app_unclean_exit when it is left over.
Which Manalens SDK runs in each Electron process.
ProcessSDKKeySends
RendererWeb SDK with the app optionPublishable keyPage views, game_open, sessions, level events, and errors
Main processNode SDK in app modePublishable keyapp_started, app_quit, app_unclean_exit, and updates
Your backend (optional)Node SDK in server modeSecret key, on the server onlyPurchases and other server events

Install command

npm install github:manalens/manalens-js

Why serve the renderer over https?

Manalens accepts a web event only when its page URL is an http or https URL on your project domain. A file:// page gets 403. Serve the local renderer files under a host on your project domain with protocol.handle(), such as game.example.com. The files stay in the app; no request goes to the network for them. Every other https request goes out as usual.

Main process

protocol.handle('https', request => {
  const url = new URL(request.url);
  if (url.hostname !== 'game.example.com') return net.fetch(request, { bypassCustomProtocolHandlers: true });
  // localFile() returns a path inside rendererDir, or undefined for any other path.
  const file = localFile(url.pathname);
  return file ? net.fetch(pathToFileURL(file).toString()) : new Response('Not found', { status: 404 });
});
window.loadURL('https://game.example.com/');

How do I start the web SDK in the renderer?

Create one instance with the app option. Set platform from the OS: windows, macos, or linux. Then send the renderer anonymous ID to the main process over IPC, through a small preload API. Keep contextIsolation and the sandbox on.

Renderer

import { createAnalytics } from '@manalens/web';

const analytics = createAnalytics({
  publishableKey: 'YOUR_PUBLISHABLE_KEY',
  endpoint: 'https://events.manalens.dev',
  app: { platform: 'windows', engine: 'other', storefront: 'steam', app_version: '1.0.0', build: '1' },
});
window.game.setAnonymousId(analytics.getAnonymousId());
analytics.track('level_start', { level: 1, attempt: 1 });

How do I start the Node SDK in the main process?

Use app mode with the same Publishable key and the anonymous ID from the renderer. The Node SDK refuses a Secret key in app mode. Anyone who has the app can read every file in it, so never put a Secret key or an API key in an Electron app.

Main process

import { Manalens } from '@manalens/node';

ipcMain.handle('game:anonymous-id', (_event, anonymousId) => {
  analytics = new Manalens('YOUR_PUBLISHABLE_KEY', {
    endpoint: 'https://events.manalens.dev',
    mode: 'app',
    anonymousId,
    app: { platform: 'windows', storefront: 'steam', app_version: app.getVersion(), build: '1' },
  });
  analytics.track({ event: 'app_started' });
});

How do I report unclean exits in Electron?

A renderer cannot tell a crash from a close, so the main process does it. At start, write a running flag file in the user data folder, with the session ID and the build. A normal quit removes it. If the flag is still there at the next start, send app_unclean_exit with previous_session_id and previous_build. On quit, send app_quit and call shutdown() with a timeout of 2 seconds before the app exits.

Main process

if (existsSync(flagPath)) {
  const previous = JSON.parse(readFileSync(flagPath, 'utf8'));
  analytics.track({ event: 'app_unclean_exit', properties: { previous_session_id: previous.session_id, previous_build: previous.build } });
}
writeFileSync(flagPath, JSON.stringify({ session_id: analytics.getSessionId(), build: '1' }));

app.on('before-quit', event => {
  event.preventDefault();
  analytics.track({ event: 'app_quit' });
  analytics.shutdown(2000).finally(() => { rmSync(flagPath, { force: true }); app.exit(0); });
});

How do I check that Electron events arrive?

Start the app and play a level. Check that the page view, app_started, and the level events appear in the project activity feed with the same anonymous ID. Quit the app and check for app_quit. Then end the process from the task manager, start the app again, and check for app_unclean_exit.

Add the endpoint origin to connect-src in the content security policy of the renderer. For a local test, use a project with the domain localhost and serve the renderer from game.localhost.

Frequently asked questions

Why can the renderer not load from file://?

Because Manalens accepts a web event only when its page URL is an http or https URL on your project domain, and a file:// page gets 403. Serve the local files under a host on your project domain with protocol.handle(). The files stay in the app, and no request goes to the network for them.

Which key goes in an Electron app?

Only the Publishable key, in both processes. Anyone who has the app can read every file in it, so never put a Secret key or an API key in it. The Node SDK refuses a Secret key in app mode, so a key that leaks into a build fails at start.

How do the two processes count as one player?

The web SDK in the renderer makes the anonymous ID. The renderer sends it to the main process over IPC, and the main process passes it to the Node SDK as anonymousId. Events from both processes then have the same anonymous ID and count as one player.

Is there a complete example?

Yes. The with-electron project in the Manalens examples repository on GitHub has the renderer, the preload API, the https handler, the main-process analytics, and the running flag for unclean exits. Its main-process module runs in plain Node.js, so you can test it without Electron.

Sources and implementation