Snowplow Tracker in Swift

Hi, I’m Razvan and I am writing this small article because I started working with Snowplow and there was a lack of information regarding the implementation of Snowplow Tracker on Swift.

Yes, I’m talking about this pod: https://github.com/snowplow/snowplow-objc-tracker

Of course, you need to implement Storage part on a cloud, but that’s a different story.

If you have to implement the tracker in your iOS app, here is what you have to do.

  1. Install the pod: Add ‘SnowplowTracker’ to your Podfile project:
pod 'SnowplowTracker'

2. Create a class from where you can manage the singleton SnowPlow Tracker, let’s call it: SnowPlowManager.swift

import SnowplowTracker
class SnowplowManager: NSObject {
    @objc var tracker: SPTracker?
static var shared: SnowplowManager? = SnowplowManager.init()
}

3. You need to set the tracker so we will build a function in our SnowPlowManager so that we can enable the tracker when the app starts:

func setTracker() {
    let emitter = SPEmitter.build({ builder in
    builder?.setUrlEndpoint("www.testsite.com") // Here is where you set the URL where you send the data
    builder?.setProtocol(.https) // you can either use http or https
})
    tracker = SPTracker.build({ builder in
        builder?.setEmitter(emitter) // set the emitter of the tracker
        builder?.setAppId(Bundle.main.bundleIdentifier) // you can track the appId
        builder?.setAutotrackScreenViews(true) // these function autotracks all of the screen events with full detail
        builder?.setScreenContext(true) // get screen context like resolutions, width, height
        builder?.setInstallEvent(true) // get events about the first install of the app
        builder?.setApplicationContext(true) // get events about the device, app context
        builder?.setSessionContext(true) // get stats about the current session
        let subject = SPSubject.init(platformContext: true, andGeoContext: true) // You can set a subject where you can set several datas such as place, id and others
        subject?.setUserId("agent007")
        builder?.setSubject(subject) // add subject to the tracker
        builder?.setExceptionEvents(true) // also track exceptional events
        builder?.setLifecycleEvents(true) // these one is tracking the background/foreground stats of the sessions
    })
}

4. Start the tracker when the app starts, right in your AppDelegate. There you have a didFinishLaunchingWithOptions function. Add there a simple line:

SnowplowManager.shared?.setTracker()

5. If you want to send custom events from the app with the custom context of the app, here is how you can do.

func trackCustomEvent() {
     let posterSchema = "iglu:com.testsite/global_context/jsonschema/1-0-0"
// On data context you can add anything you want to send to the tracking event
     let data = [
     "device": "iOS", // check the device
     "hasEmail": true, // or false, you can have a function to verify that
     "locale": "en_US" // you can track the current language of the phone
     ] as [String : Any]
     let posterContext = SPSelfDescribingJson(schema: posterSchema, andData: data as NSObject?)
// Let's say you want to track a custom screen event and set the context and other things you wish
     let event = SPScreenView.build { (builder) in
          builder?.setName("eventiOS")
          builder?.setContexts([posterContext ?? ""])
     }
     tracker?.trackScreenViewEvent(event) // send the event
}

6. After that, you can easily use this last function whenever you want in the app by using:

SnowplowManager.shared?.trackCustomEvent()

That's it. You managed to successfully implement the SnowPlow Tracker in Swift. For any further question, just ask me.