Install
One package for the main process, the preload and every page of an Electron app.
echo "@gotmode:registry=https://gotmode.com/sdk/npm/" >> .npmrc npm install @gotmode/metrics-electron
The .npmrc line sends every @gotmode package to Mode, so commit it with the project. pnpm, Yarn 1 and Bun read it too.
Mode serves its SDKs itself, not from a public package registry. A published version never changes, and every file and its SHA-256 is listed in SHA256SUMS.
| File | Version | SHA-256 |
|---|---|---|
gotmode-metrics-electron-0.1.0.tgz | 0.1.0 | 33c4499e0849aa28430c4cee50c75cb400fb1b774b2d07449502573155333607 |
gotmode-metrics-core-0.1.0.tgz | 0.1.0 | fac754c0bb498c57505735897073c09b2860044af54376c1a4611c095b9203c3 |
Start
The main process owns the queue and the person. Call init there before the app is ready.
import { app, autoUpdater } from 'electron';
import mode from '@gotmode/metrics-electron/main';
mode.init('YOUR_INGEST_KEY', {
api_host: 'https://m.example.com',
protocol: 'myapp',
updater: autoUpdater,
});Then expose the bridge in your preload script, which your bundler bundles with it.
import { exposeModeMetrics } from '@gotmode/metrics-electron/preload';
exposeModeMetrics();Pages then call window.modeMetrics.capture('exported_report', { rows: 120 }) and the rest, and every call reaches the main process, so the app has one person and one session across all its windows. protocol is your app's own link scheme, and updater takes Electron's updater or electron-updater's.
Events and people
Capture what matters in your app, and name the person once they sign in.
mode.capture('added_to_cart', { sku: 'tee-black-m', price: 24 });
mode.identify('user_42', { plan: 'pro' });
mode.group('company', 'acme', { seats: 25 });
mode.register({ app_theme: 'dark' });
mode.reset(); // on logoutA different user than the one stored starts a new person. Person properties set before identify wait for it. An empty id, one over 200 bytes, or a placeholder such as null or guest is never sent. alias, set_person_properties, unset_person_properties, register_once, register_for_session, unregister, flags (flag, flag_enabled, flag_payload) and capture_exception work as on the web.
What is captured automatically
Every event below arrives with no code beyond init and the preload.
| Event | When |
|---|---|
$app_installed, $app_updated, $app_opened | The first start, a new version, every start |
$app_backgrounded | No window of the app has focus, and $app_opened when one has it again |
$app_quit | Quitting, once what is queued was sent |
$window_opened, $window_closed, $window_focus, $window_blur | Every window |
$screen | Every page a window loads and every route a single page app moves to |
$click, $rage_click | Clicks and taps on controls in every page with the preload |
$deep_link_opened | A link in your scheme opens the app |
$system_suspend, $system_resume, $system_lock, $system_unlock | The computer sleeps, wakes, locks and unlocks |
$update_available, $update_downloaded, $update_error | The updater, with the version |
$exception | Uncaught errors in the main process, crashed renderers and child processes, and every page's errors |
A page loaded from a file is named by the file alone, never the folder it sits in. Window titles are scrubbed of emails and long numbers. Uncaught exceptions are watched and never swallowed, so your app behaves as it did. The platform is macOS, Windows or Linux, and every request names your app and its version.
Consent and opt out
Start with consent pending, and nothing is written to the device or sent until the person answers.
mode.init('YOUR_INGEST_KEY', { api_host: 'https://m.example.com', consent: 'pending' });
mode.set_consent('granted');
mode.set_consent({ ad_storage: 'denied' });
mode.opt_out();Pending holds up to 200 events in memory. Denied drops events and deletes what was stored. Without ad storage consent, ad click ids and the Mode click id are left out. An opt out is remembered on the device and stops every send until mode.opt_in().
Delivery
Events wait on the device and are sent in batches, so nothing is lost offline and nothing is counted twice.
| Setting | Value |
|---|---|
| Batches | A second after an event, at once at 50 events, and when the app goes to the background |
| Queue | Up to 1,000 events stored on the device |
| Retries | From 1 second to 5 minutes, with the same event ids |
| Shutdown | mode.shutdown() sends what is queued and removes every listener |