@wxt-dev/analytics 0.2.7 → 0.4.0
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 +121 -36
- package/dist/background-plugin.d.mts +3 -0
- package/dist/background-plugin.d.ts +3 -0
- package/dist/background-plugin.mjs +6 -0
- package/dist/index.d.mts +10 -9
- package/dist/index.d.ts +10 -9
- package/dist/index.mjs +204 -32
- package/dist/module.d.mts +11 -0
- package/dist/module.d.ts +11 -0
- package/dist/module.mjs +53 -0
- package/dist/providers/google-analytics-4.d.mts +4 -4
- package/dist/providers/google-analytics-4.d.ts +4 -4
- package/dist/providers/google-analytics-4.mjs +57 -52
- package/dist/providers/umami.d.mts +3 -3
- package/dist/providers/umami.d.ts +3 -3
- package/dist/providers/umami.mjs +51 -41
- package/dist/{shared/analytics.2893a879.d.ts → types.d.mts} +2 -2
- package/dist/{shared/analytics.2893a879.d.mts → types.d.ts} +2 -2
- package/dist/types.mjs +1 -0
- package/package.json +17 -11
- package/dist/client.d.mts +0 -5
- package/dist/client.d.ts +0 -5
- package/dist/client.mjs +0 -201
package/README.md
CHANGED
|
@@ -1,89 +1,174 @@
|
|
|
1
1
|
# WXT Analytics
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Report analytics events from your web extension extension.
|
|
4
4
|
|
|
5
5
|
## Supported Analytics Providers
|
|
6
6
|
|
|
7
7
|
- [Google Analytics 4 (Measurement Protocol)](#google-analytics-4-measurement-protocol)
|
|
8
8
|
- [Umami](#umami)
|
|
9
9
|
|
|
10
|
-
##
|
|
10
|
+
## Install With WXT
|
|
11
|
+
|
|
12
|
+
1. Install the NPM package:
|
|
13
|
+
```bash
|
|
14
|
+
pnpm i @wxt-dev/analytics
|
|
15
|
+
```
|
|
16
|
+
2. In your `wxt.config.ts`, add the WXT module:
|
|
17
|
+
```ts
|
|
18
|
+
export default defineConfig({
|
|
19
|
+
modules: ['@wxt-dev/analytics/module'],
|
|
20
|
+
});
|
|
21
|
+
```
|
|
22
|
+
3. In your `<srcDir>/app.config.ts`, add a provider:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// <srcDir>/app.config.ts
|
|
26
|
+
import { umami } from '@wxt-dev/analytics/providers/umami';
|
|
27
|
+
|
|
28
|
+
export default defineAppConfig({
|
|
29
|
+
analytics: {
|
|
30
|
+
debug: true,
|
|
31
|
+
providers: [
|
|
32
|
+
// ...
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
4. Then use the `#analytics` module to report events:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { analytics } from '#analytics';
|
|
42
|
+
|
|
43
|
+
await analytics.track('some-event');
|
|
44
|
+
await analytics.page();
|
|
45
|
+
await analytics.identify('some-user-id');
|
|
46
|
+
analytics.autoTrack(document.body);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Install Without WXT
|
|
50
|
+
|
|
51
|
+
1. Install the NPM package:
|
|
52
|
+
```bash
|
|
53
|
+
pnpm i @wxt-dev/analytics
|
|
54
|
+
```
|
|
55
|
+
2. Create an `analytics` instance:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
// utils/analytics.ts
|
|
59
|
+
import { createAnalytics } from '@wxt-dev/analytics';
|
|
60
|
+
|
|
61
|
+
export const analytics = createAnalytics({
|
|
62
|
+
providers: [
|
|
63
|
+
// ...
|
|
64
|
+
],
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
3. Import your analytics module in the background to initialize the message listener:
|
|
69
|
+
```ts
|
|
70
|
+
// background.ts
|
|
71
|
+
import './utils/analytics';
|
|
72
|
+
```
|
|
73
|
+
4. Then use your `analytics` instance to report events:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { analytics } from './utils/analytics';
|
|
77
|
+
|
|
78
|
+
await analytics.track('some-event');
|
|
79
|
+
await analytics.page();
|
|
80
|
+
await analytics.identify('some-user-id');
|
|
81
|
+
analytics.autoTrack(document.body);
|
|
82
|
+
```
|
|
11
83
|
|
|
12
|
-
|
|
84
|
+
## Providers
|
|
13
85
|
|
|
14
|
-
|
|
15
|
-
pnpm i @wxt-dev/analytics
|
|
16
|
-
```
|
|
86
|
+
### Google Analytics 4 (Measurement Protocol)
|
|
17
87
|
|
|
18
|
-
|
|
88
|
+
The [Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/ga4) is an alternative to GTag for reporting events to Google Analytics for MV3 extensions.
|
|
19
89
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
90
|
+
> [Why use the Measurement Protocol instead of GTag?](https://developer.chrome.com/docs/extensions/how-to/integrate/google-analytics-4#measurement-protocol)
|
|
91
|
+
|
|
92
|
+
Follow [Google's documentation](https://developer.chrome.com/docs/extensions/how-to/integrate/google-analytics-4#setup-credentials) to obtain your credentials and put them in your `.env` file:
|
|
93
|
+
|
|
94
|
+
```dotenv
|
|
95
|
+
WXT_GA_API_SECRET='...'
|
|
24
96
|
```
|
|
25
97
|
|
|
26
|
-
|
|
98
|
+
Then add the `googleAnalytics4` provider to your `<srcDir>/app.config.ts` file:
|
|
27
99
|
|
|
28
100
|
```ts
|
|
29
|
-
|
|
101
|
+
import { googleAnalytics4 } from '@wxt-dev/analytics/providers/google-analytics-4';
|
|
102
|
+
|
|
30
103
|
export default defineAppConfig({
|
|
31
104
|
analytics: {
|
|
32
|
-
debug: true,
|
|
33
105
|
providers: [
|
|
34
|
-
|
|
106
|
+
googleAnalytics4({
|
|
107
|
+
apiSecret: import.meta.env.WXT_GA_API_SECRET,
|
|
108
|
+
measurementId: '...',
|
|
109
|
+
}),
|
|
35
110
|
],
|
|
36
111
|
},
|
|
37
112
|
});
|
|
38
113
|
```
|
|
39
114
|
|
|
40
|
-
|
|
115
|
+
### Umami
|
|
41
116
|
|
|
42
|
-
|
|
43
|
-
import { analytics } from '@wxt-dev/analytics';
|
|
117
|
+
[Umami](https://umami.is/) is a privacy-first, open source analytics platform.
|
|
44
118
|
|
|
45
|
-
|
|
46
|
-
await analytics.page();
|
|
47
|
-
await analytics.identify('some-user-id');
|
|
48
|
-
```
|
|
119
|
+
In Umami's dashboard, create a new website. The website's name and domain can be anything. Obviously, an extension doesn't have a domain, so make one up if you don't have one.
|
|
49
120
|
|
|
50
|
-
|
|
121
|
+
After the website has been created, save the website ID and domain to your `.env` file:
|
|
51
122
|
|
|
52
|
-
|
|
123
|
+
```dotenv
|
|
124
|
+
WXT_UMAMI_WEBSITE_ID='...'
|
|
125
|
+
WXT_UMAMI_DOMAIN='...'
|
|
126
|
+
```
|
|
53
127
|
|
|
54
|
-
|
|
128
|
+
Then add the `umami` provider to your `<srcDir>/app.config.ts` file:
|
|
55
129
|
|
|
56
130
|
```ts
|
|
57
|
-
import {
|
|
131
|
+
import { umami } from '@wxt-dev/analytics/providers/umami';
|
|
58
132
|
|
|
59
133
|
export default defineAppConfig({
|
|
60
134
|
analytics: {
|
|
61
135
|
providers: [
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
136
|
+
umami({
|
|
137
|
+
apiUrl: 'https://<your-umami-instance>/api',
|
|
138
|
+
websiteId: import.meta.env.WXT_UMAMI_WEBSITE_ID,
|
|
139
|
+
domain: import.meta.env.WXT_UMAMI_DOMAIN,
|
|
65
140
|
}),
|
|
66
141
|
],
|
|
67
142
|
},
|
|
68
143
|
});
|
|
69
144
|
```
|
|
70
145
|
|
|
71
|
-
|
|
146
|
+
### Custom Provider
|
|
72
147
|
|
|
73
|
-
|
|
148
|
+
If your analytics platform is not supported, you can provide an implementation of the `AnalyticsProvider` type in your `app.config.ts` instead:
|
|
74
149
|
|
|
75
150
|
```ts
|
|
76
|
-
import {
|
|
151
|
+
import { defineAnalyticsProvider } from '@wxt-dev/analytics/client';
|
|
152
|
+
|
|
153
|
+
interface CustomAnalyticsOptions {
|
|
154
|
+
// ...
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const customAnalytics = defineAnalyticsProvider<CustomAnalyticsOptions>(
|
|
158
|
+
(analytics, analyticsConfig, providerOptions) => {
|
|
159
|
+
// ...
|
|
160
|
+
},
|
|
161
|
+
);
|
|
77
162
|
|
|
78
163
|
export default defineAppConfig({
|
|
79
164
|
analytics: {
|
|
80
165
|
providers: [
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
websiteId: '...',
|
|
84
|
-
hostname: '...',
|
|
166
|
+
customAnalytics({
|
|
167
|
+
// ...
|
|
85
168
|
}),
|
|
86
169
|
],
|
|
87
170
|
},
|
|
88
171
|
});
|
|
89
172
|
```
|
|
173
|
+
|
|
174
|
+
Example `AnalyticsProvider` implementations can be found at [`./modules/analytics/providers`](https://github.com/wxt-dev/wxt/tree/main/packages/analytics/modules/analytics/providers).
|
package/dist/index.d.mts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { A as AnalyticsConfig } from './shared/analytics.2893a879.mjs';
|
|
1
|
+
import { AnalyticsConfig, Analytics, AnalyticsProvider } from './types.mjs';
|
|
3
2
|
|
|
4
|
-
declare
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
declare function createAnalytics(config?: AnalyticsConfig): Analytics;
|
|
4
|
+
declare function defineAnalyticsProvider<T = never>(definition: (
|
|
5
|
+
/** The analytics object. */
|
|
6
|
+
analytics: Analytics,
|
|
7
|
+
/** Config passed into the analytics module from `app.config.ts`. */
|
|
8
|
+
config: AnalyticsConfig,
|
|
9
|
+
/** Provider options */
|
|
10
|
+
options: T) => ReturnType<AnalyticsProvider>): (options: T) => AnalyticsProvider;
|
|
10
11
|
|
|
11
|
-
export {
|
|
12
|
+
export { createAnalytics, defineAnalyticsProvider };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { A as AnalyticsConfig } from './shared/analytics.2893a879.js';
|
|
1
|
+
import { AnalyticsConfig, Analytics, AnalyticsProvider } from './types.js';
|
|
3
2
|
|
|
4
|
-
declare
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
declare function createAnalytics(config?: AnalyticsConfig): Analytics;
|
|
4
|
+
declare function defineAnalyticsProvider<T = never>(definition: (
|
|
5
|
+
/** The analytics object. */
|
|
6
|
+
analytics: Analytics,
|
|
7
|
+
/** Config passed into the analytics module from `app.config.ts`. */
|
|
8
|
+
config: AnalyticsConfig,
|
|
9
|
+
/** Provider options */
|
|
10
|
+
options: T) => ReturnType<AnalyticsProvider>): (options: T) => AnalyticsProvider;
|
|
10
11
|
|
|
11
|
-
export {
|
|
12
|
+
export { createAnalytics, defineAnalyticsProvider };
|
package/dist/index.mjs
CHANGED
|
@@ -1,36 +1,208 @@
|
|
|
1
|
-
import '
|
|
2
|
-
import 'wxt/sandbox';
|
|
3
|
-
import { defineWxtModule, addAlias } from 'wxt/modules';
|
|
4
|
-
import { resolve } from 'node:path';
|
|
1
|
+
import { UAParser } from 'ua-parser-js';
|
|
5
2
|
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
});
|
|
16
|
-
const analyticsModulePath = resolve(
|
|
17
|
-
wxt.config.wxtDir,
|
|
18
|
-
"analytics/index.ts"
|
|
3
|
+
const ANALYTICS_PORT = "@wxt-dev/analytics";
|
|
4
|
+
function createAnalytics(config) {
|
|
5
|
+
if (typeof chrome === "undefined" || !chrome?.runtime?.id)
|
|
6
|
+
throw Error(
|
|
7
|
+
"Cannot use WXT analytics in contexts without access to the browser.runtime APIs"
|
|
8
|
+
);
|
|
9
|
+
if (config == null) {
|
|
10
|
+
console.warn(
|
|
11
|
+
"[@wxt-dev/analytics] Config not provided to createAnalytics. If you're using WXT, add the 'analytics' property to '<srcDir>/app.config.ts'."
|
|
19
12
|
);
|
|
20
|
-
const analyticsModuleCode = `
|
|
21
|
-
import { createAnalytics } from '@wxt-dev/analytics/client';
|
|
22
|
-
import { useAppConfig } from 'wxt/client';
|
|
23
|
-
|
|
24
|
-
export const analytics = createAnalytics(useAppConfig().analytics);
|
|
25
|
-
`;
|
|
26
|
-
addAlias(wxt, "#analytics", analyticsModulePath);
|
|
27
|
-
wxt.hook("prepare:types", async (_, entries) => {
|
|
28
|
-
entries.push({
|
|
29
|
-
path: analyticsModulePath,
|
|
30
|
-
text: analyticsModuleCode
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
13
|
}
|
|
34
|
-
|
|
14
|
+
if (location.pathname === "/background.js")
|
|
15
|
+
return createBackgroundAnalytics(config);
|
|
16
|
+
return createFrontendAnalytics();
|
|
17
|
+
}
|
|
18
|
+
function createBackgroundAnalytics(config) {
|
|
19
|
+
const userIdStorage = config?.userId ?? defineStorageItem("wxt-analytics:user-id");
|
|
20
|
+
const userPropertiesStorage = config?.userProperties ?? defineStorageItem(
|
|
21
|
+
"wxt-analytics:user-properties",
|
|
22
|
+
{}
|
|
23
|
+
);
|
|
24
|
+
const enabled = config?.enabled ?? defineStorageItem("local:wxt-analytics:enabled", false);
|
|
25
|
+
const platformInfo = chrome.runtime.getPlatformInfo();
|
|
26
|
+
const userAgent = UAParser();
|
|
27
|
+
let userId = Promise.resolve(userIdStorage.getValue()).then(
|
|
28
|
+
(id) => id ?? globalThis.crypto.randomUUID()
|
|
29
|
+
);
|
|
30
|
+
let userProperties = userPropertiesStorage.getValue();
|
|
31
|
+
const manifest = chrome.runtime.getManifest();
|
|
32
|
+
const getBackgroundMeta = () => ({
|
|
33
|
+
timestamp: Date.now(),
|
|
34
|
+
// Don't track sessions for the background, it can be running
|
|
35
|
+
// indefinitely, and will inflate session duration stats.
|
|
36
|
+
sessionId: void 0,
|
|
37
|
+
language: navigator.language,
|
|
38
|
+
referrer: void 0,
|
|
39
|
+
screen: void 0,
|
|
40
|
+
url: location.href,
|
|
41
|
+
title: void 0
|
|
42
|
+
});
|
|
43
|
+
const getBaseEvent = async (meta) => {
|
|
44
|
+
const platform = await platformInfo;
|
|
45
|
+
return {
|
|
46
|
+
meta,
|
|
47
|
+
user: {
|
|
48
|
+
id: await userId,
|
|
49
|
+
properties: {
|
|
50
|
+
version: config?.version ?? manifest.version_name ?? manifest.version,
|
|
51
|
+
wxtMode: import.meta.env.MODE,
|
|
52
|
+
wxtBrowser: import.meta.env.BROWSER,
|
|
53
|
+
arch: platform.arch,
|
|
54
|
+
os: platform.os,
|
|
55
|
+
browser: userAgent.browser.name,
|
|
56
|
+
browserVersion: userAgent.browser.version,
|
|
57
|
+
...await userProperties
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
const analytics = {
|
|
63
|
+
identify: async (newUserId, newUserProperties = {}, meta = getBackgroundMeta()) => {
|
|
64
|
+
userId = Promise.resolve(newUserId);
|
|
65
|
+
userProperties = Promise.resolve(newUserProperties);
|
|
66
|
+
await Promise.all([
|
|
67
|
+
userIdStorage.setValue?.(newUserId),
|
|
68
|
+
userPropertiesStorage.setValue?.(newUserProperties)
|
|
69
|
+
]);
|
|
70
|
+
const event = await getBaseEvent(meta);
|
|
71
|
+
if (config?.debug)
|
|
72
|
+
console.debug("[@wxt-dev/analytics] identify", event);
|
|
73
|
+
if (await enabled.getValue()) {
|
|
74
|
+
await Promise.allSettled(
|
|
75
|
+
providers.map((provider) => provider.identify(event))
|
|
76
|
+
);
|
|
77
|
+
} else if (config?.debug) {
|
|
78
|
+
console.debug(
|
|
79
|
+
"[@wxt-dev/analytics] Analytics disabled, identify() not uploaded"
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
page: async (location2, meta = getBackgroundMeta()) => {
|
|
84
|
+
const baseEvent = await getBaseEvent(meta);
|
|
85
|
+
const event = {
|
|
86
|
+
...baseEvent,
|
|
87
|
+
page: {
|
|
88
|
+
url: meta?.url ?? globalThis.location?.href,
|
|
89
|
+
location: location2,
|
|
90
|
+
title: meta?.title ?? globalThis.document?.title
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
if (config?.debug)
|
|
94
|
+
console.debug("[@wxt-dev/analytics] page", event);
|
|
95
|
+
if (await enabled.getValue()) {
|
|
96
|
+
await Promise.allSettled(
|
|
97
|
+
providers.map((provider) => provider.page(event))
|
|
98
|
+
);
|
|
99
|
+
} else if (config?.debug) {
|
|
100
|
+
console.debug(
|
|
101
|
+
"[@wxt-dev/analytics] Analytics disabled, page() not uploaded"
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
track: async (eventName, eventProperties, meta = getBackgroundMeta()) => {
|
|
106
|
+
const baseEvent = await getBaseEvent(meta);
|
|
107
|
+
const event = {
|
|
108
|
+
...baseEvent,
|
|
109
|
+
event: { name: eventName, properties: eventProperties }
|
|
110
|
+
};
|
|
111
|
+
if (config?.debug)
|
|
112
|
+
console.debug("[@wxt-dev/analytics] track", event);
|
|
113
|
+
if (await enabled.getValue()) {
|
|
114
|
+
await Promise.allSettled(
|
|
115
|
+
providers.map((provider) => provider.track(event))
|
|
116
|
+
);
|
|
117
|
+
} else if (config?.debug) {
|
|
118
|
+
console.debug(
|
|
119
|
+
"[@wxt-dev/analytics] Analytics disabled, track() not uploaded"
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
setEnabled: async (newEnabled) => {
|
|
124
|
+
await enabled.setValue?.(newEnabled);
|
|
125
|
+
},
|
|
126
|
+
autoTrack: () => {
|
|
127
|
+
return () => {
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
const providers = config?.providers?.map((provider) => provider(analytics, config)) ?? [];
|
|
132
|
+
chrome.runtime.onConnect.addListener((port) => {
|
|
133
|
+
if (port.name === ANALYTICS_PORT) {
|
|
134
|
+
port.onMessage.addListener(({ fn, args }) => {
|
|
135
|
+
void analytics[fn]?.(...args);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return analytics;
|
|
140
|
+
}
|
|
141
|
+
function createFrontendAnalytics() {
|
|
142
|
+
const port = chrome.runtime.connect({ name: ANALYTICS_PORT });
|
|
143
|
+
const sessionId = Date.now();
|
|
144
|
+
const getFrontendMetadata = () => ({
|
|
145
|
+
sessionId,
|
|
146
|
+
timestamp: Date.now(),
|
|
147
|
+
language: navigator.language,
|
|
148
|
+
referrer: globalThis.document?.referrer || void 0,
|
|
149
|
+
screen: globalThis.window ? `${globalThis.window.screen.width}x${globalThis.window.screen.height}` : void 0,
|
|
150
|
+
url: location.href,
|
|
151
|
+
title: document.title || void 0
|
|
152
|
+
});
|
|
153
|
+
const methodForwarder = (fn) => (...args) => {
|
|
154
|
+
port.postMessage({ fn, args: [...args, getFrontendMetadata()] });
|
|
155
|
+
};
|
|
156
|
+
const analytics = {
|
|
157
|
+
identify: methodForwarder("identify"),
|
|
158
|
+
page: methodForwarder("page"),
|
|
159
|
+
track: methodForwarder("track"),
|
|
160
|
+
setEnabled: methodForwarder("setEnabled"),
|
|
161
|
+
autoTrack: (root) => {
|
|
162
|
+
const onClick = (event) => {
|
|
163
|
+
const element = event.target;
|
|
164
|
+
if (!element || !INTERACTIVE_TAGS.has(element.tagName) && !INTERACTIVE_ROLES.has(element.getAttribute("role")))
|
|
165
|
+
return;
|
|
166
|
+
void analytics.track("click", {
|
|
167
|
+
tagName: element.tagName?.toLowerCase(),
|
|
168
|
+
id: element.id || void 0,
|
|
169
|
+
className: element.className || void 0,
|
|
170
|
+
textContent: element.textContent?.substring(0, 50) || void 0,
|
|
171
|
+
// Limit text content length
|
|
172
|
+
href: element.href
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
root.addEventListener("click", onClick, { capture: true, passive: true });
|
|
176
|
+
return () => {
|
|
177
|
+
root.removeEventListener("click", onClick);
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
return analytics;
|
|
182
|
+
}
|
|
183
|
+
function defineStorageItem(key, defaultValue) {
|
|
184
|
+
return {
|
|
185
|
+
getValue: async () => (await chrome.storage.local.get(key))[key] ?? defaultValue,
|
|
186
|
+
setValue: (newValue) => chrome.storage.local.set({ [key]: newValue })
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
const INTERACTIVE_TAGS = /* @__PURE__ */ new Set([
|
|
190
|
+
"A",
|
|
191
|
+
"BUTTON",
|
|
192
|
+
"INPUT",
|
|
193
|
+
"SELECT",
|
|
194
|
+
"TEXTAREA"
|
|
195
|
+
]);
|
|
196
|
+
const INTERACTIVE_ROLES = /* @__PURE__ */ new Set([
|
|
197
|
+
"button",
|
|
198
|
+
"link",
|
|
199
|
+
"checkbox",
|
|
200
|
+
"menuitem",
|
|
201
|
+
"tab",
|
|
202
|
+
"radio"
|
|
203
|
+
]);
|
|
204
|
+
function defineAnalyticsProvider(definition) {
|
|
205
|
+
return (options) => (analytics, config) => definition(analytics, config, options);
|
|
206
|
+
}
|
|
35
207
|
|
|
36
|
-
export {
|
|
208
|
+
export { createAnalytics, defineAnalyticsProvider };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as wxt from 'wxt';
|
|
2
|
+
import { AnalyticsConfig } from './types.mjs';
|
|
3
|
+
|
|
4
|
+
declare module 'wxt/sandbox' {
|
|
5
|
+
interface WxtAppConfig {
|
|
6
|
+
analytics: AnalyticsConfig;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
declare const _default: wxt.WxtModule<wxt.WxtModuleOptions>;
|
|
10
|
+
|
|
11
|
+
export { _default as default };
|
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as wxt from 'wxt';
|
|
2
|
+
import { AnalyticsConfig } from './types.js';
|
|
3
|
+
|
|
4
|
+
declare module 'wxt/sandbox' {
|
|
5
|
+
interface WxtAppConfig {
|
|
6
|
+
analytics: AnalyticsConfig;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
declare const _default: wxt.WxtModule<wxt.WxtModuleOptions>;
|
|
10
|
+
|
|
11
|
+
export { _default as default };
|
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import 'wxt';
|
|
2
|
+
import 'wxt/sandbox';
|
|
3
|
+
import { defineWxtModule, addAlias, addWxtPlugin } from 'wxt/modules';
|
|
4
|
+
import { resolve } from 'node:path';
|
|
5
|
+
|
|
6
|
+
const index = defineWxtModule({
|
|
7
|
+
name: "analytics",
|
|
8
|
+
imports: [{ name: "analytics", from: "#analytics" }],
|
|
9
|
+
setup(wxt) {
|
|
10
|
+
const wxtAnalyticsFolder = resolve(wxt.config.wxtDir, "analytics");
|
|
11
|
+
const wxtAnalyticsIndex = resolve(wxtAnalyticsFolder, "index.ts");
|
|
12
|
+
const clientModuleId = "@wxt-dev/analytics" ;
|
|
13
|
+
const pluginModuleId = "@wxt-dev/analytics/background-plugin" ;
|
|
14
|
+
wxt.hook("build:manifestGenerated", (_, manifest) => {
|
|
15
|
+
manifest.permissions ?? (manifest.permissions = []);
|
|
16
|
+
if (!manifest.permissions.includes("storage")) {
|
|
17
|
+
manifest.permissions.push("storage");
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
const wxtAnalyticsCode = [
|
|
21
|
+
`import { createAnalytics } from '${clientModuleId }';`,
|
|
22
|
+
`import { useAppConfig } from 'wxt/client';`,
|
|
23
|
+
``,
|
|
24
|
+
`export const analytics = createAnalytics(useAppConfig().analytics);`,
|
|
25
|
+
``
|
|
26
|
+
].join("\n");
|
|
27
|
+
addAlias(wxt, "#analytics", wxtAnalyticsIndex);
|
|
28
|
+
wxt.hook("prepare:types", async (_, entries) => {
|
|
29
|
+
entries.push({
|
|
30
|
+
path: wxtAnalyticsIndex,
|
|
31
|
+
text: wxtAnalyticsCode
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
wxt.hook("entrypoints:resolved", (_, entrypoints) => {
|
|
35
|
+
const hasBackground = entrypoints.find(
|
|
36
|
+
(entry) => entry.type === "background"
|
|
37
|
+
);
|
|
38
|
+
if (!hasBackground) {
|
|
39
|
+
entrypoints.push({
|
|
40
|
+
type: "background",
|
|
41
|
+
inputPath: "virtual:user-background",
|
|
42
|
+
name: "background",
|
|
43
|
+
options: {},
|
|
44
|
+
outputDir: wxt.config.outDir,
|
|
45
|
+
skipped: false
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
addWxtPlugin(wxt, pluginModuleId);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export { index as default };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AnalyticsProvider } from '../types.mjs';
|
|
2
2
|
|
|
3
|
-
interface
|
|
3
|
+
interface GoogleAnalytics4ProviderOptions {
|
|
4
4
|
apiSecret: string;
|
|
5
5
|
measurementId: string;
|
|
6
6
|
}
|
|
7
|
-
declare const googleAnalytics4: (options:
|
|
7
|
+
declare const googleAnalytics4: (options: GoogleAnalytics4ProviderOptions) => AnalyticsProvider;
|
|
8
8
|
|
|
9
|
-
export { type
|
|
9
|
+
export { type GoogleAnalytics4ProviderOptions, googleAnalytics4 };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AnalyticsProvider } from '../types.js';
|
|
2
2
|
|
|
3
|
-
interface
|
|
3
|
+
interface GoogleAnalytics4ProviderOptions {
|
|
4
4
|
apiSecret: string;
|
|
5
5
|
measurementId: string;
|
|
6
6
|
}
|
|
7
|
-
declare const googleAnalytics4: (options:
|
|
7
|
+
declare const googleAnalytics4: (options: GoogleAnalytics4ProviderOptions) => AnalyticsProvider;
|
|
8
8
|
|
|
9
|
-
export { type
|
|
9
|
+
export { type GoogleAnalytics4ProviderOptions, googleAnalytics4 };
|
|
@@ -1,56 +1,61 @@
|
|
|
1
|
+
import { defineAnalyticsProvider } from '../index.mjs';
|
|
2
|
+
import 'ua-parser-js';
|
|
3
|
+
|
|
1
4
|
const DEFAULT_ENGAGEMENT_TIME_IN_MSEC = 100;
|
|
2
|
-
const googleAnalytics4 = (
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
Object.
|
|
19
|
-
name,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
5
|
+
const googleAnalytics4 = defineAnalyticsProvider(
|
|
6
|
+
(_, config, options) => {
|
|
7
|
+
const send = async (data, eventName, eventProperties) => {
|
|
8
|
+
const url = new URL(
|
|
9
|
+
config?.debug ? "/debug/mp/collect" : "/mp/collect",
|
|
10
|
+
"https://www.google-analytics.com"
|
|
11
|
+
);
|
|
12
|
+
if (options.apiSecret)
|
|
13
|
+
url.searchParams.set("api_secret", options.apiSecret);
|
|
14
|
+
if (options.measurementId)
|
|
15
|
+
url.searchParams.set("measurement_id", options.measurementId);
|
|
16
|
+
const userProperties = {
|
|
17
|
+
language: data.meta.language,
|
|
18
|
+
screen: data.meta.screen,
|
|
19
|
+
...data.user.properties
|
|
20
|
+
};
|
|
21
|
+
const mappedUserProperties = Object.fromEntries(
|
|
22
|
+
Object.entries(userProperties).map(([name, value]) => [
|
|
23
|
+
name,
|
|
24
|
+
value == null ? void 0 : { value }
|
|
25
|
+
])
|
|
26
|
+
);
|
|
27
|
+
await fetch(url.href, {
|
|
28
|
+
method: "POST",
|
|
29
|
+
body: JSON.stringify({
|
|
30
|
+
client_id: data.user.id,
|
|
31
|
+
consent: {
|
|
32
|
+
ad_user_data: "DENIED",
|
|
33
|
+
ad_personalization: "DENIED"
|
|
34
|
+
},
|
|
35
|
+
user_properties: mappedUserProperties,
|
|
36
|
+
events: [
|
|
37
|
+
{
|
|
38
|
+
name: eventName,
|
|
39
|
+
params: {
|
|
40
|
+
session_id: data.meta.sessionId,
|
|
41
|
+
engagement_time_msec: DEFAULT_ENGAGEMENT_TIME_IN_MSEC,
|
|
42
|
+
...eventProperties
|
|
43
|
+
}
|
|
39
44
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
})
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
45
|
+
]
|
|
46
|
+
})
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
identify: () => Promise.resolve(),
|
|
51
|
+
// No-op, user data uploaded in page/track
|
|
52
|
+
page: (event) => send(event, "page_view", {
|
|
53
|
+
page_title: event.page.title,
|
|
54
|
+
page_location: event.page.location
|
|
55
|
+
}),
|
|
56
|
+
track: (event) => send(event, event.event.name, event.event.properties)
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
);
|
|
55
60
|
|
|
56
61
|
export { googleAnalytics4 };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AnalyticsProvider } from '../types.mjs';
|
|
2
2
|
|
|
3
3
|
interface UmamiProviderOptions {
|
|
4
|
-
|
|
4
|
+
apiUrl: string;
|
|
5
5
|
websiteId: string;
|
|
6
|
-
|
|
6
|
+
domain: string;
|
|
7
7
|
}
|
|
8
8
|
declare const umami: (options: UmamiProviderOptions) => AnalyticsProvider;
|
|
9
9
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AnalyticsProvider } from '../types.js';
|
|
2
2
|
|
|
3
3
|
interface UmamiProviderOptions {
|
|
4
|
-
|
|
4
|
+
apiUrl: string;
|
|
5
5
|
websiteId: string;
|
|
6
|
-
|
|
6
|
+
domain: string;
|
|
7
7
|
}
|
|
8
8
|
declare const umami: (options: UmamiProviderOptions) => AnalyticsProvider;
|
|
9
9
|
|
package/dist/providers/umami.mjs
CHANGED
|
@@ -1,44 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
url: event.page.url,
|
|
17
|
-
hostname: options.hostname,
|
|
18
|
-
language: event.meta.language ?? "",
|
|
19
|
-
referrer: event.meta.referrer ?? "",
|
|
20
|
-
screen: event.meta.screen ?? "",
|
|
21
|
-
title: event.page.title ?? "<blank>",
|
|
22
|
-
data: event.user.properties
|
|
23
|
-
});
|
|
24
|
-
},
|
|
25
|
-
track: async (event) => {
|
|
26
|
-
await send({
|
|
27
|
-
name: event.event.name,
|
|
28
|
-
website: options.websiteId,
|
|
29
|
-
url: event.meta.url ?? "/",
|
|
30
|
-
title: "<blank>",
|
|
31
|
-
hostname: options.hostname,
|
|
32
|
-
language: event.meta.language ?? "",
|
|
33
|
-
referrer: event.meta.referrer ?? "",
|
|
34
|
-
screen: event.meta.screen ?? "",
|
|
35
|
-
data: {
|
|
36
|
-
...event.event.properties,
|
|
37
|
-
...event.user.properties
|
|
38
|
-
}
|
|
1
|
+
import { defineAnalyticsProvider } from '../index.mjs';
|
|
2
|
+
import 'ua-parser-js';
|
|
3
|
+
|
|
4
|
+
const umami = defineAnalyticsProvider(
|
|
5
|
+
(_, config, options) => {
|
|
6
|
+
const send = (payload) => {
|
|
7
|
+
if (config.debug) {
|
|
8
|
+
console.debug("[@wxt-dev/analytics] Sending event to Umami:", payload);
|
|
9
|
+
}
|
|
10
|
+
return fetch(`${options.apiUrl}/send`, {
|
|
11
|
+
method: "POST",
|
|
12
|
+
headers: {
|
|
13
|
+
"Content-Type": "application/json"
|
|
14
|
+
},
|
|
15
|
+
body: JSON.stringify({ type: "event", payload })
|
|
39
16
|
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
17
|
+
};
|
|
18
|
+
return {
|
|
19
|
+
identify: () => Promise.resolve(),
|
|
20
|
+
// No-op, user data uploaded in page/track
|
|
21
|
+
page: async (event) => {
|
|
22
|
+
await send({
|
|
23
|
+
name: "page_view",
|
|
24
|
+
website: options.websiteId,
|
|
25
|
+
url: event.page.url,
|
|
26
|
+
hostname: options.domain,
|
|
27
|
+
language: event.meta.language ?? "",
|
|
28
|
+
referrer: event.meta.referrer ?? "",
|
|
29
|
+
screen: event.meta.screen ?? "",
|
|
30
|
+
title: event.page.title ?? "<blank>",
|
|
31
|
+
data: event.user.properties
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
track: async (event) => {
|
|
35
|
+
await send({
|
|
36
|
+
name: event.event.name,
|
|
37
|
+
website: options.websiteId,
|
|
38
|
+
url: event.meta.url ?? "/",
|
|
39
|
+
title: "<blank>",
|
|
40
|
+
hostname: options.domain,
|
|
41
|
+
language: event.meta.language ?? "",
|
|
42
|
+
referrer: event.meta.referrer ?? "",
|
|
43
|
+
screen: event.meta.screen ?? "",
|
|
44
|
+
data: {
|
|
45
|
+
...event.event.properties,
|
|
46
|
+
...event.user.properties
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
);
|
|
43
53
|
|
|
44
54
|
export { umami };
|
|
@@ -45,7 +45,7 @@ type AnalyticsProvider = (analytics: Analytics, config: AnalyticsConfig) => {
|
|
|
45
45
|
page: (event: AnalyticsPageViewEvent) => Promise<void>;
|
|
46
46
|
/** Upload a custom event */
|
|
47
47
|
track: (event: AnalyticsTrackEvent) => Promise<void>;
|
|
48
|
-
/** Upload
|
|
48
|
+
/** Upload information about the user */
|
|
49
49
|
identify: (event: BaseAnalyticsEvent) => Promise<void>;
|
|
50
50
|
};
|
|
51
51
|
interface BaseAnalyticsEvent {
|
|
@@ -86,4 +86,4 @@ interface AnalyticsTrackEvent extends BaseAnalyticsEvent {
|
|
|
86
86
|
};
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
export type { AnalyticsConfig
|
|
89
|
+
export type { Analytics, AnalyticsConfig, AnalyticsEventMetadata, AnalyticsPageInfo, AnalyticsPageViewEvent, AnalyticsProvider, AnalyticsStorageItem, AnalyticsTrackEvent, BaseAnalyticsEvent };
|
|
@@ -45,7 +45,7 @@ type AnalyticsProvider = (analytics: Analytics, config: AnalyticsConfig) => {
|
|
|
45
45
|
page: (event: AnalyticsPageViewEvent) => Promise<void>;
|
|
46
46
|
/** Upload a custom event */
|
|
47
47
|
track: (event: AnalyticsTrackEvent) => Promise<void>;
|
|
48
|
-
/** Upload
|
|
48
|
+
/** Upload information about the user */
|
|
49
49
|
identify: (event: BaseAnalyticsEvent) => Promise<void>;
|
|
50
50
|
};
|
|
51
51
|
interface BaseAnalyticsEvent {
|
|
@@ -86,4 +86,4 @@ interface AnalyticsTrackEvent extends BaseAnalyticsEvent {
|
|
|
86
86
|
};
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
export type { AnalyticsConfig
|
|
89
|
+
export type { Analytics, AnalyticsConfig, AnalyticsEventMetadata, AnalyticsPageInfo, AnalyticsPageViewEvent, AnalyticsProvider, AnalyticsStorageItem, AnalyticsTrackEvent, BaseAnalyticsEvent };
|
package/dist/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxt-dev/analytics",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Add analytics to your web extension",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,9 +14,16 @@
|
|
|
14
14
|
"types": "./dist/index.d.mts",
|
|
15
15
|
"default": "./dist/index.mjs"
|
|
16
16
|
},
|
|
17
|
-
"./
|
|
18
|
-
"types": "./dist/
|
|
19
|
-
"default": "./dist/
|
|
17
|
+
"./module": {
|
|
18
|
+
"types": "./dist/module.d.mts",
|
|
19
|
+
"default": "./dist/module.mjs"
|
|
20
|
+
},
|
|
21
|
+
"./background-plugin": {
|
|
22
|
+
"types": "./dist/background-plugin.d.mts",
|
|
23
|
+
"default": "./dist/background-plugin.mjs"
|
|
24
|
+
},
|
|
25
|
+
"./types": {
|
|
26
|
+
"types": "./dist/types.d.mts"
|
|
20
27
|
},
|
|
21
28
|
"./providers/google-analytics-4": {
|
|
22
29
|
"types": "./dist/providers/google-analytics-4.d.mts",
|
|
@@ -33,17 +40,16 @@
|
|
|
33
40
|
"dist"
|
|
34
41
|
],
|
|
35
42
|
"peerDependencies": {
|
|
36
|
-
"wxt": ">=0.
|
|
43
|
+
"wxt": ">=0.19.23"
|
|
37
44
|
},
|
|
38
45
|
"devDependencies": {
|
|
39
|
-
"@aklinker1/check": "^1.
|
|
46
|
+
"@aklinker1/check": "^1.4.5",
|
|
40
47
|
"@types/chrome": "^0.0.268",
|
|
41
48
|
"@types/ua-parser-js": "^0.7.39",
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"typescript": "^5.5.2",
|
|
49
|
+
"publint": "^0.2.12",
|
|
50
|
+
"typescript": "^5.6.3",
|
|
45
51
|
"unbuild": "^2.0.0",
|
|
46
|
-
"wxt": "0.19.
|
|
52
|
+
"wxt": "0.19.23"
|
|
47
53
|
},
|
|
48
54
|
"dependencies": {
|
|
49
55
|
"ua-parser-js": "^1.0.38"
|
|
@@ -51,7 +57,7 @@
|
|
|
51
57
|
"scripts": {
|
|
52
58
|
"dev": "buildc --deps-only -- wxt",
|
|
53
59
|
"dev:build": "buildc --deps-only -- wxt build",
|
|
54
|
-
"check": "
|
|
60
|
+
"check": "pnpm build && check",
|
|
55
61
|
"build": "buildc -- unbuild"
|
|
56
62
|
}
|
|
57
63
|
}
|
package/dist/client.d.mts
DELETED
package/dist/client.d.ts
DELETED
package/dist/client.mjs
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
import { UAParser } from 'ua-parser-js';
|
|
2
|
-
|
|
3
|
-
const ANALYTICS_PORT = "@wxt-dev/analytics";
|
|
4
|
-
function createAnalytics(config) {
|
|
5
|
-
if (typeof chrome === "undefined" || !chrome?.runtime?.id)
|
|
6
|
-
throw Error(
|
|
7
|
-
"Cannot use WXT analytics in contexts without access to the browser.runtime APIs"
|
|
8
|
-
);
|
|
9
|
-
if (config == null) {
|
|
10
|
-
console.warn(
|
|
11
|
-
"[@wxt-dev/analytics] Config not provided to createAnalytics. If you're using WXT, add the 'analytics' property to '<srcDir>/app.config.ts'."
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
if (location.pathname === "/background.js")
|
|
15
|
-
return createBackgroundAnalytics(config);
|
|
16
|
-
return createFrontendAnalytics();
|
|
17
|
-
}
|
|
18
|
-
function createBackgroundAnalytics(config) {
|
|
19
|
-
const userIdStorage = config?.userId ?? defineStorageItem("wxt-analytics:user-id");
|
|
20
|
-
const userPropertiesStorage = config?.userProperties ?? defineStorageItem(
|
|
21
|
-
"wxt-analytics:user-properties",
|
|
22
|
-
{}
|
|
23
|
-
);
|
|
24
|
-
const enabled = config?.enabled ?? defineStorageItem("local:wxt-analytics:enabled", false);
|
|
25
|
-
const platformInfo = chrome.runtime.getPlatformInfo();
|
|
26
|
-
const userAgent = UAParser();
|
|
27
|
-
let userId = Promise.resolve(userIdStorage.getValue()).then(
|
|
28
|
-
(id) => id ?? globalThis.crypto.randomUUID()
|
|
29
|
-
);
|
|
30
|
-
let userProperties = userPropertiesStorage.getValue();
|
|
31
|
-
const manifest = chrome.runtime.getManifest();
|
|
32
|
-
const getBackgroundMeta = () => ({
|
|
33
|
-
timestamp: Date.now(),
|
|
34
|
-
// Don't track sessions for the background, it can be running
|
|
35
|
-
// indefinitely, and will inflate session duration stats.
|
|
36
|
-
sessionId: void 0,
|
|
37
|
-
language: navigator.language,
|
|
38
|
-
referrer: void 0,
|
|
39
|
-
screen: void 0,
|
|
40
|
-
url: location.href,
|
|
41
|
-
title: void 0
|
|
42
|
-
});
|
|
43
|
-
const getBaseEvent = async (meta) => {
|
|
44
|
-
const platform = await platformInfo;
|
|
45
|
-
return {
|
|
46
|
-
meta,
|
|
47
|
-
user: {
|
|
48
|
-
id: await userId,
|
|
49
|
-
properties: {
|
|
50
|
-
version: config?.version ?? manifest.version_name ?? manifest.version,
|
|
51
|
-
wxtMode: import.meta.env.MODE,
|
|
52
|
-
wxtBrowser: import.meta.env.BROWSER,
|
|
53
|
-
arch: platform.arch,
|
|
54
|
-
os: platform.os,
|
|
55
|
-
browser: userAgent.browser.name,
|
|
56
|
-
browserVersion: userAgent.browser.version,
|
|
57
|
-
...await userProperties
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
};
|
|
62
|
-
const analytics = {
|
|
63
|
-
identify: async (newUserId, newUserProperties = {}, meta = getBackgroundMeta()) => {
|
|
64
|
-
userId = Promise.resolve(newUserId);
|
|
65
|
-
userProperties = Promise.resolve(newUserProperties);
|
|
66
|
-
await Promise.all([
|
|
67
|
-
userIdStorage.setValue?.(newUserId),
|
|
68
|
-
userPropertiesStorage.setValue?.(newUserProperties)
|
|
69
|
-
]);
|
|
70
|
-
const event = await getBaseEvent(meta);
|
|
71
|
-
if (config?.debug)
|
|
72
|
-
console.debug("[analytics] identify", event);
|
|
73
|
-
if (await enabled.getValue()) {
|
|
74
|
-
await Promise.allSettled(
|
|
75
|
-
providers.map((provider) => provider.identify(event))
|
|
76
|
-
);
|
|
77
|
-
} else if (config?.debug) {
|
|
78
|
-
console.debug(
|
|
79
|
-
"[analytics] Analytics disabled, identify() not uploaded"
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
page: async (location2, meta = getBackgroundMeta()) => {
|
|
84
|
-
const baseEvent = await getBaseEvent(meta);
|
|
85
|
-
const event = {
|
|
86
|
-
...baseEvent,
|
|
87
|
-
page: {
|
|
88
|
-
url: meta?.url ?? globalThis.location?.href,
|
|
89
|
-
location: location2,
|
|
90
|
-
title: meta?.title ?? globalThis.document?.title
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
if (config?.debug)
|
|
94
|
-
console.debug("[analytics] page", event);
|
|
95
|
-
if (await enabled.getValue()) {
|
|
96
|
-
await Promise.allSettled(
|
|
97
|
-
providers.map((provider) => provider.page(event))
|
|
98
|
-
);
|
|
99
|
-
} else if (config?.debug) {
|
|
100
|
-
console.debug("[analytics] Analytics disabled, page() not uploaded");
|
|
101
|
-
}
|
|
102
|
-
},
|
|
103
|
-
track: async (eventName, eventProperties, meta = getBackgroundMeta()) => {
|
|
104
|
-
const baseEvent = await getBaseEvent(meta);
|
|
105
|
-
const event = {
|
|
106
|
-
...baseEvent,
|
|
107
|
-
event: { name: eventName, properties: eventProperties }
|
|
108
|
-
};
|
|
109
|
-
if (config?.debug)
|
|
110
|
-
console.debug("[analytics] track", event);
|
|
111
|
-
if (await enabled.getValue()) {
|
|
112
|
-
await Promise.allSettled(
|
|
113
|
-
providers.map((provider) => provider.track(event))
|
|
114
|
-
);
|
|
115
|
-
} else if (config?.debug) {
|
|
116
|
-
console.debug("[analytics] Analytics disabled, track() not uploaded");
|
|
117
|
-
}
|
|
118
|
-
},
|
|
119
|
-
setEnabled: async (newEnabled) => {
|
|
120
|
-
await enabled.setValue?.(newEnabled);
|
|
121
|
-
},
|
|
122
|
-
autoTrack: () => {
|
|
123
|
-
return () => {
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
const providers = config?.providers?.map((provider) => provider(analytics, config)) ?? [];
|
|
128
|
-
chrome.runtime.onConnect.addListener((port) => {
|
|
129
|
-
if (port.name === ANALYTICS_PORT) {
|
|
130
|
-
port.onMessage.addListener(({ fn, args }) => {
|
|
131
|
-
void analytics[fn]?.(...args);
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
return analytics;
|
|
136
|
-
}
|
|
137
|
-
function createFrontendAnalytics() {
|
|
138
|
-
const port = chrome.runtime.connect({ name: ANALYTICS_PORT });
|
|
139
|
-
const sessionId = Date.now();
|
|
140
|
-
const getFrontendMetadata = () => ({
|
|
141
|
-
sessionId,
|
|
142
|
-
timestamp: Date.now(),
|
|
143
|
-
language: navigator.language,
|
|
144
|
-
referrer: globalThis.document?.referrer || void 0,
|
|
145
|
-
screen: globalThis.window ? `${globalThis.window.screen.width}x${globalThis.window.screen.height}` : void 0,
|
|
146
|
-
url: location.href,
|
|
147
|
-
title: document.title || void 0
|
|
148
|
-
});
|
|
149
|
-
const methodForwarder = (fn) => (...args) => {
|
|
150
|
-
port.postMessage({ fn, args: [...args, getFrontendMetadata()] });
|
|
151
|
-
};
|
|
152
|
-
const analytics = {
|
|
153
|
-
identify: methodForwarder("identify"),
|
|
154
|
-
page: methodForwarder("page"),
|
|
155
|
-
track: methodForwarder("track"),
|
|
156
|
-
setEnabled: methodForwarder("setEnabled"),
|
|
157
|
-
autoTrack: (root) => {
|
|
158
|
-
const onClick = (event) => {
|
|
159
|
-
const element = event.target;
|
|
160
|
-
if (!element || !INTERACTIVE_TAGS.has(element.tagName) && !INTERACTIVE_ROLES.has(element.getAttribute("role")))
|
|
161
|
-
return;
|
|
162
|
-
void analytics.track("click", {
|
|
163
|
-
tagName: element.tagName?.toLowerCase(),
|
|
164
|
-
id: element.id || void 0,
|
|
165
|
-
className: element.className || void 0,
|
|
166
|
-
textContent: element.textContent?.substring(0, 50) || void 0,
|
|
167
|
-
// Limit text content length
|
|
168
|
-
href: element.href
|
|
169
|
-
});
|
|
170
|
-
};
|
|
171
|
-
root.addEventListener("click", onClick, { capture: true, passive: true });
|
|
172
|
-
return () => {
|
|
173
|
-
root.removeEventListener("click", onClick);
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
return analytics;
|
|
178
|
-
}
|
|
179
|
-
function defineStorageItem(key, defaultValue) {
|
|
180
|
-
return {
|
|
181
|
-
getValue: async () => (await chrome.storage.local.get(key))[key] ?? defaultValue,
|
|
182
|
-
setValue: (newValue) => chrome.storage.local.set({ [key]: newValue })
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
const INTERACTIVE_TAGS = /* @__PURE__ */ new Set([
|
|
186
|
-
"A",
|
|
187
|
-
"BUTTON",
|
|
188
|
-
"INPUT",
|
|
189
|
-
"SELECT",
|
|
190
|
-
"TEXTAREA"
|
|
191
|
-
]);
|
|
192
|
-
const INTERACTIVE_ROLES = /* @__PURE__ */ new Set([
|
|
193
|
-
"button",
|
|
194
|
-
"link",
|
|
195
|
-
"checkbox",
|
|
196
|
-
"menuitem",
|
|
197
|
-
"tab",
|
|
198
|
-
"radio"
|
|
199
|
-
]);
|
|
200
|
-
|
|
201
|
-
export { createAnalytics };
|