1- import { init , setOptOut , track } from '@amplitude/analytics-browser' ;
1+ import { add , init , setOptOut , track } from '@amplitude/analytics-browser' ;
22import { StateCreator } from 'zustand' ;
33
44import { RootStore } from './root' ;
55
6+ // Plugin to ensure all events have app_context
7+ const createAppContextPlugin = ( context : string ) => ( {
8+ name : 'app-context-plugin' ,
9+ type : 'enrichment' as const ,
10+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11+ execute : async ( event : any ) => {
12+ return {
13+ ...event ,
14+ event_properties : {
15+ ...event . event_properties ,
16+ app_context : context ,
17+ } ,
18+ } ;
19+ } ,
20+ } ) ;
21+
622const AMPLITUDE_API_KEY = process . env . NEXT_PUBLIC_AMPLITUDE_API_KEY || '' ;
723
824export type TrackEventProperties = {
@@ -45,7 +61,7 @@ export const createAnalyticsSlice: StateCreator<
4561 walletAddress : get ( ) . account ,
4662 market : properties . market ?? get ( ) . currentMarket ,
4763 walletType : get ( ) . walletType ,
48- app_context : 'app' ,
64+ app_context : 'app' , // Fallback in case plugin doesn't apply
4965 } ;
5066
5167 try {
@@ -71,14 +87,14 @@ export const createAnalyticsSlice: StateCreator<
7187 if ( ! isInitialized ) {
7288 init ( AMPLITUDE_API_KEY , {
7389 // serverZone: 'EU',
74-
7590 autocapture : true , // disable if we don't want to capture every click and page view on the site
7691 trackingOptions : {
7792 ipAddress : false ,
7893 language : true ,
7994 platform : true ,
8095 } ,
8196 } ) ;
97+ add ( createAppContextPlugin ( 'app' ) ) ;
8298 set ( { eventsTrackingInitialized : true } ) ;
8399 }
84100
@@ -95,6 +111,7 @@ export const createAnalyticsSlice: StateCreator<
95111 platform : false ,
96112 } ,
97113 } ) ;
114+ add ( createAppContextPlugin ( 'app' ) ) ;
98115 set ( { eventsTrackingInitialized : true } ) ;
99116 }
100117
0 commit comments