Objective-C (iOS) (0.3.0)
1. Overview#
The Snowplow iOS Tracker allows you to track Snowplow events from your iOS apps and games. It supports iOS 7.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.
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 <SnowplowTracker.h>
#import <SnowplowEmitter.h>
If you have manually copied the library into your project, don't forget to change your import syntax:
#import "SnowplowTracker.h"
#import "SnowplowEmitter.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 SnowplowTracker class with the constructor:
- (id) initWithCollector:(SnowplowEmitter *)collector_
appId:(NSString *)appId_
base64Encoded:(Boolean)encoded
namespace:(NSString *)namespace_
For example:
SnowplowTracker *t1 = [[SnowplowTracker alloc] initWithCollector:collector appId:@"AF003" base64Encoded:false namespace:@"cloudfront"];
| Argument Name | Description |
|---|---|
collector | The SnowplowEmitter object you create |
namespace | The name of the tracker instance |
appId | The application ID |
base64Encoded | Whether to enable base 64 encoding |
2.2.1 collector
This is a single SnowplowEmitter object that will be used to send all the tracking events created by the SnowplowTracker to a collector. See Sending events for more on its configuration.
2.2.2 namespace
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.
2.2.3 appId
The appId argument lets you set the application ID to any string.
2.2.4 base64Encoded
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.
3. Adding extra data#
Unlike the other Trackers, the iOS tracker automatically collects your platform, screen resolution, viewport, color depth, timezone and language from the device. You can still however, set your user ID to properly track different users if you require it.
3.1 Set user ID with setUserId#
You can set the user ID to any string:
s1.setUserId( "{{USER ID}}" )
Example:
[tracker setUserId:@"alexd"];
3.2 Sending IFA#
Apps that do not display advertisements are not allowed to access Apple's Identifier For Advertisers (IFA). For this reason, the Snowplow iOS 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 as
SNOWPLOW_NO_IFA = 1
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 iOS Tracker at a glance:
| Function | *Description |
|---|---|
trackScreenView: | Track the user viewing a screen within the application |
trackPageView: | Track and record views of web pages. |
trackEcommerceTransaction: | Track an ecommerce transaction and its items |
trackStructuredEvent: | Track a Snowplow custom structured event |
trackUnstructuredEvent: | Track a Snowplow custom unstructured event |
trackTiming: | 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 parameter after all the parameters specific to that method:
- (void) trackPageView:(NSString *)pageUrl
title:(NSString *)pageTitle
referrer:(NSString *)referrer;
- (void) trackPageView:(NSString *)pageUrl
title:(NSString *)pageTitle
referrer:(NSString *)referrer
context:(NSMutableArray *)context;
- (void) trackPageView:(NSString *)pageUrl
title:(NSString *)pageTitle
referrer:(NSString *)referrer
timestamp:(double)timestamp;
- (void) trackPageView:(NSString *)pageUrl
title:(NSString *)pageTitle
referrer:(NSString *)referrer
context:(NSMutableArray *)context
timestamp:(double)timestamp;
The context