Documentation menuDesktop, mobile and extensions: Electron

Desktop, mobile and extensions

Add Loogika to Electron

You add Loogika to an Electron app by pasting the script tag into the head of the HTML file that the window loads. The renderer in Electron is a page in Chromium, so the script runs there as it does in a browser. Two conditions apply: the renderer's Content Security Policy must allow https://app.loogika.com, and the window must load over http or https.

Last updated .

What do you need before you start?

Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. It looks like this, with your own site ID in place of YOUR_SITE_ID:

<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>

Loogika has no npm package to bundle with your renderer code. The script tag is the install method, so the app fetches the script from app.loogika.com when a window opens.

How do you add Loogika to an Electron renderer?

Paste the tag in the renderer HTML, inside the head. The Content Security Policy (CSP) of the page must also allow the Loogika host. A CSP is the list of sources a page may load code from and send data to.

<meta
  http-equiv="Content-Security-Policy"
  content="default-src 'self'; script-src 'self' https://app.loogika.com; connect-src 'self' https://app.loogika.com"
/>
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>

The host appears twice for a reason. The script-src entry lets the page load the script, and the connect-src entry lets the script send its requests.

In some apps the main process sets the CSP through session.defaultSession.webRequest.onHeadersReceived. If your app works that way, that handler needs the same two sources.

Why must the Electron window load over http or https?

Chromium blocks the tracking request when the renderer runs on any other kind of origin. The Loogika tracking endpoints answer cross-origin checks (CORS) only for origins that start with http:// or https://.

How the window loadsOriginDoes tracking work?
win.loadURL() with an http or https addresshttp or httpsYes
win.loadFile()file://, sent as Origin: nullNo. The POST to /api/track never leaves the app, because Chromium stops it
A custom scheme registered with protocol.handle()The custom schemeNo, for the same reason

Load the renderer with win.loadURL() to get tracking. In development the address is your dev server, and in production it is a build that is hosted or served locally over http.

Does Loogika track navigation inside the app?

Yes. Loogika records a pageview when your router changes the address, both for routers that use the History API and for routers that use hash routes. The setting for that is SPA Navigation in Site Settings, which is on for a new site.

How do you check that Loogika is installed?

Start the app, open a few views and look at the Loogika dashboard. The pageviews show up within a few seconds.

If nothing arrives, open the developer tools of the window and type loogika in the console. An object with functions such as event and pageview means the script has loaded, and the Network tab then shows whether requests to /api/track go through.

What are common problems with Loogika in Electron?

SymptomCause and fix
A CSP violation appears in the consoleRead the message to find the directive that blocked the request, then add https://app.loogika.com to it
Requests to /api/track are blocked by CORSThe window was opened with win.loadFile() or a custom scheme. Load it over http or https with win.loadURL()
Development visits mix with real usageUse a separate site ID while you develop, as described on the localhost tracking page