Skip to content

Commit

Permalink
Remove wxt/storage
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 committed Jul 9, 2024
1 parent 6164137 commit 2f6b400
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
28 changes: 17 additions & 11 deletions packages/analytics/modules/analytics/client.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { defineWxtPlugin } from 'wxt/sandbox';
import { storage } from 'wxt/storage';
import { useAppConfig } from 'wxt/client';
import { browser } from 'wxt/browser';
import {
Analytics,
AnalyticsConfig,
AnalyticsPageViewEvent,
AnalyticsTrackEvent,
BaseAnalyticsEvent,
AnalyticsStorageItemConfig,
} from './types';
import uaParser from 'ua-parser-js';

const defineStorageItem = <T>(
key: string,
defaultValue?: NonNullable<T>,
): AnalyticsStorageItemConfig<T> => ({
getValue: async () =>
(await chrome.storage.local.get(key))[key] ?? defaultValue,
setValue: (newValue) => chrome.storage.local.set({ [key]: newValue }),
});

export let analytics: Analytics;
const ANALYTICS_PORT = 'wxt-analytics';
const interactiveTags = new Set(['A', 'BUTTON', 'INPUT', 'SELECT', 'TEXTAREA']);
Expand All @@ -31,7 +39,7 @@ export default <any>defineWxtPlugin(() => {
});

function createAnalyticsForwarder(): Analytics {
const port = browser.runtime.connect({ name: ANALYTICS_PORT });
const port = chrome.runtime.connect({ name: ANALYTICS_PORT });
const sessionId = Date.now();
const getMetadata = (): ForwardMetadata => ({
sessionId,
Expand Down Expand Up @@ -92,27 +100,25 @@ function createBackgroundAnalytics(): Analytics {

// User properties storage
const userIdStorage =
config?.userId ?? storage.defineItem<string>('local:wxt-analytics:user-id');
config?.userId ?? defineStorageItem<string>('local:wxt-analytics:user-id');
const userPropertiesStorage =
config?.userProperties ??
storage.defineItem<Record<string, string>>(
defineStorageItem<Record<string, string>>(
'local:wxt-analytics:user-properties',
{ defaultValue: {} },
{},
);
const enabled =
config?.enabled ??
storage.defineItem<boolean>('local:wxt-analytics:enabled', {
defaultValue: false,
});
defineStorageItem<boolean>('local:wxt-analytics:enabled', false);

// Cached values
const platformInfo = browser.runtime.getPlatformInfo();
const platformInfo = chrome.runtime.getPlatformInfo();
const userAgent = uaParser();
let userId = Promise.resolve(userIdStorage.getValue()).then(
(id) => id ?? globalThis.crypto.randomUUID(),
);
let userProperties = userPropertiesStorage.getValue();
const manifest = browser.runtime.getManifest();
const manifest = chrome.runtime.getManifest();

const getBaseEvent = async (
meta: ForwardMetadata = {
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wxt-dev/analytics",
"version": "0.1.4",
"version": "0.1.5",
"description": "Add analytics to your web extension",
"repository": {
"type": "git",
Expand Down

0 comments on commit 2f6b400

Please sign in to comment.