PostHog
A Skip interface for integrating PostHog analytics into your cross-platform iOS and Android applications.
About PostHog
Section titled “About PostHog”PostHog ↗ is an open-source product analytics platform that helps you understand user behavior, track events, conduct A/B tests, and analyze feature usage. It provides powerful analytics capabilities including event tracking, user identification, feature flags, session recording, and more—all while giving you full control over your data.
To include this framework in your project, add the following
dependency to your Package.swift file:
let package = Package( name: "my-package", products: [ .library(name: "MyProduct", targets: ["MyTarget"]), ], dependencies: [ .package(url: "https://source.skip.tools/skip-posthog.git", "0.0.0"..<"2.0.0"), ], targets: [ .target(name: "MyTarget", dependencies: [ .product(name: "SkipPostHog", package: "skip-posthog") ]) ])API Compatibility
Section titled “API Compatibility”SkipPostHog provides the same API surface as the PostHog iOS SDK ↗, ensuring a familiar development experience. All methods behave identically on both iOS and Android platforms, allowing you to write once and deploy everywhere. On Android, the API calls are forwarded to their equivalents in the PostHog Android SDK ↗.
Configuration
Section titled “Configuration”Initialize PostHog when your app starts, typically in your app’s initialization code:
import SkipPostHog
PostHogSDK.shared.setup( PostHogConfig(apiKey: "your-api-key"))For self-hosted PostHog instances, specify your custom host:
PostHogSDK.shared.setup( PostHogConfig(apiKey: "your-api-key", host: "https://your-posthog-instance.com"))Usage Examples
Section titled “Usage Examples”Tracking Events
Section titled “Tracking Events”Capture events to understand what users are doing in your app:
// Simple eventPostHogSDK.shared.capture("button_clicked")
// Event with propertiesPostHogSDK.shared.capture( "purchase_completed", properties: [ "product_id": "abc123", "price": 29.99, "currency": "USD" ])Identifying Users
Section titled “Identifying Users”Associate events with specific users:
// Identify a userPostHogSDK.shared.identify("user_12345")
// Identify with user propertiesPostHogSDK.shared.identify( "user_12345", userProperties: [ "email": "user@example.com", "plan": "premium", "signup_date": "2024-01-15" ])Setting User Properties
Section titled “Setting User Properties”Update properties for the current user:
PostHogSDK.shared.identify( PostHogSDK.shared.getDistinctId(), userProperties: [ "theme_preference": "dark", "notifications_enabled": true ])Screen Tracking
Section titled “Screen Tracking”Track screen views to understand navigation patterns:
PostHogSDK.shared.screen("Home Screen")
// With additional propertiesPostHogSDK.shared.screen( "Product Details", properties: [ "product_id": "xyz789", "category": "Electronics" ])Aliasing Users
Section titled “Aliasing Users”Link anonymous users with identified users:
// When a user signs up or logs inPostHogSDK.shared.alias("user_12345")Feature Flags
Section titled “Feature Flags”Check feature flags to control feature rollouts:
// Check if a feature is enabledif PostHogSDK.shared.isFeatureEnabled("new_checkout_flow") { // Show new checkout flow} else { // Show original checkout flow}
// Get feature flag payloadif let payload = PostHogSDK.shared.getFeatureFlag("experiment_variant") as? String { switch payload { case "control": // Show control variant case "test": // Show test variant default: // Default behavior }}Resetting User Data
Section titled “Resetting User Data”Clear user data when logging out:
PostHogSDK.shared.reset()Advanced Configuration
Section titled “Advanced Configuration”Configure additional options during setup:
let config = PostHogConfig( apiKey: "your-api-key", host: "https://app.posthog.com")
// Customize capture settingsconfig.captureScreenViews = trueconfig.captureApplicationLifecycleEvents = trueconfig.flushAt = 20config.flushIntervalSeconds = 30
PostHogSDK.shared.setup(config)Getting the Distinct ID
Section titled “Getting the Distinct ID”Retrieve the current user’s distinct ID:
let distinctId = PostHogSDK.shared.getDistinctId()print("Current user ID: \(distinctId)")Privacy Considerations
Section titled “Privacy Considerations”PostHog respects user privacy. Make sure to:
- Comply with applicable privacy laws (GDPR, CCPA, etc.)
- Provide clear privacy policies to your users
- Only track necessary data
- Use
PostHogSDK.shared.optOut()to allow users to opt out of tracking
// Allow users to opt outPostHogSDK.shared.optOut()
// Re-enable trackingPostHogSDK.shared.optIn()Building
Section titled “Building”This project is a free Swift Package Manager module that uses the Skip plugin to transpile Swift into Kotlin.
Building the module requires that Skip be installed using
Homebrew ↗ with brew install skiptools/skip/skip.
This will also install the necessary build prerequisites:
Kotlin, Gradle, and the Android build tools.
Testing
Section titled “Testing”The module can be tested using the standard swift test command
or by running the test target for the macOS destination in Xcode,
which will run the Swift tests as well as the transpiled
Kotlin JUnit tests in the Robolectric Android simulation environment.
Parity testing can be performed with skip test,
which will output a table of the test results for both platforms.