Skip to content

Commit 8ec996f

Browse files
authored
chore(dapp): set up test enviroment for amplitude (#1133)
* set up test enviroment for amplitude * apply suggestions * fix custom events
1 parent 98f302d commit 8ec996f

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

dapp/src/lib/utils/analytics/plugins/contextEnrichmentPlugin.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
/**
55
* Context Enrichment Plugin for Amplitude Analytics
66
*
7-
* Enriches analytics events with UI context (page URL, dialog title, etc.).
7+
* Enriches analytics events with UI context (page URL, dialog title, etc.) and
8+
* handles environment-based event prefixing for development/production separation.
89
*
910
* Uses event capture phase to snapshot UI state before handlers modify the DOM,
1011
* solving the timing issue where analytics events are processed after UI changes.
@@ -22,6 +23,10 @@ import type { BrowserConfig, EnrichmentPlugin, Event } from '@amplitude/analytic
2223
import type { ContextSnapshot } from './contextSnapshotCache';
2324
import { contextSnapshotCache } from './contextSnapshotCache';
2425

26+
// Environment detection
27+
const IS_DEVELOPMENT = process.env.NEXT_PUBLIC_BUILD_ENV !== 'production';
28+
const DEV_EVENT_PREFIX = 'dev_';
29+
2530
/** Page paths mapped to custom Amplitude event names. */
2631
const PAGE_VIEW_EVENTS_MAP: Record<string, string> = {
2732
'/': 'Landing Page Viewed',
@@ -158,8 +163,8 @@ function enrichEventWithContext(eventProperties: EnrichedEventProperties): Enric
158163
}
159164

160165
/**
161-
* Amplitude enrichment plugin that adds UI context to all events.
162-
* Enriches events with page URL, dialog title, and custom event names.
166+
* Amplitude enrichment plugin that adds UI context to all events and handles environment separation.
167+
* Enriches events with page URL, dialog title, custom event names, and development prefixing.
163168
*/
164169
export function contextEnrichmentPlugin(): EnrichmentPlugin {
165170
let cleanupContextCapture: (() => void) | null = null;
@@ -206,6 +211,15 @@ export function contextEnrichmentPlugin(): EnrichmentPlugin {
206211

207212
const filteredProperties = filterProperties(enrichedEventProperties);
208213

214+
// Environment handling - add prefix
215+
if (
216+
IS_DEVELOPMENT &&
217+
event.event_type &&
218+
!event.event_type.startsWith(DEV_EVENT_PREFIX)
219+
) {
220+
event.event_type = DEV_EVENT_PREFIX + event.event_type;
221+
}
222+
209223
return {
210224
...event,
211225
event_properties: filteredProperties,

0 commit comments

Comments
 (0)