Skip to content

Will That Analytics SDK Work in Your Chrome Extension? A Manifest V3 Checklist

6 min readModerok team

Six checks to run on any analytics SDK before you ship it in an MV3 extension: remote code, DOM globals, dynamic imports, listeners, queue, permissions.

An analytics SDK is safe to put in a Manifest V3 Chrome extension only if it ships all of its code in your bundle, never reads window, document, localStorage or XMLHttpRequest on the paths you call, loads no modules with dynamic import(), registers its chrome.* listeners synchronously at the top level, persists pending events outside memory, and asks nothing unusual of your manifest. All six are cheap to check against the SDK's compiled output, before you write a line of integration code.

TL;DR: Grep the package's shipped dist/ for http://, https://, import(, and DOM globals. Then load the extension, send one event, kill the service worker, and check that queued events survive. An SDK that fails the grep risks a Chrome Web Store rejection or throws on first call; one that fails the termination test loses events quietly, which is worse.

Why a page-oriented SDK is the wrong default

A page-oriented analytics SDK assumes a DOM, a long-lived JavaScript context, localStorage, and permission to pull a script tag from a CDN. An MV3 extension service worker offers none of that. Chrome's migration guide is blunt about the three that matter most: service workers "can't access the DOM or the window interface", they are "not backward compatible with XMLHttpRequest()", and the web platform's Storage interface "cannot be used in a service worker" (Migrate to a service worker).

The six checks every Manifest V3 analytics SDK has to pass

CheckThe SDK fails ifReference
1. Remote codeits compiled output fetches or injects JavaScript from a URLDeal with remote hosted code violations
2. DOM globalsa code path you call reads window, document, localStorage or XMLHttpRequestMigrate to a service worker
3. Dynamic importit loads modules with import() at runtimeService worker basics
4. Listener timingit registers chrome.* listeners after an awaitEvents in service workers
5. Queue durabilityits pending events live only in a global variableService worker lifecycle
6. Manifest costits quickstart asks for permissions beyond storageChrome extension analytics permissions

Check 1: does it ship all of its code?

Remotely hosted code (RHC) is, says Chrome's RHC guide, "anything that is executed by the browser that is loaded from someplace other than the extension's own files", and MV3 extensions must bundle all of it. Its advice for spotting RHC is a plain string search, with one detail: "make sure you are searching the compiled version of the code, since that is what is being evaluated by the store."

grep -rn "http://\|https://" node_modules/@scope/analytics-sdk/dist/

Hits in comments or source maps are noise. A hit inside a createElement("script") call, or in a fetch() whose result reaches eval or new Function, is a real violation. The same doc notes RHC often arrives through a dependency, with no exception for first-party libraries, and that a rejection carrying the Blue Argon notification ID means reviewers believe your extension contains some.

Check 2: does it touch the DOM?

grep -rhoE "\b(window|document|localStorage|XMLHttpRequest)\b" \
  node_modules/@scope/analytics-sdk/dist/*.js | sort | uniq -c | sort -rn

Then re-run as grep -rnE without the counting pipeline and read the hits in context, because the tally proves nothing on its own. A bundle also shipped for browsers often guards these behind a feature detect such as typeof document !== "undefined", and those branches are harmless in a service worker as long as the fallback does something sensible. What kills you is an unguarded read at module scope: the exception fires the moment your service worker starts and takes the whole background script with it. An XMLHttpRequest transport is a hard fail, since Chrome's migration guide says it "can't be called from a service worker, extension or otherwise" and the replacement is global fetch().

Check 3: does it load modules at runtime?

Service workers support the import statement (with "type": "module" in your manifest's background key) and importScripts(). Dynamic import() is explicitly "not supported". If an SDK code-splits part of its transport or feature set behind import(), this grep matters:

grep -rn "import(" node_modules/@scope/analytics-sdk/dist/*.js

A hit usually means the SDK expects your bundler to inline the chunk. If one survives into your dist/, the code path that needs it throws at runtime.

Check 4: does it register listeners synchronously?

This is the easiest check to skip, and the one that silently breaks install counting. Chrome's events guide is unambiguous: "Event handlers in service workers need to be declared in the global scope, meaning they should be at the top level of the script and not be nested inside functions." That is what makes them "registered synchronously on initial script execution".

An analytics SDK that measures installs therefore has to add its chrome.runtime.onInstalled listener before its first await. If it does the natural-looking thing instead, loading stored state and then subscribing, the listener is not in place when onInstalled is the event that woke the worker, and the install is never recorded. The Moderok SDK splits init() for exactly this reason: listener registration is synchronous, while loading storage, creating the analytics profile id and recovering pending events happen afterwards, with events tracked in between held as drafts.

Check 5: what happens to queued events when the worker dies?

Chrome normally terminates an extension service worker after 30 seconds of inactivity, when a single request takes longer than five minutes, or when a fetch() response takes more than 30 seconds to arrive; receiving an event or calling an extension API resets those timers. The lifecycle doc is explicit about the consequence: "Any global variables you set will be lost if the service worker shuts down." It also notes that the Web Storage API is unavailable here, leaving chrome.storage, IndexedDB and the Cache Storage API as the places pending events can actually live.

An SDK that holds its queue in memory and flushes on a timer is lossy by construction in an extension. Grep for the fix:

grep -rn "chrome.storage\|browser.storage\|indexedDB" node_modules/@scope/analytics-sdk/dist/*.js

Then prove it: a vendor claim is not a test. Chrome documents forcing termination from a test with a Target.closeTarget call against the worker's target (Test service worker termination with Puppeteer). Track a few events, terminate, wake the worker, confirm they still go out. Expect a window rather than a guarantee: any SDK that debounces its writes, Moderok included, can lose the newest event if you kill the worker in the moment between tracking and persisting.

Check 6: what does it cost your manifest?

The expected answer here is boring. Anonymous event collection needs somewhere to keep an identifier and a queue, so "permissions": ["storage"] is the normal cost, and that is what the setup instructions of a provider built for extensions will tell you to add. This check exists to catch the outlier: an SDK whose quickstart wants host_permissions, cookies or webRequest for a feature you are not buying. Permissions are user-visible and reviewers read them, so read the quickstart before you install it.

Verifying the integration you pick

The fastest end-to-end check is the service worker console: turn on the SDK's debug logging, fire one event by hand, and watch for the request. In Moderok that is debug: true on init(), which logs enqueue, flush and transport results behind a [moderok] prefix (see the debugging guide). An enqueue line with no transport result means a queue that is filling and not flushing.

If an SDK fails a check

Three realistic options. Skip the SDK and POST to the vendor's documented HTTP ingestion endpoint from the service worker with fetch(), which is what a measurement-protocol style integration amounts to. Move the SDK into an offscreen document, a real page with a DOM, accepting the extra lifecycle complexity. Or use an SDK written for this environment.

Moderok is in the third category: 5.7 kB gzipped at v1.0.2, zero runtime dependencies, no DOM globals in the source, no remotely hosted code, and a capped, debounced queue persisted to chrome.storage.local with retry backoff. Run the six checks above against it, then see what the dashboard reports.