This guide explains how to set up and use Firebase Analytics in your React Native Expo app.
The following packages have been installed:
@react-native-firebase/app- Core Firebase SDK@react-native-firebase/analytics- Firebase Analytics module
- Go to the Firebase Console
- Create a new project or select an existing one
- Add your iOS and Android apps to the project
- Download the configuration files:
google-services.jsonfor Android (place inandroid/app/)GoogleService-Info.plistfor iOS (place inios/YourAppName/)
Edit firebase.config.js with your actual Firebase project configuration:
const firebaseConfig = {
apiKey: "your-actual-api-key",
authDomain: "your-project-id.firebaseapp.com",
projectId: "your-actual-project-id",
storageBucket: "your-project-id.appspot.com",
messagingSenderId: "your-actual-sender-id",
appId: "your-actual-app-id",
measurementId: "your-actual-measurement-id"
};The app.json has been updated to include the Firebase plugin:
{
"plugins": [
"expo-router",
"@react-native-firebase/app"
]
}Use the AnalyticsService class from services/analytics.ts for all analytics operations:
import { AnalyticsService, AnalyticsEvents } from '@/services/analytics';
// Log a custom event
AnalyticsService.logEvent('button_click', {
button_name: 'login_button',
screen_name: 'login_screen'
});
// Log screen views
AnalyticsService.setCurrentScreen('HomeScreen', 'HomeScreen');
// Set user properties
AnalyticsService.setUserProperties({
user_type: 'premium',
preferred_language: 'en'
});
// Set user ID
AnalyticsService.setUserId('user123');Use the AnalyticsEvents constants for common events:
// Available events
AnalyticsEvents.SCREEN_VIEW
AnalyticsEvents.LOGIN
AnalyticsEvents.SIGN_UP
AnalyticsEvents.PURCHASE
AnalyticsEvents.SEARCH
AnalyticsEvents.SHARE
AnalyticsEvents.SELECT_CONTENT
AnalyticsEvents.APP_OPEN
AnalyticsEvents.TUTORIAL_BEGIN
AnalyticsEvents.TUTORIAL_COMPLETESee app/(tabs)/index.tsx for an example of how to implement analytics in a screen:
import { useEffect } from 'react';
import { AnalyticsService, AnalyticsEvents } from '@/services/analytics';
export default function MyScreen() {
useEffect(() => {
// Log screen view
AnalyticsService.setCurrentScreen('MyScreen', 'MyScreen');
// Log custom event
AnalyticsService.logEvent(AnalyticsEvents.SCREEN_VIEW, {
screen_name: 'MyScreen',
screen_class: 'MyScreen'
});
}, []);
// Component JSX...
}For development builds with Firebase Analytics:
# Create development build
expo build:android --type apk
expo build:ios --type simulatorFor production builds:
# Android
expo build:android --type app-bundle
# iOS
expo build:ios --type archiveYou can enable/disable analytics collection:
// Disable analytics collection
AnalyticsService.setAnalyticsCollectionEnabled(false);
// Enable analytics collection
AnalyticsService.setAnalyticsCollectionEnabled(true);To reset all analytics data:
AnalyticsService.resetAnalyticsData();To enable debug mode for analytics (development only):
# Android
adb shell setprop debug.firebase.analytics.app your.package.name
# iOS - Add to your app's arguments in Xcode:
-FIRAnalyticsDebugEnabled- Events appear in the Firebase Console under Analytics > Events
- Real-time events can be viewed in Analytics > DebugView (when debug mode is enabled)
- Custom events may take up to 24 hours to appear in standard reports
- Event Naming: Use descriptive, consistent event names
- Parameter Limits: Keep event parameters under 25 per event
- Privacy: Always respect user privacy and comply with regulations
- Performance: Analytics calls are asynchronous and won't block UI
- Testing: Test analytics in development builds before production
- Events not appearing: Check Firebase configuration and ensure app is properly connected
- Build errors: Ensure all Firebase configuration files are in the correct locations
- iOS build issues: Verify
GoogleService-Info.plistis added to the Xcode project