Refactor analytics enrichment and fix screen tracking initialization - #2127
Merged
Conversation
Web never sent any Amplitude events because initAnalytics is gated behind splashScreenHidden, which is only flipped by onLayoutRootView. That onLayout was attached to SafeAreaProvider, which does not forward an arbitrary onLayout prop to its underlying view on web (it only wires its own inset measurement), so the callback never ran and analytics was never initialized. Native works because the native SafeAreaProvider does forward onLayout. Move onLayout to GestureHandlerRootView, which passes it through to a react-native-web View (fires via ResizeObserver) on web and lays out normally on native. Also gate trackScreen behind a new analyticsReady flag set once initAnalytics resolves, so the first (landing) Page Viewed fires after init instead of being queued before the Amplitude proxy serverUrl is configured and flushed to the wrong endpoint. Enrich the Amplitude Page Viewed event the same way track() does: extract a shared enrichEventParams() helper and use it for screen tracking so UTM/attribution fields land as top-level, queryable properties (utm_source, utm_campaign, ...) and route params are flattened instead of nested under a `params` object. Kept screen tracking on the Amplitude-only path to avoid double-counting in Firebase/GTM. https://claude.ai/code/session_01KR9AG8hkYVigCMsQmDKGS2
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors the analytics event enrichment logic to reduce duplication and fixes a timing issue where screen views were tracked before analytics initialization completed, potentially causing events to be sent to incorrect endpoints.
Key Changes
Extract shared enrichment logic: Created
enrichEventParams()helper function that centralizes attribution, device context, and platform data enrichment. This function is now used by bothtrack()andtrackAmplitudeScreen()to ensure consistent event properties across all event types.Fix screen tracking initialization: Added
analyticsReadystate that gates screen view tracking untilinitAnalytics()completes. This prevents early screen views from being queued before the Amplitude SDK is properly configured with proxy/serverUrl settings on web.Flatten route params in screen events: Modified
trackAmplitudeScreen()to flatten route params into top-level properties (matchingtrack()behavior), making utm_source, utm_campaign, and other attribution data directly queryable in Amplitude instead of nested in a params object.Reorganize layout component: Moved
onLayoutcallback fromSafeAreaProvidertoGestureHandlerRootViewand addedflex: 1style to ensure proper layout handling.Implementation Details
enrichEventParams()function consolidates all enrichment logic that was previously duplicated in thetrack()function, improving maintainability.analyticsReadydependency in the screen tracking effect ensures the effect re-fires once initialization completes, capturing the initial route with proper context.https://claude.ai/code/session_01KR9AG8hkYVigCMsQmDKGS2