Documentation menuDesktop, mobile and extensions: Chrome extension

Desktop, mobile and extensions

Add Loogika to a Chrome extension

The Loogika script tag cannot load in a Manifest V3 Chrome extension, because extension pages may only run code that ships inside the extension package. You track an extension by sending pageviews and events to the Loogika HTTP API with fetch. The extension needs your site ID and a host_permissions entry for https://app.loogika.com/*.

Last updated .

Why does the script tag not work in a Chrome extension?

Manifest V3 fixes the Content Security Policy for extension pages at script-src 'self' 'wasm-unsafe-eval'; object-src 'self', and an extension cannot relax it. A script tag that points at app.loogika.com is refused, and the popup console shows a "Refused to load the script" error.

Loogika has no npm package to bundle in place of the tag. The method that remains is a request from your own extension code to POST https://app.loogika.com/api/track, the endpoint the tracking script itself sends to.

What do you need before you start?

You only need your site ID. Open Site Settings in Loogika and look at the tag under the Tracking Script tab. The ID is the 12-character value after siteId=.

Do not put a Loogika API key in the extension. The endpoint accepts requests without a key, and a key in code that runs in the browser can be read by anyone.

How do you send data from an extension page to Loogika?

Add the host permission to the manifest, then call fetch in the script of every extension page that you track. Those pages are the popup, the options page and the side panel.

First grant the extension permission to call Loogika. Cross-origin checks (CORS) do not apply when an extension page sends a request to a host that host_permissions lists. The pattern must include the path wildcard at the end.

{
  "manifest_version": 3,
  "host_permissions": ["https://app.loogika.com/*"]
}

Then add two small functions to the page script, for example popup.js. Replace YOUR_SITE_ID with your own ID.

const LOOGIKA_TRACK_URL = "https://app.loogika.com/api/track";
const LOOGIKA_SITE_ID = "YOUR_SITE_ID";

function sendToLoogika(fields) {
  return fetch(LOOGIKA_TRACK_URL, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      site_id: LOOGIKA_SITE_ID,
      hostname: location.hostname,
      pathname: location.pathname,
      page_title: document.title,
      language: navigator.language,
      screenWidth: screen.width,
      screenHeight: screen.height,
      ...fields,
    }),
  }).catch(() => {});
}

function trackPageview() {
  return sendToLoogika({ type: "pageview" });
}

function trackEvent(name, properties = {}) {
  return sendToLoogika({
    type: "custom_event",
    event_name: name,
    properties: JSON.stringify(properties),
  });
}

// One pageview each time the popup opens
trackPageview();

Reload the unpacked extension at chrome://extensions after you change the manifest.

Which fields does the request use?

The request body is JSON, and only site_id and type are required for a pageview. Loogika rejects a body with a field it does not know with status 400, so send only fields from the table.

FieldValue in the exampleRule
site_idYour site IDRequired, sent as text
typepageview or custom_eventRequired
event_nameThe name you pass to trackEventRequired for custom_event, up to 256 characters
propertiesYour propertiesA JSON object written as text, up to 2,048 characters
hostnameThe extension IDOptional
pathnameThe page file, such as /popup.htmlOptional, up to 2,048 characters
page_titleThe title of the pageOptional, up to 512 characters
languageThe browser languageOptional
screenWidth, screenHeightThe screen size in pixelsOptional, whole numbers

Extension pages have addresses like chrome-extension://<id>/popup.html. The hostname in your reports is therefore the extension ID, and the path tells the popup and the options page apart. The sending events page lists every field and event type.

How do you track clicks in the popup?

Call trackEvent from a click handler in the page script. The data-loogika-event attribute does nothing in an extension, because the Loogika script that reads it is not loaded.

document.getElementById("save")?.addEventListener("click", () => {
  trackEvent("settings_saved", { theme: "dark" });
});

Should you track from a content script?

No. Content scripts run inside the websites your users visit. A tracker in a content script would file pages that belong to other people under your site ID.

How do you check that data reaches Loogika?

Open the popup, then inspect it and look at the Network tab. A stored event gets status 200 with the body {"success":true}.

Confirm the result in the Loogika dashboard as well. Loogika also answers 200 for traffic it classifies as a bot, and it keeps that traffic out of your reports. The bot detection page explains where such traffic is shown.

What are common problems with Loogika in a Chrome extension?

SymptomCause and fix
The popup console reports "Refused to load the script"A remote script tag is on the page. Remove it and use the fetch functions
CORS blocks the POST to /api/trackThe manifest has no host_permissions entry, or the entry does not match. Use the pattern https://app.loogika.com/*
The request is blocked by your own Content Security PolicyThe default policy for extension pages does not limit connect-src. If you define your own, add https://app.loogika.com to connect-src
Status 400 with "Invalid payload"A field is wrong or unknown. The details value in the response names it
Status 404 with "Site not found"The site_id does not exist. Compare it with the ID in Site Settings