@wxt-dev/analytics 0.2.6 → 0.2.8
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 +2 -1
- package/dist/client.d.mts +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/client.mjs +16 -34
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/providers/google-analytics-4.d.mts +1 -1
- package/dist/providers/google-analytics-4.d.ts +1 -1
- package/dist/providers/umami.d.mts +1 -1
- package/dist/providers/umami.d.ts +1 -1
- package/dist/shared/{analytics.079e9952.d.mts → analytics.2893a879.d.mts} +17 -14
- package/dist/shared/{analytics.079e9952.d.ts → analytics.2893a879.d.ts} +17 -14
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -40,11 +40,12 @@ export default defineAppConfig({
|
|
|
40
40
|
Then use the `analytics` import to report events:
|
|
41
41
|
|
|
42
42
|
```ts
|
|
43
|
-
import { analytics } from '
|
|
43
|
+
import { analytics } from '#analytics';
|
|
44
44
|
|
|
45
45
|
await analytics.track('some-event');
|
|
46
46
|
await analytics.page();
|
|
47
47
|
await analytics.identify('some-user-id');
|
|
48
|
+
analytics.autoTrack(document.body);
|
|
48
49
|
```
|
|
49
50
|
|
|
50
51
|
## Providers
|
package/dist/client.d.mts
CHANGED
package/dist/client.d.ts
CHANGED
package/dist/client.mjs
CHANGED
|
@@ -13,7 +13,7 @@ function createAnalytics(config) {
|
|
|
13
13
|
}
|
|
14
14
|
if (location.pathname === "/background.js")
|
|
15
15
|
return createBackgroundAnalytics(config);
|
|
16
|
-
return createFrontendAnalytics(
|
|
16
|
+
return createFrontendAnalytics();
|
|
17
17
|
}
|
|
18
18
|
function createBackgroundAnalytics(config) {
|
|
19
19
|
const userIdStorage = config?.userId ?? defineStorageItem("wxt-analytics:user-id");
|
|
@@ -29,7 +29,7 @@ function createBackgroundAnalytics(config) {
|
|
|
29
29
|
);
|
|
30
30
|
let userProperties = userPropertiesStorage.getValue();
|
|
31
31
|
const manifest = chrome.runtime.getManifest();
|
|
32
|
-
const
|
|
32
|
+
const getBackgroundMeta = () => ({
|
|
33
33
|
timestamp: Date.now(),
|
|
34
34
|
// Don't track sessions for the background, it can be running
|
|
35
35
|
// indefinitely, and will inflate session duration stats.
|
|
@@ -39,17 +39,11 @@ function createBackgroundAnalytics(config) {
|
|
|
39
39
|
screen: void 0,
|
|
40
40
|
url: location.href,
|
|
41
41
|
title: void 0
|
|
42
|
-
})
|
|
42
|
+
});
|
|
43
|
+
const getBaseEvent = async (meta) => {
|
|
43
44
|
const platform = await platformInfo;
|
|
44
45
|
return {
|
|
45
|
-
meta
|
|
46
|
-
sessionId: meta.sessionId,
|
|
47
|
-
timestamp: meta.timestamp,
|
|
48
|
-
screen: meta.screen,
|
|
49
|
-
referrer: meta.referrer,
|
|
50
|
-
language: meta.language,
|
|
51
|
-
url: meta.url
|
|
52
|
-
},
|
|
46
|
+
meta,
|
|
53
47
|
user: {
|
|
54
48
|
id: await userId,
|
|
55
49
|
properties: {
|
|
@@ -66,14 +60,14 @@ function createBackgroundAnalytics(config) {
|
|
|
66
60
|
};
|
|
67
61
|
};
|
|
68
62
|
const analytics = {
|
|
69
|
-
identify: async (newUserId, newUserProperties = {},
|
|
63
|
+
identify: async (newUserId, newUserProperties = {}, meta = getBackgroundMeta()) => {
|
|
70
64
|
userId = Promise.resolve(newUserId);
|
|
71
65
|
userProperties = Promise.resolve(newUserProperties);
|
|
72
66
|
await Promise.all([
|
|
73
67
|
userIdStorage.setValue?.(newUserId),
|
|
74
68
|
userPropertiesStorage.setValue?.(newUserProperties)
|
|
75
69
|
]);
|
|
76
|
-
const event = await getBaseEvent(
|
|
70
|
+
const event = await getBaseEvent(meta);
|
|
77
71
|
if (config?.debug)
|
|
78
72
|
console.debug("[analytics] identify", event);
|
|
79
73
|
if (await enabled.getValue()) {
|
|
@@ -86,14 +80,14 @@ function createBackgroundAnalytics(config) {
|
|
|
86
80
|
);
|
|
87
81
|
}
|
|
88
82
|
},
|
|
89
|
-
page: async (location2,
|
|
90
|
-
const baseEvent = await getBaseEvent(
|
|
83
|
+
page: async (location2, meta = getBackgroundMeta()) => {
|
|
84
|
+
const baseEvent = await getBaseEvent(meta);
|
|
91
85
|
const event = {
|
|
92
86
|
...baseEvent,
|
|
93
87
|
page: {
|
|
94
|
-
url:
|
|
88
|
+
url: meta?.url ?? globalThis.location?.href,
|
|
95
89
|
location: location2,
|
|
96
|
-
title:
|
|
90
|
+
title: meta?.title ?? globalThis.document?.title
|
|
97
91
|
}
|
|
98
92
|
};
|
|
99
93
|
if (config?.debug)
|
|
@@ -106,8 +100,8 @@ function createBackgroundAnalytics(config) {
|
|
|
106
100
|
console.debug("[analytics] Analytics disabled, page() not uploaded");
|
|
107
101
|
}
|
|
108
102
|
},
|
|
109
|
-
track: async (eventName, eventProperties,
|
|
110
|
-
const baseEvent = await getBaseEvent(
|
|
103
|
+
track: async (eventName, eventProperties, meta = getBackgroundMeta()) => {
|
|
104
|
+
const baseEvent = await getBaseEvent(meta);
|
|
111
105
|
const event = {
|
|
112
106
|
...baseEvent,
|
|
113
107
|
event: { name: eventName, properties: eventProperties }
|
|
@@ -140,10 +134,10 @@ function createBackgroundAnalytics(config) {
|
|
|
140
134
|
});
|
|
141
135
|
return analytics;
|
|
142
136
|
}
|
|
143
|
-
function createFrontendAnalytics(
|
|
137
|
+
function createFrontendAnalytics() {
|
|
144
138
|
const port = chrome.runtime.connect({ name: ANALYTICS_PORT });
|
|
145
139
|
const sessionId = Date.now();
|
|
146
|
-
const
|
|
140
|
+
const getFrontendMetadata = () => ({
|
|
147
141
|
sessionId,
|
|
148
142
|
timestamp: Date.now(),
|
|
149
143
|
language: navigator.language,
|
|
@@ -153,13 +147,7 @@ function createFrontendAnalytics(config) {
|
|
|
153
147
|
title: document.title || void 0
|
|
154
148
|
});
|
|
155
149
|
const methodForwarder = (fn) => (...args) => {
|
|
156
|
-
|
|
157
|
-
console.debug(
|
|
158
|
-
`[analytics] Sending ${fn} to background for upload`,
|
|
159
|
-
...args
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
port.postMessage({ fn, args: [...args, getMetadata()] });
|
|
150
|
+
port.postMessage({ fn, args: [...args, getFrontendMetadata()] });
|
|
163
151
|
};
|
|
164
152
|
const analytics = {
|
|
165
153
|
identify: methodForwarder("identify"),
|
|
@@ -167,14 +155,8 @@ function createFrontendAnalytics(config) {
|
|
|
167
155
|
track: methodForwarder("track"),
|
|
168
156
|
setEnabled: methodForwarder("setEnabled"),
|
|
169
157
|
autoTrack: (root) => {
|
|
170
|
-
if (config?.debug) {
|
|
171
|
-
console.debug("[analytics] autoTrack() called!");
|
|
172
|
-
}
|
|
173
158
|
const onClick = (event) => {
|
|
174
159
|
const element = event.target;
|
|
175
|
-
if (config?.debug) {
|
|
176
|
-
console.debug("[analytics] autoTrack() element clicked", element);
|
|
177
|
-
}
|
|
178
160
|
if (!element || !INTERACTIVE_TAGS.has(element.tagName) && !INTERACTIVE_ROLES.has(element.getAttribute("role")))
|
|
179
161
|
return;
|
|
180
162
|
void analytics.track("click", {
|
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -49,25 +49,28 @@ type AnalyticsProvider = (analytics: Analytics, config: AnalyticsConfig) => {
|
|
|
49
49
|
identify: (event: BaseAnalyticsEvent) => Promise<void>;
|
|
50
50
|
};
|
|
51
51
|
interface BaseAnalyticsEvent {
|
|
52
|
-
meta:
|
|
53
|
-
/** Identifier of the session the event was fired from */
|
|
54
|
-
sessionId: number | undefined;
|
|
55
|
-
/** `Date.now()` of when the event was reported */
|
|
56
|
-
timestamp: number;
|
|
57
|
-
/** `"1920x1080"` */
|
|
58
|
-
screen: string | undefined;
|
|
59
|
-
/** `document.referrer` */
|
|
60
|
-
referrer: string | undefined;
|
|
61
|
-
/** `navigator.language` */
|
|
62
|
-
language: string | undefined;
|
|
63
|
-
/** `location.href` */
|
|
64
|
-
url: string | undefined;
|
|
65
|
-
};
|
|
52
|
+
meta: AnalyticsEventMetadata;
|
|
66
53
|
user: {
|
|
67
54
|
id: string;
|
|
68
55
|
properties: Record<string, string | undefined>;
|
|
69
56
|
};
|
|
70
57
|
}
|
|
58
|
+
interface AnalyticsEventMetadata {
|
|
59
|
+
/** Identifier of the session the event was fired from */
|
|
60
|
+
sessionId: number | undefined;
|
|
61
|
+
/** `Date.now()` of when the event was reported */
|
|
62
|
+
timestamp: number;
|
|
63
|
+
/** `"1920x1080"` */
|
|
64
|
+
screen: string | undefined;
|
|
65
|
+
/** `document.referrer` */
|
|
66
|
+
referrer: string | undefined;
|
|
67
|
+
/** `navigator.language` */
|
|
68
|
+
language: string | undefined;
|
|
69
|
+
/** `location.href` */
|
|
70
|
+
url: string | undefined;
|
|
71
|
+
/** `document.title` */
|
|
72
|
+
title: string | undefined;
|
|
73
|
+
}
|
|
71
74
|
interface AnalyticsPageInfo {
|
|
72
75
|
url: string;
|
|
73
76
|
title: string | undefined;
|
|
@@ -49,25 +49,28 @@ type AnalyticsProvider = (analytics: Analytics, config: AnalyticsConfig) => {
|
|
|
49
49
|
identify: (event: BaseAnalyticsEvent) => Promise<void>;
|
|
50
50
|
};
|
|
51
51
|
interface BaseAnalyticsEvent {
|
|
52
|
-
meta:
|
|
53
|
-
/** Identifier of the session the event was fired from */
|
|
54
|
-
sessionId: number | undefined;
|
|
55
|
-
/** `Date.now()` of when the event was reported */
|
|
56
|
-
timestamp: number;
|
|
57
|
-
/** `"1920x1080"` */
|
|
58
|
-
screen: string | undefined;
|
|
59
|
-
/** `document.referrer` */
|
|
60
|
-
referrer: string | undefined;
|
|
61
|
-
/** `navigator.language` */
|
|
62
|
-
language: string | undefined;
|
|
63
|
-
/** `location.href` */
|
|
64
|
-
url: string | undefined;
|
|
65
|
-
};
|
|
52
|
+
meta: AnalyticsEventMetadata;
|
|
66
53
|
user: {
|
|
67
54
|
id: string;
|
|
68
55
|
properties: Record<string, string | undefined>;
|
|
69
56
|
};
|
|
70
57
|
}
|
|
58
|
+
interface AnalyticsEventMetadata {
|
|
59
|
+
/** Identifier of the session the event was fired from */
|
|
60
|
+
sessionId: number | undefined;
|
|
61
|
+
/** `Date.now()` of when the event was reported */
|
|
62
|
+
timestamp: number;
|
|
63
|
+
/** `"1920x1080"` */
|
|
64
|
+
screen: string | undefined;
|
|
65
|
+
/** `document.referrer` */
|
|
66
|
+
referrer: string | undefined;
|
|
67
|
+
/** `navigator.language` */
|
|
68
|
+
language: string | undefined;
|
|
69
|
+
/** `location.href` */
|
|
70
|
+
url: string | undefined;
|
|
71
|
+
/** `document.title` */
|
|
72
|
+
title: string | undefined;
|
|
73
|
+
}
|
|
71
74
|
interface AnalyticsPageInfo {
|
|
72
75
|
url: string;
|
|
73
76
|
title: string | undefined;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxt-dev/analytics",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
4
|
"description": "Add analytics to your web extension",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
|
-
"url": "https://github.com/wxt-dev/wxt.git",
|
|
7
|
+
"url": "git+https://github.com/wxt-dev/wxt.git",
|
|
8
8
|
"directory": "packages/analytics"
|
|
9
9
|
},
|
|
10
10
|
"license": "MIT",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"wxt": ">=0.
|
|
36
|
+
"wxt": ">=0.19.9"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@aklinker1/check": "^1.3.1",
|