Skip to main content

Objective-C (iOS) (0.7.0)

You are reading documentation for an outdated version. Here’s the latest one!

1. Overview‚Äč

The Snowplow Objective-C Tracker allows you to track Snowplow events from your iOS, OSX and tvOS apps and games. It supports iOS 7.0+, OSX 10.9+ and tvOS 9.0+.

The tracker should be straightforward to use if you are comfortable with iOS development; its API is modelled after Snowplow's Python Tracker so any prior experience with that tracker is helpful but not necessary. If you haven't already, have a look at the iOS Tracker Setup guide before continuing.

You can also find detailed documentation for the method calls in the tracker classes available as part of the CocoaPods documentation.


The Tracker is designed to be used as a Singleton object. Meaning that within your application you should only have to create one Tracker for the lifecycle of your application. Without setting this up:

  • Multiple Emitters could become active resulting in the same events being sent multiple times.
  • Thrashing of the database.
  • Hogging of resources for sending HTTP requests which can slow your application.

For a basic example of the Singleton pattern:

// --- Header File 'SnowplowManager.h'

@class SPTracker;

@interface SnowplowManager : NSObject {
SPTracker *tracker;

@property (nonatomic, retain) SPTracker *tracker;

+ (id) snowplowManager;


// --- Method File 'SnowplowManager.m'

#import "SnowplowManager.h"
#import "SPTracker.h"
#import "SPEmitter.h"

@implementation SnowplowManager

@synthesize tracker;

#pragma mark Singleton Methods

+ (id) snowplowManager {
static SnowplowManager *sharedSnowplowManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedSnowplowManager = [[self alloc] init];
return sharedSnowplowManager;

- (id) init {
self = [super init];
if (self) {
SPEmitter *emitter = [SPEmitter build:^(id<SPEmitterBuilder> builder) {

[builder setUrlEndpoint:@"com.acme"]
}]; tracker = [SPTracker build:^(id<SPTrackerBuilder> builder) {
[builder setEmitter:emitter]
}]; } return self; }


You can then access your Tracker via SnowplowManager *snowplowManager = [SnowplowManager snowplowManager].

1.1 Demonstration App‚Äč

If you would like to see the Tracker in action you can launch the demo app like so:

  • Download the GitHub repo:¬†git clone
  • In XCode open the¬†SnowplowDemo.xcworkspace¬†file.
  • Select the device you want to launch the SnowplowDemo into!

You will then need to simply enter a valid endpoint URL and hit the Start Demo! button.

1.2 HTTPs and Certificates‚Äč

Please note that with the release of iOS 9, tvOS 9 and OS-x 10.11 Apple's Application Transport Security now requires:

  1. That all network communication be done using HTTPs by default. There are ways around this if need be.
  2. We have noticed that for the Application Transport Security to work with your certificates you will need to supply the whole certificate chain.

2. Initialization‚Äč

Assuming you have completed the iOS Tracker Setup for your project, you are now ready to initialze the Snowplow Tracker.

2.1 Importing the library‚Äč

Adding the library into your project is as simple as adding the headers into your class file:

#import <SPTracker.h>
#import <SPEmitter.h>

If you have manually copied the library into your project, don't forget to change your import syntax:

#import "SPTracker.h"
#import "SPEmitter.h"

If you have statically added the library you will need to further amend your syntax:

#import "SnowplowTracker/SPTracker.h"
#import "SnowplowTracker/SPEmitter.h"

That's it - you are now ready to initialize a tracker instance.

2.2 Creating a tracker‚Äč

To instantiate a tracker in your code simply instantiate the SPTracker class with the following builder pattern:

SPTracker *tracker = [SPTracker build:^(id<SPTrackerBuilder> builder) {
[builder setEmitter:emitter]; // Required
[builder setSubject:subject]; // Optional
[builder setAppId:_appId]; // Optional
[builder setTrackerNamespace:_namespace]; // Optional
[builder setBase64Encoded:YES]; // Optional
[builder setSessionContext:YES]; // Optional
[builder setForegroundTimeout:300]; // Optional
[builder setBackgroundTimeout:150]; // Optional
[builder setCheckInterval:10]; // Optional
Builder FunctionDescription
setEmitterThe SPEmitter object you create
setSubjectThe SPSubject object you create
setAppIdThe application ID
setTrackerNamespaceThe name of the tracker instance
setBase64EncodedWhether to enable base 64 encoding
setSessionContextWhether to enable client sessions
setForegroundTimeoutThe session foreground timeout
setBackgroundTimeoutThe session background timeout
setCheckIntervalThe session checking interval


This is a single SPEmitter object that will be used to send all the tracking events created by the SPTracker to a collector. See Sending events for more on its configuration.


If provided, the namespace argument will be attached to every event fired by the new tracker. This allows you to later identify which tracker fired which event if you have multiple trackers running.


The appId argument lets you set the application ID to any string.


By default, unstructured events and custom contexts are encoded into Base64 to ensure that no data is lost or corrupted. You can turn encoding on or off using the Boolean base64Encoded argument.


By default, no client sessionization is activated. Once enabled the Tracker will start appending a client_session context to each event it sends and it will maintain this session information for the life of the application; i.e. as long as the application is installed on the device.

NOTE: A known bug existed in version 0.6.0 for the default settings where the foreground and background timeouts are passed as ms rather than s. To ensure a sane timeout please add the following to your Tracker creation:

SPTracker *tracker = [SPTracker build:^(id<SPTrackerBuilder> builder) {
[builder setForegroundTimeout:600]; // 10 minutes
[builder setBackgroundTimeout:300]; // 5 minutes


This function when called will pause all event tracking and sessionization actions until resume is called.

[tracker pauseEventTracking];


This function will resume all event tracking when called (if it was paused) and will also re-enable sessionization if it was already on.

[tracker resumeEventTracking];

3. Adding extra data‚Äč

To add extra data to the Tracker you will need to append an SPSubject object to the Tracker. This can be done either during Tracker creation or added later.

Be aware that the use of the geo-location requires you to set all values yourself; we do not currently support automatic geo detection.

SPSubject *subject = [[SPSubject alloc] init];

// OR with the optional platform/geo-location context...

SPSubject *subject = [[SPSubject alloc] initWithPlatformContext:YES andGeoContext:NO];

// Add it to the Tracker during construction...
SPTracker *tracker = [SPTracker build:^(id<SPTrackerBuilder> builder) {
[builder setSubject:subject]; // Optional

// Add it later...

[tracker setSubject:subject];

NOTE: initWithPlatformContext refers to getting the context for the particular platform that the Tracker is running on. In the case of an iOS application, this will then automatically fetch the mobile_context for all of your events.

Standard Pairs‚Äč


3.1 Sending IFA‚Äč

Apps that do not display advertisements are not allowed to access Apple's Identifier For Advertisers (IFA). For this reason, the Snowplow Objective-C Tracker will only send IFA as part of the mobile_context which is attached to each event if you have the AdSupport.framework included in your app (and are therefore intending to serve ads).

For the avoidance of doubt, you can also avoid sending IFA regardless of your advertising situation, thus:

  • Click on¬†Build Settings¬†to your app's project in Xcode
  • Search for¬†Preprocessor Macros
  • Add a macro defined asSNOWPLOW_NO_IFA = 1

3.2 Set user ID with¬†setUserId‚Äč

You can set the user ID to any string:

[subject setUserId:_userId];


[subject setUserId:@"alexd"];

3.3 Set Screen Resolution with¬†setResolutionWithWidth‚Äč

You can set the screen resolution to any width and height.


[subject setResolutionWithWidth:1920 andHeight:1080];

3.4 Set View Port with¬†setViewPortWithWidth‚Äč

You can set the viewport to any width and height.


[subject setViewPortWithWidth:1920 andHeight:1080];

3.5 Set Color Dpeth with¬†setColorDepth‚Äč

You can set the color depth to any integer.


[subject setColorDepth:20];

3.6 Set Timezone with¬†setTimezone‚Äč

You can set the timezone to any string.


[subject setTimezone:@"UTC"];

3.7 Set Language with¬†setLanguage‚Äč

You can set the language to any string.


[subject setLanguage:@"en"];

3.8 Set IP Address with¬†setIpAddress‚Äč

You can set the user IP Address to any string.


[subject setIpAddress:@""];

3.9 Set the Useragent with¬†setUseragent‚Äč

You can set the Useragent to any string.


[subject setUseragent:@"aUseragent"];

3.10 Set the Network User ID with¬†setNetworkUserId‚Äč

You can set the Network User ID to any string.


[subject setNetworkUserId:@"nuid"];

3.11 Set the Domain User ID with¬†setDomainUserId‚Äč

You can set the Domain User ID to any string.


[subject setDomainUserId:@"duid"];

3.12 Set Geo-Location variables‚Äč

Due to difficulty in getting these variables automatically, we are depending on the developer to pass in these values for us if they wish to populate the geo-location. This will hopefully change in the future.

NOTE: latitude and longitude are required fields and must be present if you decide to include the geo-location context with your events.

These are the available functions for geo-location which are all called directly on a subject object:

[subject setGeoXXX:];
  • setGeoLatitude¬†: Sets the latitude value
  • setGeoLongitude¬†: Sets the longitude value
  • setGeoLatitudeLongitudeAccuracy¬†: Sets the lat-long accuracy
  • setGeoAltitude¬†: Sets the altitude
  • setGeoAltitudeAccuracy¬†: Sets the altitude accuracy
  • setGeoBearing¬†: Sets the bearing
  • setGeoSpeed¬†: Sets the speed
  • setGeoTimestamp¬†: Sets a timestamp (must be in ms since unix epoch)

Once this is set it will be automatically attached to all events being sent.

4. Tracking specific events‚Äč

Snowplow has been built to enable you to track a wide range of events that occur when users interact with your websites and apps. We are constantly growing the range of functions available in order to capture that data more richly.

Tracking methods supported by the Objective-C Tracker at a glance:

trackScreenViewEvent:Track the user viewing a screen within the application
trackPageViewEvent:Track and record views of web pages.
trackEcommerceEvent:Track an ecommerce transaction and its items
trackStructuredEvent:Track a Snowplow custom structured event
trackUnstructuredEvent:Track a Snowplow custom unstructured event
trackTimingEvent:Track a Snowplow user timing event

4.1 Common‚Äč

All events are tracked with specific methods on the tracker instance, of the form trackXXX(), where XXX is the name of the event to track.

4.1.1 Custom contexts‚Äč

In short, custom contexts let you add additional information about the circumstances surrounding an event in the form of an NSDictionary object. Each tracking method accepts an additional optional contexts builder method.

The context argument should consist of a NSMutableArray of NSDictionary representing an array of one or more contexts. The format of each individual context element is the same as for an unstructured event.

If a visitor arrives on a page advertising a movie, the context dictionary might look like this:

"schema": "iglu:com.acme_company/movie_poster/jsonschema/2-1-1",
"data": {
"movieName": "The Guns of Navarone",
"posterCountry": "US",
"posterYear": "1961"

The corresponding NSDictionary would look like this:

NSDictionary *poster = @{
@"data": @{
@"movieName": @"The Guns of Navarone",
@"posterCountry": @"US",
@"posterYear": @"1961"

Sending the movie poster context with an event looks like this:

event = [SPStructured build:^(id<SPStructuredBuilder> builder) {
[builder setCategory:@"DemoCategory"];
[builder setAction:@"DemoAction"];
[builder setContexts:[NSMutableArray arrayWithArray:@[poster]]];

[tracker trackStructuredEvent:event];

Note that even if there is only one custom context attached to the event, it still needs to be placed in an array.

4.1.2 Optional timestamp argument‚Äč

In all the trackers, we offer a way to set the timestamp if you want the event to show as tracked at a specific time. If you don't, we create a timestamp while the event is being tracked.

Please note this argument must always be in milliseconds since the unix epoch like so 1446542245000.

Here is an example:

event = [SPStructured build:^(id<SPStructuredBuilder> builder) {
[builder setCategory:@"DemoCategory"];
[builder setAction:@"DemoAction"];
[builder setTimestamp:1446542245000];

[tracker trackStructuredEvent:event];

4.1.3 Optional Event ID argument‚Äč

This Tracker also offers a way to set a custom Event ID with each event you send to snowplow. If you do not populate this field we will do it automatically.

Please note that to be valid the event id must be a UUID.

Here is an example:

event = [SPStructured build:^(id<SPStructuredBuilder> builder) {
[builder setCategory:@"DemoCategory"];
[builder setAction:@"DemoAction"];
[builder setEventId:"your-custom-uuid-string"];

[tracker trackStructuredEvent:event];

4.1.4 Tracker method return values‚Äč

To be confirmed. As of now, trackers do not return anything.

4.2 Track screen views with¬†trackScreenViewEvent:‚Äč

Use trackScreenViewEvent: to track a user viewing a screen (or equivalent) within your app. Arguments are:

setNameHuman-readable name for this screenNoNSString*
setIdUnique identifier for this screenNoNSString*
setContextsCustom context for the eventNoNSMutableArray*
setTimestampOptional timestamp for the eventNoNSInteger
setEventIdOptional event id for the eventNoNSString*


SPScreenView *event = [SPScreenView build:^(id<SPScreenViewBuilder> builder) {
[builder setName:@"HUD > Save Game"];
[builder setId:@"screen23"];

[tracker_ trackScreenViewEvent:event];

NOTE: You must populate at least one of name or id for the event to build.

4.3 Track pageviews with¬†trackPageViewEvent:‚Äč

Use trackPageViewEvent: to track a user viewing a page within your app.

Arguments are:

setPageUrlThe URL of the pageYesNSString*
setPageTitleThe title of the pageNoNSString*
setReferrerThe address which linked to the pageNoNSString*
setContextsCustom context for the eventNoNSMutableArray*
setTimestampOptional timestamp for the eventNoNSInteger
setEventIdOptional event id for the eventNoNSString*


SPPageView *event = [SPPageView build:^(id<SPPageViewBuilder> builder) {
[builder setPageUrl:@"DemoPageUrl"];
[builder setPageTitle:@"DemoPageTitle"];
[builder setReferrer:@"DemoPageReferrer"];

[tracker trackPageViewEvent:event];

4.4 Track ecommerce transactions with¬†trackEcommerceEvent:‚Äč

Use trackEcommerceEvent: to track an ecommerce transaction. Arguments:

setOrderIdID of the eCommerce transactionYesNSString*
setTotalValueTotal transaction valueYesdouble
setAffiliationTransaction affiliationNoNSString*
setTaxValueTransaction tax valueNodouble
setShippingDelivery cost chargedNodouble
setCityDelivery address cityNoNSString*
setStateDelivery address stateNoNSString*
setCountryDelivery address countryNoNSString*
setCurrencyTransaction currencyNoNSString*
setItemsItems in the transactionYesNSArray*
setContextsCustom context for the eventNoNSMutableArray*
setTimestampOptional timestamp for the eventNoNSInteger
setEventIdOptional event id for the eventNoNSString*

trackEcommerceEvent: fires multiple events: one "transaction" event for the transaction as a whole, and one "transaction item" event for each element of the items array. Each transaction item event will have the same timestamp, orderId, and currency as the main transaction event.

The items argument is an NSArray containing an SPEcommerceItem for each item in the transaction. There is a convenience object for each item called SPEcommerceItem:. Arguments:

setItemIdID of the eCommerce transactionYesNSString*
setSkuItem SKUYesNSString*
setPriceItem priceYesdouble
setQuantityItem quantityYesNSInteger
setNameItem nameNoNSString*
setCategoryItem categoryNoNSString*
setCurrencyTransaction currencyNoNSString*
setContextsCustom context for the eventNoNSMutableArray*
setTimestampOptional timestamp for the eventNoNSInteger
setEventIdOptional event id for the eventNoNSString*

Example of tracking a transaction containing one item:

NSString *transactionID = @"6a8078be";
NSString *currency = @"USD";
NSMutableArray *itemArray = [NSMutableArray array];

[itemArray addObject:[SPEcommerceItem build:^(id<SPEcommTransactionItemBuilder> builder) {
[builder setItemId:transactionID];
[builder setSku:@"pbz0026"];
[builder setName:@"Hot Chocolate"];
[builder setCategory:@"Drink"];
[builder setPrice:0.75F];
[builder setQuantity:1];
[builder setCurrency:currency]

SPEcommerce *event = [SPEcommerce build:^(id<SPEcommTransactionBuilder> builder) {
[builder setOrderId:transactionID];
[builder setTotalValue:350];
[builder setAffiliation:@"DemoTranAffiliation"];
[builder setTaxValue:10];
[builder setShipping:15];
[builder setCity:@"Boston"];
[builder setState:@"Massachusetts"];
[builder setCountry:@"USA"];
[builder setCurrency:currency];
[builder setItems:itemArray]

[tracker trackEcommerceEvent:event];

4.5 Track structured events with¬†trackStructuredEvent:‚Äč

Use trackStructuredEvent: to track a custom event happening in your app which fits the Google Analytics-style structure of having up to five fields (with only the first two required):

setCategoryThe grouping of structured events which this action belongs toYesNSString*
setActionDefines the type of user interaction which this event involvesYesNSString*
setLabelA string to provide additional dimensions to the event dataNoNSString*
setPropertyA string describing the object or the action performed on itNoNSString*
setValueA value to provide numerical data about the eventNoNSInteger
setContextsCustom context for the eventNoNSMutableArray*
setTimestampOptional timestamp for the eventNoNSInteger
setEventIdOptional event id for the eventNoNSString*


SPStructured *event = [SPStructured build:^(id<SPStructuredBuilder> builder) {
[builder setCategory:@"shop"];
[builder setAction:@"add-to-basket"];
[builder setLabel:@"Add To Basket"];
[builder setProperty:@"pcs"];
[builder setValue:27]

[tracker trackStructuredEvent:event];

4.6 Track unstructured events with¬†trackUnstructuredEvent:‚Äč

Custom unstructured events are a flexible tool that enables Snowplow users to define their own event types and send them into Snowplow.

When a user sends in a custom unstructured event, they do so as a JSON of name-value properties, that conforms to a JSON schema defined for the event earlier.

Use trackUnstructuredEvent: to track a custom event which consists of a name and an unstructured set of properties. This is useful when:

  • You want to track event types which are proprietary/specific to your business (i.e. not already part of Snowplow), or
  • You want to track events which have unpredictable or frequently changing properties

The arguments are as follows:

setEventDataThe properties of the eventYesSPSelfDescribingJson*
setContextsCustom context for the eventNoNSMutableArray*
setTimestampOptional timestamp for the eventNoNSInteger
setEventIdOptional event id for the eventNoNSString


NSDictionary * data = @{@"level": @23, @"score": @56473};
SPSelfDescribingJson * sdj = [[SPSelfDescribingJson alloc] initWithSchema:@"iglu:com.acme/save_game/jsonschema/1-0-0"

SPUnstructured *event = [SPUnstructured build:^(id<SPUnstructuredBuilder> builder) {
[builder setEventData:sdj];

[tracker trackUnstructuredEvent:event];

For more on JSON schema, see the blog post.

4.7 Track user timings with¬†trackTimingEvent:‚Äč

Use trackTimingEvent: to track a user timing in your app - for example, how long a game took to load, or how long an in-app purchase took to download. The fields are as follows:

setCategoryCategorizing timing variables into logical groups (e.g API calls, asset loading)YesNSString*
setVariableIdentify the timing being recordedYesNSString*
setTimingThe number of milliseconds in elapsed time to reportYesNSInteger
setLabelOptional description of this timingYesNSString*
setContextsCustom context for the eventNoNSMutableArray*
setTimestampOptional timestamp for the eventNoNSInteger
setEventIdOptional event id for the eventNoNSString*


SPTiming *event = [SPTiming build:^(id<SPTimingBuilder> builder) {
[builder setCategory:@"Application"];
[builder setVariable:@"Background"];
[builder setTiming:324];
[builder setLabel:@"5231804123"]

[tracker trackTimingEvent:event];

5. Sending events:¬†SPEmitter‚Äč

Events created by the Tracker are sent to a collector using a SnowplowEmitter instance. You can create one using the following builder example:

SPEmitter *emitter = [SPEmitter build:^(id<SPEmitterBuilder> builder) {
[builder setUrlEndpoint:_url]; // Required
[builder setHttpMethod:_method]; // Optional
[builder setProtocol:_protocol]; // Optional
[builder setCallback:self]; // Optional
[builder setEmitRange:200]; // Optional
[builder setEmitThreadPoolSize:20]; // Optional
[builder setByteLimitGet:50000]; // Optional
[builder setByteLimitPost:50000]; // Optional }];

A key change to the emitter construction is the removal of the setBufferOption and the addition of byteLimit. In the case of POST requests, we will now add events up until a byte limit rather than an arbitrary event count. However, there are a few more implications:

  • If an event, by itself, exceeds this limit it will be sent but it is then removed from the queue irrespective of the result.
  • If you set the limit higher than 52000 it is likely that none of your events will make it to the collector.

NOTE: The current safe maximum byte threshold is 52000, however, this may change in the future.

Builder FunctionDescription
setUrlEndpointThe collector resource name to use for sending events
setHttpMethodThe method sending; either GET or POST
setProtocolThe protocol option; HTTP or HTTPS
setCallbackThe optional emitter callback
setEmitRangeThe count of events that are retrieved from the database
setEmitThreadPoolSizeThe size of the emitting Thread Pool
setByteLimitGetThe max bytes in a GET request
setByteLimitPostThe max bytes in a POST request

5.1 Using a protocol‚Äč

The protocol argument determines if the event is sent over HTTP or HTTPS. In the case of iOS 9.0 all events are automatically sent with HTTPS.

You can set this during the creation of a SPEmitter object or use the setter -(void)setProtocol:

SPEmitter *emitter = [SPEmitter build:^(id<SPEmitterBuilder> builder) {
[builder setUrlEndpoint:_url];
[builder setProtocol:SPHttp]

[emitter setProtocol:SPHttps];

Here are all the possible options that you can use:

SPHttpSends events as HTTP
SPHttpsSends events as HTTPs

5.2 Choosing the HTTP method‚Äč

Snowplow supports receiving events via GET and POST requests. In a GET request, each event is sent in an individual request. With POST requests, events can be bundled together in one request.

Here are all the posibile options that you can use:

SPRequestGetEvents are sent individually as GET requests
SPRequestPostEvents are sent in a group when 10 events are received in one POST request

5.3 Adding an Emitter Callback‚Äč

You are now also able to include an emitter callback which will return the count of successful and failed events.

To implement you will need to:

  • Add the¬†RequestCallback¬†protocol to your header file:
// Example from the SnowplowDemo -> ViewController.h file:
@interface ViewController : UIViewController <UITextFieldDelegate, RequestCallback>

// Extra Example
@interface MyObjcClass : NSObject <RequestCallback>
  • In your paired¬†.m¬†file add the following functions:
// Define Callback Functions
- (void) onSuccessWithCount:(NSInteger)successCount {
// Do something with result

- (void) onFailureWithCount:(NSInteger)failureCount successCount:(NSInteger)successCount {
// Do something with results
  • Construct the¬†SPEmitter¬†like so:
NSURL *url = [[NSURL alloc] initWithString:@""];

SPEmitter *emitter = [SPEmitter build:^(id<SPEmitterBuilder> builder) {
[builder setUrlEndpoint:url];
[builder setCallback:self]

The self will work only if you have declared the callback functions in the same class as you are creating the Emitter from. Otherwise you will need to pass in the target for the class in which you have defined these functions.

5.4 Sending HTTP requests‚Äč

You can set this during the creation of a SPEmitter object:

NSURL *url = [[NSURL alloc] initWithString:@""];

SPEmitter *emitter = [SPEmitter build:^(id<SPEmitterBuilder> builder) {
[builder setUrlEndpoint:url];
[builder setHttpMethod:SPRequestPost];

// OR
[builder setHttpMethod:SPRequestGet]

6. Utility Functions‚Äč

The SPUtilities class contains a host of static functions which are used throughout the Tracker. To see all of the available functions please consult the SPUtilities.h file.


This function will only return the IDFA under the following conditions:

  • The device is running iOS.
  • The AdSupport library is in your project.

To use:

NSString* appleIdfa = [SPUtilities getAppleIdfa];


This function will only return the IDFV under the following conditions:

  • The device is running iOS.

To use:

NSString* appleIdfv = [SPUtilities getAppleIdfv];


This function will only return the OpenIDFA under the following conditions:

  • The device is running iOS.
  • The iOS version is less than 9.0.

To use:

NSString* openIdfa = [SPUtilities getOpenIdfa];
Was this page helpful?