@@ -9,11 +9,12 @@ import type {
99 AnalyticsEventMetadata ,
1010 AnalyticsProvider ,
1111} from './types' ;
12+ import { browser } from '@wxt-dev/browser' ;
1213
1314const ANALYTICS_PORT = '@wxt-dev/analytics' ;
1415
1516export function createAnalytics ( config ?: AnalyticsConfig ) : Analytics {
16- if ( typeof chrome === 'undefined' || ! chrome ?. runtime ?. id )
17+ if ( ! browser ?. runtime ?. id )
1718 throw Error (
1819 'Cannot use WXT analytics in contexts without access to the browser.runtime APIs' ,
1920 ) ;
@@ -51,13 +52,13 @@ function createBackgroundAnalytics(
5152 defineStorageItem < boolean > ( 'local:wxt-analytics:enabled' , false ) ;
5253
5354 // Cached values
54- const platformInfo = chrome . runtime . getPlatformInfo ( ) ;
55+ const platformInfo = browser . runtime . getPlatformInfo ( ) ;
5556 const userAgent = UAParser ( ) ;
5657 let userId = Promise . resolve ( userIdStorage . getValue ( ) ) . then (
5758 ( id ) => id ?? globalThis . crypto . randomUUID ( ) ,
5859 ) ;
5960 let userProperties = userPropertiesStorage . getValue ( ) ;
60- const manifest = chrome . runtime . getManifest ( ) ;
61+ const manifest = browser . runtime . getManifest ( ) ;
6162
6263 const getBackgroundMeta = ( ) => ( {
6364 timestamp : Date . now ( ) ,
@@ -178,7 +179,7 @@ function createBackgroundAnalytics(
178179 config ?. providers ?. map ( ( provider ) => provider ( analytics , config ) ) ?? [ ] ;
179180
180181 // Listen for messages from the rest of the extension
181- chrome . runtime . onConnect . addListener ( ( port ) => {
182+ browser . runtime . onConnect . addListener ( ( port ) => {
182183 if ( port . name === ANALYTICS_PORT ) {
183184 port . onMessage . addListener ( ( { fn, args } ) => {
184185 // @ts -expect-error: Untyped fn key
@@ -194,7 +195,7 @@ function createBackgroundAnalytics(
194195 * Creates an analytics client for non-background contexts.
195196 */
196197function createFrontendAnalytics ( ) : Analytics {
197- const port = chrome . runtime . connect ( { name : ANALYTICS_PORT } ) ;
198+ const port = browser . runtime . connect ( { name : ANALYTICS_PORT } ) ;
198199 const sessionId = Date . now ( ) ;
199200 const getFrontendMetadata = ( ) : AnalyticsEventMetadata => ( {
200201 sessionId,
@@ -252,8 +253,8 @@ function defineStorageItem<T>(
252253) : AnalyticsStorageItem < T > {
253254 return {
254255 getValue : async ( ) =>
255- ( await chrome . storage . local . get ( key ) ) [ key ] ?? defaultValue ,
256- setValue : ( newValue ) => chrome . storage . local . set ( { [ key ] : newValue } ) ,
256+ ( await browser . storage . local . get ( key ) ) [ key ] ?? defaultValue ,
257+ setValue : ( newValue ) => browser . storage . local . set ( { [ key ] : newValue } ) ,
257258 } ;
258259}
259260
0 commit comments