This library uses an internal queue to make calls fast and non-blocking. It also batches requests and flushes asynchronously, making it perfect to use in any part of your mobile app.
Installation
PostHog is available through CocoaPods and Carthage or you can add it as a Swift Package Manager based dependency.
CocoaPods
Carthage
Swift Package Manager
Add PostHog as a dependency in your Xcode project "Package Dependencies" and select the project target for your app, as appropriate.
For a Swift Package Manager based project, add PostHog as a dependency in your "Package.swift" file's Package dependencies section:
and then as a dependency for the Package target utilizing PostHog:
Configuration
With Objective-C
With Swift
Making calls
Identify
We highly recommend reading our section on Identifying users to better understand how to correctly use this method.
When you start tracking events with PostHog, each user gets an anonymous ID that is used to identify them in the system.
In order to link this anonymous user with someone from your database, use the identify call. 
Identify lets you add metadata to your users so you can easily identify who they are in PostHog, as well as do things like segment users by these properties.
An identify call requires:
- distinct_idwhich uniquely identifies your user in your database
- propertieswith a dictionary of key:value pairs
For example:
The most obvious place to make this call is whenever a user signs up, or when they update their information.
When you call identify, all previously tracked anonymous events will be linked to the user.
Capture
Capture allows you to send events related to anything a user does within your system, which you can later use in PostHog to find patterns in usage, work out which features to improve, or find out where people are giving up.
A capture call requires:
- event nameto specify the event- We recommend using [noun][verb], like movie playedormovie updatedto easily identify what your events mean later on.
 
- We recommend using [noun][verb], like 
Optionally you can submit:
- properties, which can be an array with any information you'd like to add
For example:
Setting user properties via an event
To set properties on your users via an event, you can leverage the event properties $set and $set_once.
$set
Example
Usage
When capturing an event, you can pass a property called $set as an event property, and specify its value to be an object with properties to be set on the user that will be associated with the user who triggered the event.
$set_once
Example
Usage
$set_once works just like $set, except that it will only set the property if the user doesn't already have that property set.
Flush
You can set the number of events in the configuration that should queue before flushing.
Setting this to 1 will send events immediately and will use more battery. This is set to 20 by default.
You can also manually flush the queue:
Reset
To reset the user's ID and anonymous ID, call reset. Usually you would do this right after the user logs out.
Sending screen views
With configuration.recordScreenViews set as YES, PostHog will try to record all screen changes automatically.
If you want to manually send a new screen capture event, use the screen function.
A note about IDFA (identifier for advertisers) collection in iOS 14
Starting with iOS 14, Apple will further restrict apps that track users. Any references to Apple's AdSupport framework, even in strings, will trip the App Store's static analysis.
Hence starting with posthog-ios version 1.2.0 we have removed all references to Apple's AdSupport framework.
Group analytics
Group analytics allows you to associate the events for that person's session with a group (e.g. teams, organizations, etc.). Read the Group Analytics guide for more information.
Note: This is a paid feature and is not available on the open-source or free cloud plan. Learn more here.
- Associate the events for this session with a group
- Associate the events for this session with a group AND update the properties of that group
The name is a special property which is used in the PostHog UI for the name of the Group. If you don't specify a name property, the group ID will be used instead.
Feature Flags
Feature Flags allow you to safely deploy and roll back new features.
Note: This requires minimum library version 2.0.0
Here's how you can use them:
- Check if a feature is enabled:
- Trigger a reload of the feature flags:
- By default, this function will send a $feature_flag_calledevent to your instance every time it's called so you're able to do analytics.
Get a flag value
If you're using multivariate feature flags, you can also get the value of the flag, as well as whether or not it is enabled.
All configuration options
The configuration element contains several other settings you can toggle:
Thank you
This library is largely based on the analytics-ios package.