The iOS SDK makes it easy to track events from Swift and Objective-C applications.
Installation
The iOS SDK is hosted on GitHub at
git@github.com:ht-sdks/events-sdk-swift.git
under the name
events-sdk-swift
.
You may install the SDK by adding it to your package.swift
file, or adding it
as a Package Dependency via Xcode.
Initialization
You should initialize the SDK in a central location and store it in a variable for use across your application. For example, you may want to initialize it in your app delegate.
Swift example:
import Hightouch
let configuration = Configuration(writeKey: "YOUR_WRITE_KEY")
.apiHost("us-east-1.hightouch-events.com/v1")
analytics = Analytics(configuration: configuration)
Objective-C example:
@import Hightouch
HTConfiguration *config = [[HTConfiguration alloc] initWithWriteKey:@"YOUR_WRITE_KEY"];
config.apiHost = "us-east-1.hightouch-events.com/v1";
analytics = [[HTAnalytics alloc] initWithConfiguration: config];
Configuration options:
Option | Type | Description |
---|---|---|
writeKey | String | Your Highotuch write key. |
apiHost | String | The API host to send the tracked events to. |
flushAt | Int | The number of events to queue before sending events to the Hightouch API. Defaults to 20. |
flushInterval | Int | The maximum amount of time (in seconds) to store events locally before forwarding to Hightouch. Defaults to 30 seconds. |
trackApplicationLifecycleEvents | Bool | Whether to automatically track application lifecycle events (like opening the application). Defaults to true . |
API
Identify
The identify
method sends an identify
event. It accepts user id and trait
information for the current user -- if you only have partial information, you
can just supply a single parameter.
Method signature:
func identify<T: Codable>(userId: String, traits: T)
func identify<T: Codable>(traits: T)
func identify(userId: String)
Method parameters:
Parameter | Type | Description |
---|---|---|
userId | String | The user's persistent ID |
traits | Codable | Additional traits about the user, such as email and name . |
Track
The track
method sends a track
event. The event name is required, and
additional properties that describe the event may be supplied.
Method signature:
func track(name: String)
func track<P: Codable>(name: String, properties: P?)
Method parameters:
Parameter | Type | Description |
---|---|---|
name | String | The name of the event. |
properties | Codable | Additional properties about the event, such as product_id . |
Screen
The screen
method sends a screen
event. The screen title is required, and
additional information about the screen's category and properties may be
supplied.
Method signature:
func screen(title: String, category: String? = nil)
func screen<P: Codable>(title: String, category: String? = nil, properties: P?)
Method parameters:
Parameter | Type | Description |
---|---|---|
title | String | The screen's name. For example "Getting started" |
category | String | The screen's category. For example "Docs" |
properties | Codable | Additional properties about the event, such as from_link . |
Group
The group
method sends a group
event.
The id identifying the user's group is required. Additional traits on the group can be provided.
Method signature:
func group(groupId: String)
func group<T: Codable>(groupId: String, traits: T?)
Method parameters:
Parameter | Type | Description |
---|---|---|
groupId | String | The id for the group. |
traits | Codable | Additional traits about the group, such as company_name . |
Flush
The iOS SDK buffers events locally before sending them to Hightouch's servers. This minimizes the number of requests made by the SDK and makes the tracking non-blocking.
To force the local buffer to be sent to Hightouch immediately call the flush
method.
Method signature:
func flush()
Reset
The reset
method resets the identify
and group
for the local session.
Specifically, it resets the anonymousId
, userId
, referrer
, and traits
.
The reset
method should be called when users log out. This way, if the user
logs back in with another account, the userIds and traits from the different
sessions remain separate.
Method signature:
func reset()