aklinker1/webext-core

Analytics Package

Opened this issue · 0 comments

Collecting any kind of analytics is hard for web extensions. Google Analytics, the most popular analytics product, doesn't support the chrome extension runtime, neither does Firebase. Other large analytics software (Umami, Amplitude) also don't support chrome extensions, because they all rely on the window object, which doesn't exist in MV3 service workers.

That said, collecting analytics isn't impossible. You just end up writing custom implmentations that interact with the service's API directly, which is kinda a pain.

I'd like to support:

interface ExtensionAnalytics {
  init(context?: string): void;
  trackEvent(action: string, properties: Record<string, any>): void;
  trackPageView(path: string): void;
}

Very basic APIs. As for clients, I'd like to provide GA4 and Umami clients with the package to get started. All will just be HTTP clients.

Initializing analytics should look something like this:

// background.ts
const umamiClient = defineUmamiClient({ ... });
// const googleAnalyticsClient = defineGoogleAnalyticsClient({ ... });
// const allClients = defineMultipleClients(umamiClient, googleAnalyticsClient, ...);
// Or create your own client

const analytics = registerExtensionAnalytics({
  client: umamiClient,
  isEnabled: () => true,
});
analytics.init("background");
// popup.ts
const analytics = getExtensionAnalytics();
analytics.init("popup");