Skip to content

PostHog

A Skip interface for integrating PostHog analytics into your cross-platform iOS and Android applications.

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")
])
]
)

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.

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")
)

Capture events to understand what users are doing in your app:

// Simple event
PostHogSDK.shared.capture("button_clicked")
// Event with properties
PostHogSDK.shared.capture(
"purchase_completed",
properties: [
"product_id": "abc123",
"price": 29.99,
"currency": "USD"
]
)

Associate events with specific users:

// Identify a user
PostHogSDK.shared.identify("user_12345")
// Identify with user properties
PostHogSDK.shared.identify(
"user_12345",
userProperties: [
"email": "user@example.com",
"plan": "premium",
"signup_date": "2024-01-15"
]
)

Update properties for the current user:

PostHogSDK.shared.identify(
PostHogSDK.shared.getDistinctId(),
userProperties: [
"theme_preference": "dark",
"notifications_enabled": true
]
)

Track screen views to understand navigation patterns:

PostHogSDK.shared.screen("Home Screen")
// With additional properties
PostHogSDK.shared.screen(
"Product Details",
properties: [
"product_id": "xyz789",
"category": "Electronics"
]
)

Link anonymous users with identified users:

// When a user signs up or logs in
PostHogSDK.shared.alias("user_12345")

Check feature flags to control feature rollouts:

// Check if a feature is enabled
if PostHogSDK.shared.isFeatureEnabled("new_checkout_flow") {
// Show new checkout flow
} else {
// Show original checkout flow
}
// Get feature flag payload
if let payload = PostHogSDK.shared.getFeatureFlag("experiment_variant") as? String {
switch payload {
case "control":
// Show control variant
case "test":
// Show test variant
default:
// Default behavior
}
}

Clear user data when logging out:

PostHogSDK.shared.reset()

Configure additional options during setup:

let config = PostHogConfig(
apiKey: "your-api-key",
host: "https://app.posthog.com"
)
// Customize capture settings
config.captureScreenViews = true
config.captureApplicationLifecycleEvents = true
config.flushAt = 20
config.flushIntervalSeconds = 30
PostHogSDK.shared.setup(config)

Retrieve the current user’s distinct ID:

let distinctId = PostHogSDK.shared.getDistinctId()
print("Current user ID: \(distinctId)")

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 out
PostHogSDK.shared.optOut()
// Re-enable tracking
PostHogSDK.shared.optIn()

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.

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.