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
2223import type { ContextSnapshot } from './contextSnapshotCache' ;
2324import { 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. */
2631const 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 */
164169export 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