iOS experiments installation

  1. Install via CocoaPods

    Required

    Add PostHog to your Podfile:

    Podfile
    pod "PostHog", "~> 3.0"

    Or install via Swift Package Manager:

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

    Required

    Initialize PostHog in your AppDelegate:

    AppDelegate.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>"
    let POSTHOG_HOST = "https://us.i.posthog.com"
    let config = PostHogConfig(apiKey: POSTHOG_API_KEY, host: POSTHOG_HOST)
    PostHogSDK.shared.setup(config)
    return true
    }
    }
  3. Implement your experiment

    Required

    Experiments run on top of our feature flags. You can define which version of your code runs based on the return value of the feature flag:

    if (PostHogSDK.shared.getFeatureFlag("your-experiment-feature-flag") as? String == "test") {
    // do something
    } else {
    // It's a good idea to let control variant always be the default behaviour,
    // so if something goes wrong with flag evaluation, you don't break your app.
    }
  4. Run your experiment

    Required

    Once you've implemented the feature flag in your code, you'll enable it for a target audience by creating a new experiment in the PostHog dashboard.

  5. Next steps

    Recommended

    ResourceDescription
    Creating an experimentHow to create an experiment in PostHog
    Adding experiment codeHow to implement experiments for all platforms
    Statistical significanceUnderstanding when results are meaningful
    Experiment insightsHow to analyze your experiment data
    More tutorialsOther real-world examples and use cases

Community questions

Was this page useful?

Questions about this page? or post a community question.