Draft
Conversation
# Vercel Web Analytics Integration - Implementation Report
## Overview
Successfully implemented and enhanced Vercel Web Analytics integration in the rankq React Native project for automatic page view tracking and custom event support.
## What Was Done
### Existing Setup Verified
The following Vercel Web Analytics setup was already in place:
1. **Package Installed**: `@vercel/analytics` (v1.6.1) in package.json
2. **Analytics Service**: `/src/services/analytics.ts` with core functions:
- `initializeAnalytics()` - Initializes the analytics script
- `trackPageView(path, title)` - Tracks page views
- `trackEvent(name, data)` - Tracks custom events
3. **App Initialization**: `App.tsx` calls `initializeAnalytics()` on startup via `useEffect`
### Enhancement Implemented
Enhanced the React Navigation integration to automatically track route changes:
**Modified File: `src/navigation/AppNavigator.tsx`**
- Added imports for `useRef` from React and `trackPageView` from analytics service
- Added `ref` to `NavigationContainer` component
- Implemented `onReady` callback to capture initial route on app startup
- Implemented `onStateChange` callback to:
- Detect route changes
- Call `trackPageView()` when navigation occurs
- Avoid tracking duplicate route transitions
## Technical Details
### Route Tracking Flow
1. When the app launches, `onReady()` captures the initial route name
2. When users navigate between screens (Landing → Leaderboard or vice versa), `onStateChange()` is triggered
3. For each unique route change, `trackPageView()` is called with:
- `path`: Route formatted as `/{routeName}` (e.g., `/Landing`, `/Leaderboard`)
- `title`: The route name itself for better tracking clarity
4. Route name is cached in `routeNameRef` to prevent duplicate tracking
### Implementation Benefits
- **Automatic Route Tracking**: No need to manually add tracking to every screen
- **Development & Production Modes**: Analytics respects `NODE_ENV` setting
- **Error Handling**: All analytics calls wrapped in try-catch blocks to prevent app crashes
- **React Native Compatible**: Uses `inject()` function from `@vercel/analytics` suitable for web/Expo
- **TypeScript Safe**: Full type safety with proper TypeScript annotations
## Files Modified
1. `src/navigation/AppNavigator.tsx` - Added route tracking with React Navigation integration
## Files Not Modified (Already Complete)
1. `src/services/analytics.ts` - Core analytics service (already well-implemented)
2. `App.tsx` - Already initializes analytics properly
3. `package.json` - Already includes `@vercel/analytics` dependency
## Verification Steps Completed
✅ TypeScript compilation successful (no type errors)
✅ App can start without errors (verified with `npm run web`)
✅ All changes follow existing code patterns and conventions
✅ No breaking changes to existing functionality
## How It Works Post-Deployment
1. **Enable in Vercel Dashboard**: Project admin enables Web Analytics from project settings
2. **New Routes Created**: After deployment, Vercel creates `/_vercel/insights/*` routes
3. **Automatic Tracking**: Every route change in the app automatically sends page view events
4. **Data Available**: Analytics data becomes visible in Vercel dashboard after user visits
5. **Custom Events**: Teams can use `trackEvent()` function for additional analytics
## Next Steps for Users
- Deploy app to Vercel using `vercel deploy`
- Enable Web Analytics in Vercel project settings
- Monitor analytics in the Vercel dashboard
- Optionally add custom event tracking using `trackEvent()` for specific user interactions
## Notes
- Route support is now enabled (addresses the documentation requirement for route tracking)
- The implementation is suitable for both web and mobile (Expo) platforms
- No additional dependencies were required
- All error handling prevents analytics failures from affecting app stability
Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Contributor
Author
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Vercel Web Analytics Integration - Implementation Report
Overview
Successfully implemented and enhanced Vercel Web Analytics integration in the rankq React Native project for automatic page view tracking and custom event support.
What Was Done
Existing Setup Verified
The following Vercel Web Analytics setup was already in place:
@vercel/analytics(v1.6.1) in package.json/src/services/analytics.tswith core functions:initializeAnalytics()- Initializes the analytics scripttrackPageView(path, title)- Tracks page viewstrackEvent(name, data)- Tracks custom eventsApp.tsxcallsinitializeAnalytics()on startup viauseEffectEnhancement Implemented
Enhanced the React Navigation integration to automatically track route changes:
Modified File:
src/navigation/AppNavigator.tsxuseReffrom React andtrackPageViewfrom analytics servicereftoNavigationContainercomponentonReadycallback to capture initial route on app startuponStateChangecallback to:trackPageView()when navigation occursTechnical Details
Route Tracking Flow
onReady()captures the initial route nameonStateChange()is triggeredtrackPageView()is called with:path: Route formatted as/{routeName}(e.g.,/Landing,/Leaderboard)title: The route name itself for better tracking clarityrouteNameRefto prevent duplicate trackingImplementation Benefits
NODE_ENVsettinginject()function from@vercel/analyticssuitable for web/ExpoFiles Modified
src/navigation/AppNavigator.tsx- Added route tracking with React Navigation integrationFiles Not Modified (Already Complete)
src/services/analytics.ts- Core analytics service (already well-implemented)App.tsx- Already initializes analytics properlypackage.json- Already includes@vercel/analyticsdependencyVerification Steps Completed
✅ TypeScript compilation successful (no type errors)
✅ App can start without errors (verified with
npm run web)✅ All changes follow existing code patterns and conventions
✅ No breaking changes to existing functionality
How It Works Post-Deployment
/_vercel/insights/*routestrackEvent()function for additional analyticsNext Steps for Users
vercel deploytrackEvent()for specific user interactionsNotes
View Project · Web Analytics
Created by itschirag57 with Vercel Agent
Summary by cubic
Add automatic page view tracking with Vercel Web Analytics in React Navigation. Captures the initial route and tracks route changes without duplicates for cleaner analytics.
Written for commit a3b2ade. Summary will update on new commits.