Skip to content

skiptools/skip-posthog

Repository files navigation

SkipPostHog

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

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.

Setup

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

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

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

Tracking Events

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

Identifying Users

Associate events with specific users:

// Identify a user
PostHogSDK.shared.identify("user_12345")

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

Setting User Properties

Update properties for the current user:

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

Screen Tracking

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

Aliasing Users

Link anonymous users with identified users:

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

Feature Flags

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
    }
}

Resetting User Data

Clear user data when logging out:

PostHogSDK.shared.reset()

Advanced Configuration

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)

Getting the Distinct ID

Retrieve the current user's distinct ID:

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

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 out
PostHogSDK.shared.optOut()

// Re-enable tracking
PostHogSDK.shared.optIn()

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

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.

License

This software is licensed under the GNU Lesser General Public License v3.0, with the following linking exception to clarify that distribution to restricted environments (e.g., app stores) is permitted:

This software is licensed under the LGPL3, included below. As a special exception to the GNU Lesser General Public License version 3 ("LGPL3"), the copyright holders of this Library give you permission to convey to a third party a Combined Work that links statically or dynamically to this Library without providing any Minimal Corresponding Source or Minimal Application Code as set out in 4d or providing the installation information set out in section 4e, provided that you comply with the other provisions of LGPL3 and provided that you meet, for the Application the terms and conditions of the license(s) which apply to the Application. Except as stated in this special exception, the provisions of LGPL3 will continue to comply in full to this Library. If you modify this Library, you may apply this exception to your version of this Library, but you are not obliged to do so. If you do not wish to do so, delete this exception statement from your version. This exception does not (and cannot) modify any license terms which apply to the Application, with which you must still comply.

About

PostHog integration with Skip

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages