iOS product analytics installation

PostHog is available through CocoaPods or you can add it as a Swift Package Manager based dependency.

CocoaPods

Podfile
pod "PostHog", "~> 3.0"

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:

Package.swift
dependencies: [
.package(url: "https://github.com/PostHog/posthog-ios.git", from: "3.0.0")
],

and then as a dependency for the Package target utilizing PostHog:

Package.swift
.target(
name: "myApp",
dependencies: [.product(name: "PostHog", package: "posthog-ios")]),

Configuration

Configuration is done through the PostHogConfig object. Here's a basic configuration example to get you started.

You can find more advanced configuration options in the configuration page.

Swift
import Foundation
import PostHog
import UIKit
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
let POSTHOG_API_KEY = "<ph_project_api_key>"
// usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com'
let POSTHOG_HOST = "https://us.i.posthog.com"
let config = PostHogConfig(apiKey: POSTHOG_API_KEY, host: POSTHOG_HOST)
PostHogSDK.shared.setup(config)
return true
}
}

Community questions

Was this page useful?

Questions about this page? or post a community question.