A/B testing installation

Last updated:

|Edit this page

A/B testing enables you to test the impact of product changes and understand how they affect your users' behaviour. For example:

  • How changes to your onboarding flow affect your signup rate.
  • If different designs of your app's dashboard increase user engagement and retention.
  • The impact a free trial period versus money-back guarantee to determine which results in more customers.

A/B tests are also referred to as "experiments", and this is how we refer to them in the PostHog app.

To start using A/B tests, install PostHog with the library you want to run tests in (if you haven't already):

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

CocoaPods

Podfile
pod "PostHog", "~> 3.0.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

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
}
}

Questions? Ask Max AI.

It's easier than reading through 580 docs articles.

Community questions

  • Bram
    5 days ago

    Excluding anonymous users from other experiments

    Is it possible to run two different experiments for anonymous / unidentified users next to each other that can in some way be excluded so they don't influence each other ?

  • Yarik
    3 months ago

    Tests results Error

    Hi, I’m running a few A/B tests. I checked them a few months ago and got some results. Now, I’m revisiting the A/B tests and see that they are running, but in the Results section, I encounter this error. This error occurs in 2 out of 3 tests. How can I retrieve the results?

    Experiment results could not be calculated
    no viable alternative at input 'SELECT\n countIf(ifNull(equals(step_reached, 0), 0)) AS step_1,\n ,'
  • Suraj
    6 months ago

    User seeing different experiment variants on app restart

    We have integrated the A/B testing along with Analytics although we are not sending any analytics events at the moment. While testing we saw that if a user reopens the app the experiment variants being shown are different at times. Also in the people section we saw that whenever app is restarted a new person is created. What can be the issue here?

    • Suraj
      Author6 months ago

      It was a bug in the app all good here

  • Yousuf
    7 months ago

    Issue with A/B Testing

    Hi Steven, we have implemented A/B testing through PostHog and can see data being tracked in the PostHog dashboard.

    However, when we load the website, everyone sees the variant A although my distribution and rollout is set to 50/50 and no one really sees the variant B.

    Can you please look into this issue for me and let me know what I am doing wrong here?

    I look forward to hearing back from you soon.

    Thanks

  • Václav
    a year ago

    How do I set up an A/B test?

    This page only shows how to add a js snippet on the page. But how do I set up an A/B test??

Was this page useful?

Next article

Creating an experiment

To create a new experiment, go to the A/B testing tab in the PostHog app, and click on the New experiment button in the top right. This presents you with a form where you can complete the details of your new experiment: Here's a breakdown of each field in the form: Feature flag key Each experiment is backed by a feature flag . In your code, you use this feature flag key to check which experiment variant the user has been assigned to. You can use an existing feature flag if it has multiple…

Read next article

PostHog.com doesn't use third party cookies - only a single in-house cookie.

No data is sent to a third party.

Ursula von der Leyen, President of the European Commission