ScaleRocket/Mobile

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

ProviderFree TierBest For
PostHog1M events/monthProduct analytics, feature flags
Firebase AnalyticsUnlimitedGoogle ecosystem, crash reporting
Mixpanel20M events/monthFunnel analysis, retention
Amplitude10M events/monthProduct intelligence

PostHog provides product analytics, session replay, and feature flags in one tool:

npx expo install posthog-react-native

Setup

// 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/analytics
import 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:

EventPropertiesWhen
app_openedsourceApp launched
screen_viewedscreen_nameScreen becomes visible
sign_up_completedmethod (email, google, apple)User creates account
sign_in_completedmethodUser logs in
purchase_completedplan, priceSubscription purchased
feature_usedfeature_nameKey feature interaction
error_occurrederror_type, screenError 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} />;
}

On this page