Analytics
This application uses PostHog for analytics tracking. PostHog is configured at the root level and ready to track user events throughout the app.
Configuration
components/layout/rootLayoutNavigation.tsx
<PostHogProvider
apiKey={process.env.EXPO_PUBLIC_POSTHOG_API_KEY}
autocapture={{
captureScreens: false, // Disable screen capture
captureTouches: false, // Disable touch capture
}}
options={{
enableSessionReplay: false, // Disable session replay
sessionReplayConfig: {
maskAllTextInputs: true, // Mask text inputs
maskAllImages: true, // Mask images
maskAllSandboxedViews: true, // Mask sandboxed views
},
}}
>Current Settings:
- Screen capture: Disabled
- Touch capture: Disabled
- Session replay: Disabled
- Privacy: Text inputs, images, and views are masked
Environment Setup
Add your PostHog API key to .env:
EXPO_PUBLIC_POSTHOG_API_KEY=your_posthog_api_key_hereGet your API key from PostHog Dashboard → Project Settings → API Keys.
Usage
Track Events
import { usePostHog } from 'posthog-react-native';
function MyComponent() {
const posthog = usePostHog();
const handleButtonPress = () => {
posthog?.capture('button_clicked', {
button_name: 'sign_in',
screen: 'auth',
});
};
return <Button onPress={handleButtonPress} />;
}Identify Users
import { usePostHog } from 'posthog-react-native';
function LoginScreen() {
const posthog = usePostHog();
const handleLogin = async (user) => {
posthog?.identify(user.id, {
email: user.email,
name: user.name,
});
};
}Track Screen Views
import { usePostHog } from 'posthog-react-native';
import { useFocusEffect } from '@react-navigation/native';
function HomeScreen() {
const posthog = usePostHog();
useFocusEffect(() => {
posthog?.screen('home_screen');
});
}Reset on Logout
import { usePostHog } from 'posthog-react-native';
function LogoutButton() {
const posthog = usePostHog();
const handleLogout = () => {
posthog?.reset();
};
}Common Event Examples
// User actions
posthog?.capture('user_signed_in', { method: 'google' });
posthog?.capture('user_signed_out');
posthog?.capture('subscription_purchased', { plan: 'premium' });
// Feature usage
posthog?.capture('feature_used', { feature_name: 'search' });
posthog?.capture('button_clicked', { button: 'submit' });
// Errors
posthog?.capture('error_occurred', { error_type: 'network' });PostHog Hook
The usePostHog hook is available throughout the app:
import { usePostHog } from 'posthog-react-native';
const posthog = usePostHog();Note: posthog may be null if PostHog is not initialized, always use optional chaining: posthog?.capture(...)
To enable session replay or autocapture, update the configuration in rootLayoutNavigation.tsx.
More details can be found in the PostHog React Native SDK Documentation .