Instrument your Electron applications for Real User Monitoring (RUM) with the Electron SDK.
- The Electron SDK monitors the main process. It uses
dd-trace-jsto collect traces and sends the data from the main process to Datadog. - The Browser SDK monitors renderer processes. It forwards its events to the main process, which uploads them to Datadog.
Prerequisites
Before you begin, you need:
- A Datadog account with RUM enabled
- Electron 39 or later, on Linux, macOS, or Windows
Create the application in Datadog
- In Datadog, navigate to Digital Experience > Add an Application.
- Select Electron as the application type.
- Copy the client token and application ID. You need them to initialize the SDK.
If you've purchased Error Tracking as a standalone product (without RUM), navigate to Error Tracking > Settings > Browser and Mobile > Add an Application instead.
Install the SDK
Add @datadog/electron-sdk to your application:
npm install @datadog/electron-sdk
yarn add @datadog/electron-sdk
Initialize the SDK in the main process
- Import
@datadog/electron-sdk/instrumentbefore importingelectron. This import instruments the Electron APIs the SDK needs. - Call
initonce per launch with your configuration, and await it before you create any browser window.
The example assumes an ES module main process, which supports top-level await. Otherwise, wrap the code in an async function.
// src/main.ts
import '@datadog/electron-sdk/instrument';
import { app, BrowserWindow } from 'electron';
import { init } from '@datadog/electron-sdk';
await init({
clientToken: '<CLIENT_TOKEN>',
applicationId: '<APPLICATION_ID>',
site: '',
service: '<SERVICE_NAME>',
env: '<ENV_NAME>', // optional
version: '1.0.0', // optional
allowedRendererHosts: ['*'], // restrict to specific hostnames in production
});
await app.whenReady();
const mainWindow = new BrowserWindow({ width: 1200, height: 800 });
Replace the placeholders with the values from your application, and set allowedRendererHosts to the hostnames of the renderer pages that are allowed to report through the main process. For the supported values, see Allowed renderer hosts.
Windows opened before init completes are not tracked until you reload them. These windows send no data, including the interactions that keep the session alive. If end users interact only with such windows, the session expires after 15 minutes of inactivity and no new session starts.
To wait for the end user's consent, don't defer init. Call it at startup with trackingConsent: 'pending' instead. See Set tracking consent.
Set up renderer processes
Renderer processes are monitored by the Browser SDK.
- Follow the Browser SDK setup in the pages loaded by your renderer processes.
- In the main process
initcall, list the renderer hosts that report through the main process inallowedRendererHosts. Renderers whose host is not listed do not report through the main process.
Sampling, Session Replay, and profiling options are set in the main process init call, and the main process manages sessions. The values set in the renderer for these options, including sessionPersistence, are ignored.
For details on allowedRendererHosts, see Advanced Configuration.
Configure your bundler
The SDK must load before Electron. Add the Datadog plugin for your bundler to the main-process bundler configuration so that this order is respected.
By default, the plugins do not copy the SDK or dd-trace-js into the build output. Your application packager must include @datadog/electron-sdk and its runtime dependencies, so keep @datadog/electron-sdk in your application's production dependencies.
Electron Forge
The Forge Vite and Webpack plugins package only the .vite or .webpack directory. Add an ignore function to packagerConfig in forge.config.ts that also keeps the root node_modules directory, so that Forge packages your production dependencies.
// forge.config.ts
packagerConfig: {
ignore: (file) => {
if (!file) return false;
return !/^[/\\](?:\.vite|node_modules)(?:[/\\]|$)/.test(file);
},
},
Then add the Datadog Vite plugin to the main-process Vite configuration:
// vite.main.config.ts
import { defineConfig } from 'vite';
import { datadogVitePlugin } from '@datadog/electron-sdk/vite-plugin';
export default defineConfig({
plugins: [datadogVitePlugin()],
});
// forge.config.ts
packagerConfig: {
ignore: (file) => {
if (!file) return false;
return !/^[/\\](?:\.webpack|node_modules)(?:[/\\]|$)/.test(file);
},
},
Then add the Datadog Webpack plugin to the main-process Webpack configuration:
// webpack.main.config.ts
import { DatadogWebpackPlugin } from '@datadog/electron-sdk/webpack-plugin';
export const mainConfig = {
plugins: [new DatadogWebpackPlugin()],
};
Use a package manager layout that Forge supports. For example, use pnpm with a hoisted node_modules directory, or Yarn 2 and later with the node-modules linker. See the Electron Forge documentation.
Other bundlers and packagers
By default, electron-builder and plain Electron Forge package production dependencies. Add the plugin for your bundler to the main-process bundler configuration:
Use the Vite plugin, including with electron-vite and standalone Vite:
// vite.config.ts
import { defineConfig } from 'vite';
import { datadogVitePlugin } from '@datadog/electron-sdk/vite-plugin';
export default defineConfig({
plugins: [datadogVitePlugin()],
});
// webpack.config.js
const { DatadogWebpackPlugin } = require('@datadog/electron-sdk/webpack-plugin');
module.exports = {
plugins: [new DatadogWebpackPlugin()],
};
// esbuild.config.ts
import { datadogEsbuildPlugin } from '@datadog/electron-sdk/esbuild-plugin';
await esbuild.build({
plugins: [datadogEsbuildPlugin()],
});
If your packager cannot include external dependencies, pass { copyRuntimeDependencies: true } to the plugin as a fallback. The plugin then copies the SDK, dd-trace-js, and their runtime dependencies into the build output on a best-effort basis.
Set tracking consent
To collect data only after the end user consents, for example to comply with GDPR or CCPA, initialize the SDK with the consent known at startup, then update it when the end user answers:
import { init, setTrackingConsent } from '@datadog/electron-sdk';
await init({
// ...
trackingConsent: 'pending',
});
// After the end user answers the consent prompt:
setTrackingConsent(accepted ? 'granted' : 'not-granted');
The SDK does not restore the consent at the next launch, so pass it at each launch. Set the consent in the main process. For the possible values and their effects, see Tracking consent.
Next steps
After you run your app, navigate to the RUM Explorer to see sessions from your application.
- Advanced Configuration: Configure sampling, event enrichment, and renderer options.
- Error Tracking: Track main process errors and crashes.
- Data Collected: Review the data collected by the SDK.
Further reading
Additional helpful documentation, links, and articles: