Skip to content

Commit 3fb67f6

Browse files
committed
fix: context plugin
1 parent db690e1 commit 3fb67f6

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/store/analyticsSlice.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1-
import { init, setOptOut, track } from '@amplitude/analytics-browser';
1+
import { add, init, setOptOut, track } from '@amplitude/analytics-browser';
22
import { StateCreator } from 'zustand';
33

44
import { 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+
622
const AMPLITUDE_API_KEY = process.env.NEXT_PUBLIC_AMPLITUDE_API_KEY || '';
723

824
export 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

Comments
 (0)