@wxt-dev/analytics 0.1.4 → 0.1.6

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.
Files changed (2) hide show
  1. package/dist/client.mjs +11 -11
  2. package/package.json +1 -1
package/dist/client.mjs CHANGED
@@ -1,9 +1,11 @@
1
1
  import { defineWxtPlugin } from 'wxt/sandbox';
2
- import { storage } from 'wxt/storage';
3
2
  import { useAppConfig } from 'wxt/client';
4
- import { browser } from 'wxt/browser';
5
3
  import uaParser from 'ua-parser-js';
6
4
 
5
+ const defineStorageItem = (key, defaultValue) => ({
6
+ getValue: async () => (await chrome.storage.local.get(key))[key] ?? defaultValue,
7
+ setValue: (newValue) => chrome.storage.local.set({ [key]: newValue })
8
+ });
7
9
  let analytics;
8
10
  const ANALYTICS_PORT = "wxt-analytics";
9
11
  const interactiveTags = /* @__PURE__ */ new Set(["A", "BUTTON", "INPUT", "SELECT", "TEXTAREA"]);
@@ -20,7 +22,7 @@ const client = defineWxtPlugin(() => {
20
22
  analytics = isBackground ? createBackgroundAnalytics() : createAnalyticsForwarder();
21
23
  });
22
24
  function createAnalyticsForwarder() {
23
- const port = browser.runtime.connect({ name: ANALYTICS_PORT });
25
+ const port = chrome.runtime.connect({ name: ANALYTICS_PORT });
24
26
  const sessionId = Date.now();
25
27
  const getMetadata = () => ({
26
28
  sessionId,
@@ -60,21 +62,19 @@ function createAnalyticsForwarder() {
60
62
  }
61
63
  function createBackgroundAnalytics() {
62
64
  const config = useAppConfig().analytics;
63
- const userIdStorage = config?.userId ?? storage.defineItem("local:wxt-analytics:user-id");
64
- const userPropertiesStorage = config?.userProperties ?? storage.defineItem(
65
+ const userIdStorage = config?.userId ?? defineStorageItem("local:wxt-analytics:user-id");
66
+ const userPropertiesStorage = config?.userProperties ?? defineStorageItem(
65
67
  "local:wxt-analytics:user-properties",
66
- { defaultValue: {} }
68
+ {}
67
69
  );
68
- const enabled = config?.enabled ?? storage.defineItem("local:wxt-analytics:enabled", {
69
- defaultValue: false
70
- });
71
- const platformInfo = browser.runtime.getPlatformInfo();
70
+ const enabled = config?.enabled ?? defineStorageItem("local:wxt-analytics:enabled", false);
71
+ const platformInfo = chrome.runtime.getPlatformInfo();
72
72
  const userAgent = uaParser();
73
73
  let userId = Promise.resolve(userIdStorage.getValue()).then(
74
74
  (id) => id ?? globalThis.crypto.randomUUID()
75
75
  );
76
76
  let userProperties = userPropertiesStorage.getValue();
77
- const manifest = browser.runtime.getManifest();
77
+ const manifest = chrome.runtime.getManifest();
78
78
  const getBaseEvent = async (meta = {
79
79
  timestamp: Date.now(),
80
80
  // Don't track sessions for the background, it can be running
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxt-dev/analytics",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Add analytics to your web extension",
5
5
  "repository": {
6
6
  "type": "git",