Skip to content

Feature Flags (PostHog)

Feature flags are managed through PostHog to control feature rollouts, A/B testing, and gradual releases.

Setup

PostHog is initialized at the app root in app/_layout.tsx:

tsx
import { PostHogProvider } from "posthog-react-native";
import { POSTHOG_API_KEY } from "@/constants/config";

<PostHogProvider
  apiKey={POSTHOG_API_KEY}
  options={{
    host: "https://us.i.posthog.com",
    enableSessionReplay: true,
    preloadFeatureFlags: true,
    sendFeatureFlagEvent: true,
  }}
>
  {/* App content */}
</PostHogProvider>

Using Feature Flags

Import the useFeatureFlag hook from posthog-react-native:

tsx
import { useFeatureFlag } from "posthog-react-native";

export default function MyComponent() {
  const showNewFeature = useFeatureFlag("new_feature");

  return (
    <>
      {showNewFeature && <NewFeatureComponent />}
    </>
  );
}

Active Feature Flags

FlagPurposeLocation
dark_modeDark theme toggleSettings
push_notificationsPush notification settingsSettings
custom_app_iconCustom app icon selectionSettings
reading_remindersReading reminder notificationsSettings
private_profilePrivate profile optionSettings
export_dataData export functionalitySettings
delete_accountAccount deletion featureSettings
in_app_supportIn-app support/helpSettings
book_clubsBook clubs featureHome, Profile
stickersSticker collection featureProfile
book_reviewsBook review featureBook Detail
book_editionsBook edition selectionBook Detail

Adding a New Feature Flag

  1. Create the flag in PostHog - Go to PostHog dashboard > Feature Flags > New Feature Flag
  2. Use in code - Add the useFeatureFlag hook in your component:
tsx
const showMyFeature = useFeatureFlag("my_feature_flag");
  1. Conditional rendering - Wrap the feature in a conditional:
tsx
{showMyFeature && <MyFeature />}

Event Tracking

Use usePostHog to track events when users interact with features:

tsx
import { usePostHog } from "posthog-react-native";

export default function BookScreen() {
  const posthog = usePostHog();

  const handleAddToLibrary = () => {
    posthog.capture("mark_tbr", {
      isbn13: book.isbn13,
      title: book.title,
    });
  };
}

Tracked Events

EventDescription
onboarding_signup_sentUser initiated signup
user_signed_inUser completed authentication
onboarding_completeUser finished onboarding
mark_tbrBook added to Want to Read
mark_currently_readingBook marked as currently reading
mark_finishedBook marked as finished
mark_favoriteBook marked as favorite
book_sharedUser shared a book
buy_book_clickedUser clicked buy link
reading_goal_createdUser created reading goal
reading_goal_modifiedUser updated reading goal
user_signed_outUser signed out

Screen Tracking

Screens are automatically tracked in the layout files:

tsx
// app/(tabs)/_layout.tsx
import { usePostHog } from "posthog-react-native";

export default function TabLayout() {
  const posthog = usePostHog();
  const pathname = usePathname();
  const params = useGlobalSearchParams();

  useEffect(() => {
    posthog.screen(pathname, params);
  }, [pathname, params]);
}

User Identification

Users are identified after authentication in the auth store:

tsx
posthog.identify(user.id, {
  email: user.email,
  new: !onboardingComplete,
});

Best Practices

  1. Use descriptive flag names - book_reviews not feature_1
  2. Group related flags - Settings flags are all checked together
  3. Include properties in events - Always include relevant context (book ID, title, etc.)
  4. Clean up old flags - Remove flags from code after features are fully rolled out
  5. Test both states - Verify behavior when flag is on and off

Built with VitePress