Swift
The OpenPanel Swift SDK allows you to integrate OpenPanel analytics into your iOS, macOS, tvOS, and watchOS applications.
The OpenPanel Swift SDK allows you to integrate OpenPanel analytics into your iOS, macOS, tvOS, and watchOS applications.
Features
- Easy-to-use API for tracking events and user properties
- Automatic collection of app states
- Support for custom event properties
- Shared instance for easy access throughout your app
Requirements
- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+
- Xcode 12.0+
- Swift 5.3+
Installation
Step 1: Add Package via Swift Package Manager
You can add OpenPanel to an Xcode project by adding it as a package dependency.
- From the File menu, select Add Packages...
- Enter
https://github.com/Openpanel-dev/swift-sdkinto the package repository URL text field - Click Add Package
Alternatively, if you have a Package.swift file, you can add OpenPanel as a dependency:
Step 2: Import and Initialize
First, import the SDK in your Swift file:
Then, initialize the OpenPanel SDK with your client ID:
Configuration Options
Common options
apiUrl- The url of the openpanel API or your self-hosted instanceclientId- The client id of your applicationclientSecret- The client secret of your application (only required for server-side events)filter- A function that will be called before sending an event. If it returns false, the event will not be sentdisabled- If true, the library will not send any events
Additional Swift-specific options:
filter- A closure that will be called before tracking an event. If it returns false, the event will not be trackeddisabled- Set totrueto disable all event trackingautomaticTracking- Set totrueto automatically track app lifecycle events
Filter Example
Usage
Tracking Events
To track an event:
Identifying Users
To identify a user:
Setting Global Properties
To set properties that will be sent with every event:
Incrementing Properties
To increment a numeric property:
Decrementing Properties
To decrement a numeric property:
Advanced Usage
Disabling Tracking
You can temporarily disable tracking during initialization:
Custom Event Filtering
You can set up custom event filtering during initialization:
Automatic Tracking
The SDK automatically tracks app lifecycle events (app_opened and app_closed) if automaticTracking is set to true during initialization:
Thread Safety
The OpenPanel SDK is designed to be thread-safe. You can call its methods from any thread without additional synchronization.