Bestly is shutting down. Here's what you need to know. Click here.

iOS API Integration

Overview and Sample Code

It's easy to run an experiment! This sample code demonstrates how to use Bestly to A/B test two button colors:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [Bestly runExperimentWithID:@"ee3eec2619ac4260c31p985579b5f92q" A:^{
        [button setBackgroundImage:[UIImage imageNamed:@"green-button-bg.png"] forState:UIControlStateNormal];
    } B:^{
        [button setBackgroundImage:[UIImage imageNamed:@"red-button-bg.png"] forState:UIControlStateNormal];

- (void)buttonTapped:(id)sender {
    [Bestly trackEvent:@"Button Tapped" withProperties:nil];
    // Do something else with the button, such as performing a segue.

Installing the Library

The easiest way to get Bestly into your iOS project is to use CocoaPods.

  1. Install CocoaPods using gem install cocoapods
  2. Create a file in your Xcode project called Podfile and add pod 'Bestly'
  3. Run pod install in your Xcode project directory. CocoaPods will download and install the Bestly library and create a new Xcode workspace. Open up this workspace in Xcode.

Initializing the Library

To start tracking with the Bestly iOS library, you must first initialize it with your API key. You can find your API key in your project settings.

To initialize the library, call setupWithKey: with your API key as its argument.

[Bestly setupWithKey:@"{API_KEY}"];

In most cases, it makes sense to do this in application:didFinishLaunchingWithOptions: .

Running Experiments

  1. Place this code wherever you'd like to run an A/B test. Use the Experiment ID that corresponds to the experiment you want to run.

    [Bestly runExperimentWithID:@"{EXPERIMENT_ID}" A:^{
        //Code for variation A
    } B:^{
        //Code for variation B
  2. Place this code whenever you'd like to track an event. For example, if you are testing engagement for two different button colors, place this code in the button's action method.

    [Bestly trackEvent:@"{EVENT_NAME}" withProperties:nil];

Sending Events

Once you've initialized the library, you can track an event by calling trackEvent:withProperties: with the event name and properties.

[Bestly trackEvent:@"{EVENT_NAME}" withProperties:nil];

Managing User Identities

The Bestly library will assign a default unique identifier (or "alias) to each unique user who installs your application. This alias is saved to device storage so that it will persist across sessions.

If you choose, you can assign your own aliases. This is particularly useful if a user is using your app on multiple platforms (both web and mobile, for example). To assign your own alias, you can use identifyCurrentUserAs: .

Calling identifyCurrentUserAs: will change the alias being sent with your events. Unless this alias is already associated with the user, this will break all funnels because we won't be able to associate the old alias with the new

Adding an Alias

In situations where you want to add an alias to a user (in practice, this really just means when the user signs up for the first time) you should use addAliasToCurrentUser: , which sends an update to our server linking the alias we generated for your user with the alias you'd like to add.