Launch offer for the first 500 accounts: $10K MTR free for 24 months + $100 credit →
    .io

    PostHog

    Overview

    The PostHog connector sends subscription lifecycle events and their revenue from BillDog’s servers to your PostHog project. Your app doesn’t send anything for it, so purchases made through BillDog show up in PostHog even if the app is closed when a renewal or refund happens.

    The SDK’s setPosthogDistinctID setter stores PostHog’s distinct_id on the BillDog customer under the reserved attribution key posthogDistinctId. The connector sends events to that person. The value also comes back in the customer’s attribution data (customer info). Setting it never clears another provider’s ID.

    Connect PostHog

    1. Open Dashboard → Integrations and configure PostHog.
    2. Paste your PostHog project API key (phc_…, under Project settings). BillDog stores it encrypted.
    3. Pick the region: US Cloud (us.i.posthog.com), EU Cloud (eu.i.posthog.com) or Self-hosted, where you enter your instance’s https URL.
    4. Choose the events to forward and save. Test Connection sends a test_event. PostHog accepts a capture request before it checks the key, so look for the test event in your project’s Activity to confirm the key.

    What BillDog sends

    One event per lifecycle fact, posted to PostHog’s /batch/ capture endpoint under BillDog’s event names: subscription_created, purchase_completed (a renewal or a one-time purchase), subscription_cancelled, subscription_uncancelled, subscription_expired and purchase_refunded.

    • distinct_id: the PostHog distinct ID stored with setPosthogDistinctID; if there isn’t one, your app user ID; if there’s no app user ID, the BillDog customer ID.
    • Revenue: revenue in USD with currency: "USD" on purchases and renewals, the same amount negative on refunds, and none on other events. revenue_type says purchase, renewal or refund. The price the customer paid in their own currency is in price_local and local_currency.
    • Other properties: product_id, transaction_id, store, platform, period_type, is_trial, is_sandbox, entitlement_id, app_user_id and billdog_customer_id when the event has them. Email addresses and names are never sent.
    • Each event has a uuid derived from the BillDog event, so a retried delivery is the same PostHog event.
    • Sandbox purchases are sent with is_sandbox: true. Filter on it in PostHog, or leave sandbox testing to a separate BillDog project.

    A network error or a 5xx answer is retried twice. Each attempt shows in the integration’s Recent Activity, and three failures within an hour mark the integration as erroring and email the project owner.

    Store the distinct ID

    Read the distinct ID from the PostHog SDK and hand it to BillDog once BillDog has loaded customer info. With no current customer the setter fails: iOS throws, Android returns a failed Result, and React Native drops it without reporting anything.

    // iOS — setter is async throws
    import BillDogFull
    import PostHog
    
    Task {
        _ = try await BillDog.shared.getCustomerInfo()
        try await BillDog.shared.setPosthogDistinctID(PostHogSDK.shared.getDistinctId())
    }
    // Android — suspend function returning Result<Unit>
    import io.billdog.core.BillDog
    import com.posthog.PostHog
    
    scope.launch {
        BillDog.getCustomerInfo()
        BillDog.getInstance().setPosthogDistinctID(PostHog.distinctId())
    }
    // React Native — fire-and-forget, returns void
    import { BillDog } from '@billdog.io/react-native';
    
    BillDog.setPosthogDistinctID(posthog.getDistinctId());

    After identify

    PostHog’s identify() changes the distinct ID from the anonymous one to the known one. BillDog stores whatever value it was last given, so if you want the stored value to match, call the setter again after you identify a user.

    // After login, in the same place you call PostHog identify()
    PostHogSDK.shared.identify(userId)
    Task { try? await BillDog.shared.setPosthogDistinctID(PostHogSDK.shared.getDistinctId()) }

    Consent gating

    The setter is gated by the SDK’s consent flag. If your app has called setConsentRequired(true) and consent hasn’t been given, the setter fails (iOS throws; Android returns a failed Result). Nothing is queued and nothing leaves the device. Call it again after setConsentGiven(true).

    Related

    Was this page helpful?

    Or rate this page:

    We use cookies and process your data to provide our services. By continuing, you agree to our Terms and Privacy Policy.