iOS experiments installation

  1. Install PostHog iOS SDK

    Required

    PostHog is available through CocoaPods or you can add it as a Swift Package Manager based dependency.

    CocoaPods

    Podfile
    pod "PostHog", "~> 3.0"

    Swift Package Manager

    Add PostHog as a dependency in your Xcode project "Package Dependencies" and select the project target for your app, as appropriate.

    For a Swift Package Manager based project, add PostHog as a dependency in your Package.swift file's Package dependencies section:

    Package.swift
    dependencies: [
    .package(url: "https://github.com/PostHog/posthog-ios.git", from: "3.0.0")
    ],

    and then as a dependency for the Package target utilizing PostHog:

    Package.swift
    .target(
    name: "myApp",
    dependencies: [.product(name: "PostHog", package: "posthog-ios")]),

    Configuration

    Configuration is done through the PostHogConfig object. Here's a basic configuration example to get you started.

    You can find more advanced configuration options in the configuration page.

    Swift
    import Foundation
    import PostHog
    import UIKit
    class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
    let POSTHOG_API_KEY = "<ph_project_api_key>"
    // usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com'
    let POSTHOG_HOST = "https://us.i.posthog.com"
    let config = PostHogConfig(apiKey: POSTHOG_API_KEY, host: POSTHOG_HOST)
    PostHogSDK.shared.setup(config)
    return true
    }
    }

  2. Capture conversion event

    Required

    Once PostHog is initialized, you should be able to capture events. For your experiment to be meaningful, we need to capture an event that we want to measure, such as a conversion event.

    For this tutorial, we can track a simple button click on a CTA as a conversion event.

    Swift
    import PostHogSDK
    struct SomeView: View {
    var body: some View {
    Button("Call to action text") {
    PostHogSDK.shared.capture("cta clicked")
    }
    }
    }
  3. Validate PostHog events

    Checkpoint
    Confirm events are being sent to PostHog

    Before proceeding, let's make sure events are being captured and sent to PostHog. You should see cta clicked events appear in the Activity feed.

    Check for events in PostHog

  4. Create an experiment

    Required

    Go to the Experiments tab in the PostHog app and click on the New experiment button in the top right.

    Create experiment

    For this tutorial, let's create a new experiment using simplified test values:

    • Name: "Test experiment"
    • Description: "This is a test experiment"
    • Feature flag key: "test-experiment-ff-key"
    • Experiment type: "Feature flag"
    • Variants: "control" and "test"
    • Participant type: "Users"

    Then click Save as draft.

  5. Add primary metric and launch

    Required

    Scroll down to the Primary metrics section and click + Add primary metric.

    Choose Single-use and select Type > Mean.

    Then search for the event cta clicked under Metric and click Save.

    Add primary metric

    By default, experiments are exposed to 100% of users. You can customize release conditions to expose the experiment to a subset of users.

    For this tutorial, we'll ship the experiment to all users and click Launch in the top right.

  6. Call feature flag

    Required

    Use the PostHog SDK to call the experiment flag and update how your app renders based on the assigned variant.

    Swift
    import PostHogSDK
    struct SomeView: View {
    var body: some View {
    if PostHogSDK.shared.getFeatureFlag("your-experiment-key") as? String == "test" {
    Button("Test variant CTA") {
    PostHogSDK.shared.capture("cta clicked")
    }
    } else {
    Button("Control variant CTA") {
    PostHogSDK.shared.capture("cta clicked")
    }
    }
    }
    }

    Now when a user triggers a cta clicked event, PostHog automatically assigns the user to a variant and records an experiment exposure.

    By default, users are split equally between variants. If you want to assign specific users to a specific variant, see more about distribution and release conditions.

  7. Validate feature flag calls

    Checkpoint

    Make sure exposures and feature flag calls are being sent to PostHog. You should see $feature_flag_called events appear in the Activity feed.

    Check for events in PostHog

  8. Evaluate experiment results

    Recommended

    As you capture more cta clicked events, more exposures will populate the primary metrics in your experiment.

    Evaluate experiment metrics

    With enough data, you can analyze the experiment and its variants by:

    • Conversion rates
    • Statistical significance
    • Credible intervals
    • Chance to win %
    • Minimum detectable effect
    • And more

Community questions

Was this page useful?

Questions about this page? or post a community question.