@wxt-dev/analytics 0.5.0 → 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 +8 -0
- package/dist/index.mjs +8 -7
- package/dist/providers/google-analytics-4.d.mts +2 -1
- package/dist/providers/google-analytics-4.d.ts +2 -1
- package/dist/providers/google-analytics-4.mjs +2 -0
- package/dist/providers/umami.d.mts +2 -1
- package/dist/providers/umami.d.ts +2 -1
- package/dist/providers/umami.mjs +1 -0
- package/package.json +8 -8
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 (
|
|
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 =
|
|
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 =
|
|
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
|
|
@@ -126,7 +127,7 @@ function createBackgroundAnalytics(config) {
|
|
|
126
127
|
}
|
|
127
128
|
};
|
|
128
129
|
const providers = config?.providers?.map((provider) => provider(analytics, config)) ?? [];
|
|
129
|
-
|
|
130
|
+
browser.runtime.onConnect.addListener((port) => {
|
|
130
131
|
if (port.name === ANALYTICS_PORT) {
|
|
131
132
|
port.onMessage.addListener(({ fn, args }) => {
|
|
132
133
|
void analytics[fn]?.(...args);
|
|
@@ -136,7 +137,7 @@ function createBackgroundAnalytics(config) {
|
|
|
136
137
|
return analytics;
|
|
137
138
|
}
|
|
138
139
|
function createFrontendAnalytics() {
|
|
139
|
-
const port =
|
|
140
|
+
const port = browser.runtime.connect({ name: ANALYTICS_PORT });
|
|
140
141
|
const sessionId = Date.now();
|
|
141
142
|
const getFrontendMetadata = () => ({
|
|
142
143
|
sessionId,
|
|
@@ -179,8 +180,8 @@ function createFrontendAnalytics() {
|
|
|
179
180
|
}
|
|
180
181
|
function defineStorageItem(key, defaultValue) {
|
|
181
182
|
return {
|
|
182
|
-
getValue: async () => (await
|
|
183
|
-
setValue: (newValue) =>
|
|
183
|
+
getValue: async () => (await browser.storage.local.get(key))[key] ?? defaultValue,
|
|
184
|
+
setValue: (newValue) => browser.storage.local.set({ [key]: newValue })
|
|
184
185
|
};
|
|
185
186
|
}
|
|
186
187
|
const INTERACTIVE_TAGS = /* @__PURE__ */ new Set([
|
|
@@ -6,4 +6,5 @@ interface GoogleAnalytics4ProviderOptions {
|
|
|
6
6
|
}
|
|
7
7
|
declare const googleAnalytics4: (options: GoogleAnalytics4ProviderOptions) => AnalyticsProvider;
|
|
8
8
|
|
|
9
|
-
export {
|
|
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 {
|
|
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,
|
package/dist/providers/umami.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxt-dev/analytics",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Add analytics to your web extension",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -43,16 +43,16 @@
|
|
|
43
43
|
"wxt": ">=0.20.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@aklinker1/check": "^1.
|
|
47
|
-
"@types/chrome": "^0.0.280",
|
|
46
|
+
"@aklinker1/check": "^2.1.0",
|
|
48
47
|
"@types/ua-parser-js": "^0.7.39",
|
|
49
|
-
"publint": "^0.
|
|
50
|
-
"typescript": "^5.
|
|
51
|
-
"unbuild": "^3.
|
|
52
|
-
"wxt": "0.20.
|
|
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.40"
|
|
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",
|