@wxt-dev/analytics 0.4.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,15 +10,19 @@ Report analytics events from your web extension extension.
10
10
  ## Install With WXT
11
11
 
12
12
  1. Install the NPM package:
13
+
13
14
  ```bash
14
15
  pnpm i @wxt-dev/analytics
15
16
  ```
17
+
16
18
  2. In your `wxt.config.ts`, add the WXT module:
19
+
17
20
  ```ts
18
21
  export default defineConfig({
19
22
  modules: ['@wxt-dev/analytics/module'],
20
23
  });
21
24
  ```
25
+
22
26
  3. In your `<srcDir>/app.config.ts`, add a provider:
23
27
 
24
28
  ```ts
@@ -49,9 +53,11 @@ Report analytics events from your web extension extension.
49
53
  ## Install Without WXT
50
54
 
51
55
  1. Install the NPM package:
56
+
52
57
  ```bash
53
58
  pnpm i @wxt-dev/analytics
54
59
  ```
60
+
55
61
  2. Create an `analytics` instance:
56
62
 
57
63
  ```ts
@@ -66,10 +72,12 @@ Report analytics events from your web extension extension.
66
72
  ```
67
73
 
68
74
  3. Import your analytics module in the background to initialize the message listener:
75
+
69
76
  ```ts
70
77
  // background.ts
71
78
  import './utils/analytics';
72
79
  ```
80
+
73
81
  4. Then use your `analytics` instance to report events:
74
82
 
75
83
  ```ts
package/dist/index.mjs CHANGED
@@ -1,8 +1,9 @@
1
1
  import { UAParser } from 'ua-parser-js';
2
+ import { browser } from '@wxt-dev/browser';
2
3
 
3
4
  const ANALYTICS_PORT = "@wxt-dev/analytics";
4
5
  function createAnalytics(config) {
5
- if (typeof chrome === "undefined" || !chrome?.runtime?.id)
6
+ if (!browser?.runtime?.id)
6
7
  throw Error(
7
8
  "Cannot use WXT analytics in contexts without access to the browser.runtime APIs"
8
9
  );
@@ -22,13 +23,13 @@ function createBackgroundAnalytics(config) {
22
23
  {}
23
24
  );
24
25
  const enabled = config?.enabled ?? defineStorageItem("local:wxt-analytics:enabled", false);
25
- const platformInfo = chrome.runtime.getPlatformInfo();
26
+ const platformInfo = browser.runtime.getPlatformInfo();
26
27
  const userAgent = UAParser();
27
28
  let userId = Promise.resolve(userIdStorage.getValue()).then(
28
29
  (id) => id ?? globalThis.crypto.randomUUID()
29
30
  );
30
31
  let userProperties = userPropertiesStorage.getValue();
31
- const manifest = chrome.runtime.getManifest();
32
+ const manifest = browser.runtime.getManifest();
32
33
  const getBackgroundMeta = () => ({
33
34
  timestamp: Date.now(),
34
35
  // Don't track sessions for the background, it can be running
@@ -68,8 +69,7 @@ function createBackgroundAnalytics(config) {
68
69
  userPropertiesStorage.setValue?.(newUserProperties)
69
70
  ]);
70
71
  const event = await getBaseEvent(meta);
71
- if (config?.debug)
72
- console.debug("[@wxt-dev/analytics] identify", event);
72
+ if (config?.debug) console.debug("[@wxt-dev/analytics] identify", event);
73
73
  if (await enabled.getValue()) {
74
74
  await Promise.allSettled(
75
75
  providers.map((provider) => provider.identify(event))
@@ -90,8 +90,7 @@ function createBackgroundAnalytics(config) {
90
90
  title: meta?.title ?? globalThis.document?.title
91
91
  }
92
92
  };
93
- if (config?.debug)
94
- console.debug("[@wxt-dev/analytics] page", event);
93
+ if (config?.debug) console.debug("[@wxt-dev/analytics] page", event);
95
94
  if (await enabled.getValue()) {
96
95
  await Promise.allSettled(
97
96
  providers.map((provider) => provider.page(event))
@@ -108,8 +107,7 @@ function createBackgroundAnalytics(config) {
108
107
  ...baseEvent,
109
108
  event: { name: eventName, properties: eventProperties }
110
109
  };
111
- if (config?.debug)
112
- console.debug("[@wxt-dev/analytics] track", event);
110
+ if (config?.debug) console.debug("[@wxt-dev/analytics] track", event);
113
111
  if (await enabled.getValue()) {
114
112
  await Promise.allSettled(
115
113
  providers.map((provider) => provider.track(event))
@@ -129,7 +127,7 @@ function createBackgroundAnalytics(config) {
129
127
  }
130
128
  };
131
129
  const providers = config?.providers?.map((provider) => provider(analytics, config)) ?? [];
132
- chrome.runtime.onConnect.addListener((port) => {
130
+ browser.runtime.onConnect.addListener((port) => {
133
131
  if (port.name === ANALYTICS_PORT) {
134
132
  port.onMessage.addListener(({ fn, args }) => {
135
133
  void analytics[fn]?.(...args);
@@ -139,7 +137,7 @@ function createBackgroundAnalytics(config) {
139
137
  return analytics;
140
138
  }
141
139
  function createFrontendAnalytics() {
142
- const port = chrome.runtime.connect({ name: ANALYTICS_PORT });
140
+ const port = browser.runtime.connect({ name: ANALYTICS_PORT });
143
141
  const sessionId = Date.now();
144
142
  const getFrontendMetadata = () => ({
145
143
  sessionId,
@@ -182,8 +180,8 @@ function createFrontendAnalytics() {
182
180
  }
183
181
  function defineStorageItem(key, defaultValue) {
184
182
  return {
185
- getValue: async () => (await chrome.storage.local.get(key))[key] ?? defaultValue,
186
- setValue: (newValue) => chrome.storage.local.set({ [key]: newValue })
183
+ getValue: async () => (await browser.storage.local.get(key))[key] ?? defaultValue,
184
+ setValue: (newValue) => browser.storage.local.set({ [key]: newValue })
187
185
  };
188
186
  }
189
187
  const INTERACTIVE_TAGS = /* @__PURE__ */ new Set([
package/dist/module.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as wxt from 'wxt';
2
2
  import { AnalyticsConfig } from './types.mjs';
3
3
 
4
- declare module 'wxt/sandbox' {
4
+ declare module 'wxt/utils/define-app-config' {
5
5
  interface WxtAppConfig {
6
6
  analytics: AnalyticsConfig;
7
7
  }
package/dist/module.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as wxt from 'wxt';
2
2
  import { AnalyticsConfig } from './types.js';
3
3
 
4
- declare module 'wxt/sandbox' {
4
+ declare module 'wxt/utils/define-app-config' {
5
5
  interface WxtAppConfig {
6
6
  analytics: AnalyticsConfig;
7
7
  }
package/dist/module.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import 'wxt';
2
- import 'wxt/sandbox';
2
+ import 'wxt/utils/define-app-config';
3
3
  import { defineWxtModule, addAlias, addWxtPlugin, addViteConfig } from 'wxt/modules';
4
4
  import { resolve } from 'node:path';
5
5
 
@@ -12,14 +12,14 @@ const index = defineWxtModule({
12
12
  const clientModuleId = "@wxt-dev/analytics" ;
13
13
  const pluginModuleId = "@wxt-dev/analytics/background-plugin" ;
14
14
  wxt.hook("build:manifestGenerated", (_, manifest) => {
15
- manifest.permissions ?? (manifest.permissions = []);
15
+ manifest.permissions ??= [];
16
16
  if (!manifest.permissions.includes("storage")) {
17
17
  manifest.permissions.push("storage");
18
18
  }
19
19
  });
20
20
  const wxtAnalyticsCode = [
21
21
  `import { createAnalytics } from '${clientModuleId }';`,
22
- `import { useAppConfig } from 'wxt/client';`,
22
+ `import { useAppConfig } from '#imports';`,
23
23
  ``,
24
24
  `export const analytics = createAnalytics(useAppConfig().analytics);`,
25
25
  ``
@@ -6,4 +6,5 @@ interface GoogleAnalytics4ProviderOptions {
6
6
  }
7
7
  declare const googleAnalytics4: (options: GoogleAnalytics4ProviderOptions) => AnalyticsProvider;
8
8
 
9
- export { type GoogleAnalytics4ProviderOptions, googleAnalytics4 };
9
+ export { googleAnalytics4 };
10
+ export type { GoogleAnalytics4ProviderOptions };
@@ -6,4 +6,5 @@ interface GoogleAnalytics4ProviderOptions {
6
6
  }
7
7
  declare const googleAnalytics4: (options: GoogleAnalytics4ProviderOptions) => AnalyticsProvider;
8
8
 
9
- export { type GoogleAnalytics4ProviderOptions, googleAnalytics4 };
9
+ export { googleAnalytics4 };
10
+ export type { GoogleAnalytics4ProviderOptions };
@@ -1,5 +1,6 @@
1
1
  import { defineAnalyticsProvider } from '../index.mjs';
2
2
  import 'ua-parser-js';
3
+ import '@wxt-dev/browser';
3
4
 
4
5
  const DEFAULT_ENGAGEMENT_TIME_IN_MSEC = 100;
5
6
  const googleAnalytics4 = defineAnalyticsProvider(
@@ -33,6 +34,7 @@ const googleAnalytics4 = defineAnalyticsProvider(
33
34
  ad_personalization: "DENIED"
34
35
  },
35
36
  user_properties: mappedUserProperties,
37
+ user_agent: navigator.userAgent,
36
38
  events: [
37
39
  {
38
40
  name: eventName,
@@ -7,4 +7,5 @@ interface UmamiProviderOptions {
7
7
  }
8
8
  declare const umami: (options: UmamiProviderOptions) => AnalyticsProvider;
9
9
 
10
- export { type UmamiProviderOptions, umami };
10
+ export { umami };
11
+ export type { UmamiProviderOptions };
@@ -7,4 +7,5 @@ interface UmamiProviderOptions {
7
7
  }
8
8
  declare const umami: (options: UmamiProviderOptions) => AnalyticsProvider;
9
9
 
10
- export { type UmamiProviderOptions, umami };
10
+ export { umami };
11
+ export type { UmamiProviderOptions };
@@ -1,5 +1,6 @@
1
1
  import { defineAnalyticsProvider } from '../index.mjs';
2
2
  import 'ua-parser-js';
3
+ import '@wxt-dev/browser';
3
4
 
4
5
  const umami = defineAnalyticsProvider(
5
6
  (_, config, options) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxt-dev/analytics",
3
- "version": "0.4.1",
3
+ "version": "0.5.1",
4
4
  "description": "Add analytics to your web extension",
5
5
  "repository": {
6
6
  "type": "git",
@@ -40,19 +40,19 @@
40
40
  "dist"
41
41
  ],
42
42
  "peerDependencies": {
43
- "wxt": ">=0.19.23"
43
+ "wxt": ">=0.20.0"
44
44
  },
45
45
  "devDependencies": {
46
- "@aklinker1/check": "^1.4.5",
47
- "@types/chrome": "^0.0.268",
46
+ "@aklinker1/check": "^2.1.0",
48
47
  "@types/ua-parser-js": "^0.7.39",
49
- "publint": "^0.2.12",
50
- "typescript": "^5.6.3",
51
- "unbuild": "^2.0.0",
52
- "wxt": "0.19.23"
48
+ "publint": "^0.3.12",
49
+ "typescript": "^5.9.2",
50
+ "unbuild": "^3.6.1",
51
+ "wxt": "0.20.8"
53
52
  },
54
53
  "dependencies": {
55
- "ua-parser-js": "^1.0.38"
54
+ "ua-parser-js": "^1.0.40",
55
+ "@wxt-dev/browser": "^0.1.4"
56
56
  },
57
57
  "scripts": {
58
58
  "dev": "buildc --deps-only -- wxt",