Analytics
Mobile analytics with PostHog React Native, Firebase Analytics, and event tracking patterns.
Analytics
Track user behavior in your mobile app to understand engagement, conversion, and feature usage. ScaleRocket Mobile supports multiple analytics providers.
Choosing a Provider
| Provider | Free Tier | Best For |
|---|---|---|
| PostHog | 1M events/month | Product analytics, feature flags |
| Firebase Analytics | Unlimited | Google ecosystem, crash reporting |
| Mixpanel | 20M events/month | Funnel analysis, retention |
| Amplitude | 10M events/month | Product intelligence |
PostHog (Recommended)
PostHog provides product analytics, session replay, and feature flags in one tool:
npx expo install posthog-react-nativeSetup
// lib/analytics.ts
import PostHog from "posthog-react-native";
export const posthog = new PostHog("phc_your_api_key", {
host: "https://app.posthog.com",
enableSessionReplay: true,
});Wrap your app with the PostHog provider:
// app/_layout.tsx
import { PostHogProvider } from "posthog-react-native";
import { posthog } from "@/lib/analytics";
export default function RootLayout() {
return (
<PostHogProvider client={posthog}>
<Slot />
</PostHogProvider>
);
}Track Events
import { usePostHog } from "posthog-react-native";
function CheckoutScreen() {
const posthog = usePostHog();
const handlePurchase = async () => {
posthog.capture("purchase_completed", {
plan: "pro",
price: 9.99,
currency: "USD",
});
};
return <Button title="Subscribe" onPress={handlePurchase} />;
}Identify Users
Link events to a user after authentication:
function useIdentifyUser() {
const posthog = usePostHog();
const { user } = useAuth();
useEffect(() => {
if (user) {
posthog.identify(user.id, {
email: user.email,
name: user.name,
plan: user.plan,
});
}
}, [user]);
}Firebase Analytics
npx expo install @react-native-firebase/app @react-native-firebase/analyticsimport analytics from "@react-native-firebase/analytics";
// Track screen views
await analytics().logScreenView({
screen_name: "Dashboard",
screen_class: "DashboardScreen",
});
// Track custom events
await analytics().logEvent("purchase_completed", {
plan: "pro",
price: 9.99,
});
// Identify user
await analytics().setUserId(user.id);
await analytics().setUserProperties({ plan: "pro" });Note: Firebase Analytics requires a development build with native modules.
Event Tracking Patterns
Track these key events for a SaaS mobile app:
| Event | Properties | When |
|---|---|---|
app_opened | source | App launched |
screen_viewed | screen_name | Screen becomes visible |
sign_up_completed | method (email, google, apple) | User creates account |
sign_in_completed | method | User logs in |
purchase_completed | plan, price | Subscription purchased |
feature_used | feature_name | Key feature interaction |
error_occurred | error_type, screen | Error shown to user |
Screen Tracking
Automatically track screen views with Expo Router:
import { usePathname } from "expo-router";
function useScreenTracking() {
const pathname = usePathname();
const posthog = usePostHog();
useEffect(() => {
posthog.screen(pathname);
}, [pathname]);
}Add this to your root layout:
export default function RootLayout() {
useScreenTracking();
return <Slot />;
}Privacy Considerations
- Allow users to opt out of analytics in settings
- Don't track personally identifiable information (PII) without consent
- Comply with GDPR/CCPA — provide a way to delete user data
- Use anonymous IDs until the user explicitly identifies themselves
function AnalyticsOptOut() {
const [optedOut, setOptedOut] = usePersistentState("analytics-opt-out", false);
const toggle = () => {
const newValue = !optedOut;
setOptedOut(newValue);
if (newValue) {
posthog.optOut();
} else {
posthog.optIn();
}
};
return <Switch value={!optedOut} onValueChange={toggle} />;
}