The swift code for this article is available here. It is built on the WWDC 2021 watchOS Workout App example, with Swift 5 in Xcode 14.2, supporting iOS 15.0+, and watchOS 8.0+.
You have just successfully built an excellent watch app. Congratulations! Pretty soon, it will be time to update, introduce new features, and roll out some enhancements. How can you be confident that your new ideas will appeal to your users and attract new ones? Will your planned features be useful and easy to adopt? Will they flow naturally with your usage?
The best way to know this is to know how your customers are presently using your watch app. This will tell you which features of your app are under-utilized and could be removed with minimal impact, while you focus your development on what is exciting.
How can you turn on this two-way conversation? You can do this with event tracking in the Split Feature Data Platform™. It powers feature flag management, software experimentation, and continuous delivery.
This article will guide you to easily set up event tracking in your swift code for watchOS. Then we’ll show you how to measure and analyze these events within the Split Metrics impact dashboard. Along the way, you’ll see the potential to return valuable user feedback that will turn on confident data-driven, feature development for you, the developer.
Ready to dive in? Our starting point will be the WWDC 2021 Workout App example. We will add Split event tracking to learn which are the most popular workout selections. Let’s get started.
Importing the Split SDK Into Your Xcode Project
In the Xcode project click File | Add Packages… In the Search field of the dialogue window, paste the URL for the iOS client flavor of Split:
https://github.com/splitio/ios-client and click “Add Package”.
Click “Add Package” once again in a second dialog window, and you will see the Split package dependency in the Project Navigator.
Adding a Singleton Class for Wrapping Split
We’ll add a single point of communication with the Split SDK. To do this, add a new .swift file to the Controller folder and call it
SplitWrapper.swift with the following code:
Let’s take a moment to look over the
The SplitWrapper implements the singleton pattern, as it exposes a static instance variable and has a private
init function. This ensures that the Split SDK factory is (correctly) initialized only once during the lifetime of the app.
For the convenience of updating our Views automatically by the Framework, we also implement the
ObservableObject pattern. This sets us up to instantiate the
SwiftWrapper in the root of our application.
MyWorkoutApp.swift, add the following declaration:
We will pass this variable to all descendants using the
environmentObject function of the NavigationView. As a result, our
MyWorkoutsApp.swift file will look like this:
Okay, now we are prepared to add useful Swift event tracking to our app.
Calling the Split SDK to Turn On Event Tracking
Recall that we are interested in what workouts are most popular. In
StartView.swift, we already generate a list of
NavigationLinks, as shown below:
Below is the StartView in action:
To add Swift event tracking, we first reference the SwiftWrapper in StartView by declaring the
EnvironmentObject like this:
Next, we add the
onAppear function to the
NavigationLink with the following code for Split:
split.eval("track_workouts") call evaluates the feature flag that will allow us to turn event tracking on or off from Split’s web-based Management Console. The
split.track() calls each send an event to Split Cloud. We will later configure Split to use these events to calculate the popularity of the different workout selections. For convenience, we have the
split.flush() call to send cached data (e.g. events) to Split cloud, viewable on the Management Console.
We will also add
overlay functions to the List to wait for Split to instantiate and download definitions.
StartView.swift file now looks like this:
That is it! From the code side, we are good to go to send events to Split.
Getting Your Split API Key
We need a valid API key for our app to interact with Split and then just set up our feature flag and event tracking metrics. The API keys are generated within the web-based Split Management Console. Log in to Split or sign up for your free trial.
In the Management Console, click the gray square labeled “DE” at the top left corner and select “Admin settings”.
In the light gray navigation menu, select “API keys”.
Copy the client-side API key from the chart, taking note of the environment that the key is associated with, as shown in the “Environments” column.
Now let’s return to Xcode, to your
SwiftWrapper.swift file. Paste the API key as the value of
apiKey, at the following line:
Creating a Feature Flag (A.K.A. Split)
Recall that we referenced a
track_workouts feature flag to turn on event tracking in the code:
We will now define this feature flag in Split. In the Split Console, click “Splits” (split is synonymous with feature flag), and then “Create split”.
Define the feature flag with the following settings:
Click “Create” and you’ve added the
track_workouts feature flag to your Split definitions.
Adding Targeting Rules for Your Feature Flag
Targeting rules allow you to control the string values (treatments) that a feature flag returns to your code. This can be based on a variety of criteria, including randomization, and it’s useful for feature experimentation and rollouts. For our purposes, we will simply ensure our
track_workouts feature flag returns
Viewing the split definition, make sure you have the correct environment selected, and click to “Add Rules” to the split.
Then set your Default Rule to serve
on as shown below.
Now click “Review changes” and “Save”.
With this set up, the
split.eval("track_workouts") call in our watchOS app’s
StartView will return the string
Viewing Incoming Events In Split
Recall that in our swift code, we are sending events to Split cloud using the
Now, to check that the events are coming in, click “Data hub”:
Make sure your Environment matches that of your API key, click on the “Live tail” tab, and change the Data type to “Events”.
Click the “Query” button.
Now run or debug your code and you will see events coming into Split cloud when you click a workout.
Have fun clicking the workouts and seeing the events appear in Split Console. We will use these events to create three metrics in the next section.
Creating Your Metrics In Split
Let’s create metrics for analysis of our tracking events. Click ‘Metrics’ and then ‘Create metric’.
Set up a metric with properties as shown below:
Similarly, we will need to set up another metric for the
bike_ratio and a third metric for the
walk_ratio as well.
Viewing Your Split Metrics Results
To see your metrics in action, click “Splits”, select
track_workouts to open the split definition, and click on the “Metrics impact” tab. You should notice three shiny new metrics cards, as shown below:
On the ‘Metrics impact’ tab, you will find a small but very handy button link: ‘Recalculate metrics’.
Click on it, and notice the ‘in progress’ label beside it.
We will need to wait a few moments until this label disappears. Now may be a good time to take a few minutes to treat yourself to a refreshing or invigorating drink and contemplate your excellent progress with Split and data-driven development.
When you have returned, notice “in progress” has disappeared. You can refresh your browser page and you should see your workout ratios, as shown below.
That’s it! Now you can see your users’ workout choices. You can stop tracking these events instantly from the Split Management Console by setting the
track_workouts feature flag default treatment to
off. Switch it to anything other than
on as defined in your swift watchOS code or by deleting (killing) the flag altogether. You can also tweak the definition of the feature flag and add some user parameters in your code. This will activate tracking for a finely-tuned subset of users, according to criteria that you define in the Split Management Console. That’s a lot of power at your fingertips.
With Split’s event tracking capabilities, it can take the guesswork out of watchOS feature development and code with confidence. With this simple example as a springboard, we hope this inspires some great ideas of your own for leveraging the Split Feature Data Platform to your advantage.
Now you can enjoy a comfortable two-way conversation with your customers, and fully engage your users with your perfectly targeted and expertly honed, data-driven development.
Complete code featured in this article is available here, with a tip of the hat to Javier Avrudsky on the Split Team for excellent guidance. Checkout the
Get Split Certified
Split Arcade includes product explainer videos, clickable product tutorials, manipulatable code examples, and interactive challenges.
Switch It On With Split
Split gives product development teams the confidence to release features that matter faster. It’s the only feature management and experimentation platform that automatically attributes data-driven insight to every feature that’s released—all while enabling astoundingly easy deployment, profound risk reduction, and better visibility across teams. Split offers more than a platform: It offers partnership. By sticking with customers every step of the way, Split illuminates the path toward continuous improvement and timely innovation. Switch on a trial account, schedule a demo, or contact us for further questions.