DEV Community

Cover image for PostHog Chrome extension: one user ID across contexts
ExtensionBooster
ExtensionBooster

Posted on Originally published at extenify.app

PostHog Chrome extension: one user ID across contexts

Originally published on Extenify.

Setting up a PostHog Chrome extension is not the same as adding PostHog to a website, and the differences are what get extensions rejected or quietly break analytics. This guide covers the four things that matter: avoiding remote code, sharing one user ID, wiring each extension context correctly, and not blowing up your event bill.

Why an extension is not a website

An extension runs in several isolated places, and each has its own lifetime, DOM access and storage:

  • Popup and options page: normal pages with a DOM, short-lived.
  • Service worker: no DOM and no localStorage, and it wakes and sleeps.
  • Content scripts: run inside someone else's page.

If each context initializes the SDK on its own, each one creates a separate anonymous user. One person becomes three or four in your dashboard.

Keep Manifest V3 happy: no remote code

Manifest V3 forbids loading executable code from a remote host. The default PostHog browser build lazy-loads some code from a CDN, which breaks that rule. Use the build made for this case (the "no-external" bundle) and turn off external dependency loading, so everything ships inside your package. The source post targets posthog-js 1.438, so check the current docs before copying settings.

Session replay needs extra care. The default recorder can trigger a store rejection, so either import the lighter recorder or skip replay entirely.

Share one distinct ID everywhere

Generate an ID once, keep it in chrome.storage.local, and pass it to every initialization:

async function getDistinctId() {
  const { distinctId } = await chrome.storage.local.get('distinctId');
  if (distinctId) return distinctId;
  const id = crypto.randomUUID();
  await chrome.storage.local.set({ distinctId: id });
  return id;
}
Enter fullscreen mode Exit fullscreen mode

Every context calls this before starting analytics, so they all report as the same user.

Initialize per context

Context Approach
Popup Standard init with the shared ID
Service worker Memory persistence, or skip the SDK and send events straight to the capture API
Content script Relay events to the service worker instead of calling PostHog directly

For the relay, have the service worker verify the message sender and accept only event names you expect. Otherwise any page-level code that reaches your listener could inject events.

Track installs, updates and uninstalls

The service worker is the right place for lifecycle events. chrome.runtime.onInstalled reports one of four reasons: install, update, chrome_update and shared_module_update. Send only the first two as your own install and update events. For uninstalls, set an uninstall URL; it is limited to 1,023 characters, so keep identifiers short.

Avoid the billing trap

Service workers restart often. If you call identify() on every wake-up, those calls can end up making up 70 to 90 percent of your events. Identify once, remember that you did in storage, and only call it again when the user actually changes.

Privacy and store review

Disclose what you collect in your listing and privacy policy, and give users an opt-out. On Firefox, consent for telemetry uses the optional technicalAndInteraction data collection category.

What PostHog cannot see

Product analytics only sees what happens inside your extension. Store-side numbers such as listing views, ratings and category rank live outside it, so you need a separate source for those.

Key takeaways

  • Use the no-external PostHog build; remote code breaks Manifest V3 rules.
  • Store one generated ID in chrome.storage.local and reuse it in every context.
  • Service workers need memory persistence or a direct capture API call.
  • Relay content script events through the service worker and validate them.
  • Do not call identify() on every wake-up.
  • Treat session replay as a review risk until you have checked the lighter recorder.

For the complete walkthrough with all code and the debugging checklist, read the PostHog Chrome extension Manifest V3 setup guide. If you also want the store-side numbers PostHog cannot see, take a look at Extenify's extension analytics.

Which context gave you the most trouble when adding analytics to your extension: the service worker or the content script?

Top comments (0)