Track Events and Errors
SwiftyLaunch's AnalyticsKit makes it easy to capture events, errors and other data points in your app, that you can easily look up in your PostHog Dashboard. In this article, we'll go over the ways that you can do this.
Prerequisites
To capture events, first, make sure that AnalyticsKit is included in your target,
and imported into your file:
import AnalyticsKit
Now you can call the Analytics.capture()
function to capture events.
Out of the box, all SwiftyLaunch Modules, except for SharedKit include AnalyticsKit.
Basic Usage
Capturing an Event
Out of the box, SwiftyLaunch includes an Analytics.capture()
static function, that allows you to track differnet types of events.
Function Definition
class Analytics {
static public func capture(
_ eventType: EventType,
id: String,
longDescription: String? = nil,
source: EventSource,
fromView: String? = nil,
relevancy: EventRelevancy? = nil
) { }
}
eventType
: The type of the event, can be.info
,.error
,.success
, This will be prefixed in the resulting event id. See example below.id
: A unique identifier for the event. Snake Case recommended, Example:video_ad_viewed
.longDescription
(Optional): A longer description of the event. Example:User watched an ad with ad unit id 'XYZ' for 27 seconds
.source
: The source of the event. What caused it. Example:.auth
,.iap
.fromView
(Optional): The view from which the event was triggered. Example:InAppPurchaseView
.relevancy
(Optional): The relevancy of the event. Example:.low
,.medium
,.high
. Defaults to.medium
, and to.high
when the event is of type.error
.
Example Usage
import AnalyticsKit
func likePicture(withID pictureID: String) {
// ... picture liking logic
Analytics.capture(
.success,
id: "picture_liked",
longDescription: "User liked picture with ID \(pictureID)",
source: .general,
fromView: "HomeFeedView"
)
}
This will result in an event being sent to PostHog. If you go to the PostHog Dashboard, you'll see the following:
This shows the event in the PostHog dashboard with PostHog properties omitted (See the Hide PostHog Properties checkmark). Here are the full list of properties that are automatically captured with each event:
Event types
The EventType
enum defines the types of events that can be captured. By default, SwiftyLaunch creates an event type for every selected module, with an addition of a general
type.
As your app grows, you might want to add more event types.
For a social app, you might want to add a chat
and call
event type, which would be used to track events related to chat and call functionality in your app.
To add a new event type, simply add a new case to the EventType
enum in the Analytics.swift
file:
public enum EventSource: String {
case general = "general" // Catch-all for events that don't fit into any other category, be sure to create a distinct source where needed
case auth = "auth" // Stuff related to Auth / FirebaseAuth
case db = "db" // Stuff related to FirebaseFirestore
case crash = "crash" // Stuff related to Crashlytics (AnalyticsKit + FirebaseKit)
case iap = "iap" // Stuff related to In-App Purchases / RevenueCat
case notif = "notif" // Stuff related to Push Notifications / OneSignal
case aikit = "aikit" // Stuff related to AIKit
case adsKit = "adskit" // Stuff related to AIKit
case analytics = "analytics" // Stuff related to Analytics / PostHog
}
Flexbile captureEvent
function
When you call the previously mentioned Analytics.capture()
function, we take the
provided information and pass it to the captureEvent
function, which allows for a
granular control over the event properties.
Function Definition
class Analytics {
static public func captureEvent(_ event: String, properties: [String: Any]?) {}
}
event
: The name of the event. (Snake Case recommended). It's the actual event name that is visible in the PostHog dashboard.properties
(Optional): A dictionary of properties that you want to attach to the event.
By default properties
only includes a single entry: endpoint_source
, which is set to 'app'
(and to 'backend'
when called from a backend function).
Example Usage
Let's replicate the previous example using the captureEvent
function:
import AnalyticsKit
func likePicture(withID pictureID: String) {
// ... picture liking logic
Analytics.captureEvent(
"success_picture_liked", // (successful_event) + (actual_event_name)
properties: [
"source": "general",
"relevancy": "medium" // automatically added to all events with capture(), and defaults to "medium" if not provided
"long_description": "User liked picture with ID \(pictureID)",
"$screen_name": "HomeFeedView" // $ prefixed properties are reserved for PostHog, but we can override them
]
)
}