π Analytics with PostHog
Shipnative integrates PostHog for powerful product analytics, session recording, and feature flagging. This guide will help you set up PostHog to understand user behavior and roll out features effectively.PostHog Project Setup
1. Create Your PostHog Project
If you havenβt already, create a new project on the PostHog platform:- Go to https://posthog.com/ and sign up.
- Create a new project.
2. Obtain API Key and Host
Once your project is ready, youβll need your API key and host URL:- In your PostHog project dashboard, navigate to Project Settings > API Keys.
- Copy your Project API Key.
- Note your PostHog Host (e.g.,
https://app.posthog.comor your self-hosted instance URL).
3. Configure Environment Variables
During theyarn setup process, you were prompted to enter these credentials. If you skipped this or need to update them, you can manually add them to your apps/app/.env file:
Using PostHog in Your App
Shipnative provides a convenientuseAnalytics hook and utility functions for interacting with PostHog.
Using the useAnalytics Hook (Recommended)
The useAnalytics hook (found in apps/app/app/hooks/useAnalytics.ts) simplifies common analytics tasks.
Using Utility Functions
For direct event tracking or identification outside of React components, you can use the utility functions:Direct PostHog Client Access
For advanced usage, you can directly access the PostHog client instance:Key PostHog Features
Event Tracking
Track user interactions and custom events within your app.For more on event tracking, see the PostHog Event Tracking documentation.
Screen Tracking
Record which screens users visit and how they navigate.User Identification
Associate events with specific users to build user profiles.Learn more about identifying users in the PostHog Identify documentation.
Feature Flags
Control feature rollouts and A/B test new functionalities.For detailed information on feature flags, visit the PostHog Feature Flags documentation.
Groups (B2B Analytics)
If your app serves businesses, you can track groups (e.g., companies, teams).Explore group analytics in the PostHog Group Analytics documentation.
Testing Your Integration
Mock Mode Testing
- Ensure PostHog API keys are not set in your
apps/app/.envfile. - Run your app (
yarn iosoryarn android). - Trigger some events or screen views.
- Check your console logs for messages like
π [MockPostHog] Event: button_clicked, indicating that mock events are being captured.
Testing with Real Services
- Add your
EXPO_PUBLIC_POSTHOG_API_KEYandEXPO_PUBLIC_POSTHOG_HOSTtoapps/app/.env. - Run your app.
- Trigger events and screen views.
- Log in to your PostHog dashboard and navigate to:
- Events: To see all captured events.
- Persons: To see identified users and their properties.
- Feature Flags: To test your feature flag configurations.
Best Practices
Event Naming
- β
Good:
button_clicked,product_viewed,checkout_completed(usesnake_case) - β Bad:
ButtonClicked(use snake_case),click(too generic),user clicked the submit button on the checkout page(too verbose)
Event Properties
- β
Good: Include relevant, flat properties.
- β Bad: Avoid deep nesting or redundant properties.
Privacy
- Donβt track PII (Personally Identifiable Information) without explicit user consent.
- Anonymize sensitive data (e.g., credit card numbers, passwords).
- Respect user preferences for opting out of tracking.
Troubleshooting
Events not showing in PostHog
Problem: Events are tracked in your app but not appearing in the PostHog dashboard. Solution:- Double-check that your
EXPO_PUBLIC_POSTHOG_API_KEYandEXPO_PUBLIC_POSTHOG_HOSTare correct inapps/app/.env. - Verify your device has network connectivity.
- Ensure your PostHog project is active and correctly configured.
- Allow a few minutes for events to process and appear in the dashboard.
- Check your browserβs developer console or device logs for any errors related to PostHog.
Mock mode in production
Problem: Your app is using mock PostHog services in production. Solution:- Verify that your
apps/app/.envfile exists and is correctly configured withEXPO_PUBLIC_POSTHOG_API_KEYandEXPO_PUBLIC_POSTHOG_HOST. - Ensure your environment variables are prefixed with
EXPO_PUBLIC_. - Restart your Metro bundler with
yarn app:start --clearto ensure environment variables are reloaded. - Review your build configuration to ensure
.envvariables are correctly bundled.