Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/build-examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Build Examples
Comment thread
benjackwhite marked this conversation as resolved.
Resolved
on:
push:
branches:
- master
- main
- v3.0.0
pull_request:
jobs:
build:
runs-on: macos-13
steps:
- uses: actions/checkout@v4
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '15.0'
- name: Build Example
run: make buildExample
8 changes: 0 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@ jobs:
runs-on: macos-13
steps:
- uses: actions/checkout@v4

- uses: swift-actions/setup-swift@v1
with:
swift-version: "5.3.0"
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '15.0'
- name: Build SDK
run: make buildSdk
- name: Build Example
run: make buildExample
- name: Test SDK
run: make test
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Tests
on:
push:
branches:
- master
- main
- v3.0.0
pull_request:
jobs:
test:
runs-on: macos-13
steps:
- uses: actions/checkout@v4
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '15.0'
- name: Test SDK
run: make test
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: build buildSdk buildExample format swiftLint swiftFormat test lint bootstrap releaseCocoaPods
.PHONY: build buildSdk buildExample format swiftLint swiftFormat test testOnSimulator lint bootstrap releaseCocoaPods

build: buildSdk buildExample

Expand All @@ -16,9 +16,12 @@ swiftLint:
swiftFormat:
swiftformat . --swiftversion 5.3

test:
testOnSimulator:
set -o pipefail && xcodebuild test -project PostHog.xcodeproj -scheme PostHog -destination 'platform=iOS Simulator,name=iPhone 15,OS=17.0.1' | xcpretty

test:
swift test

lint:
swiftformat . --lint --swiftversion 5.3 && swiftlint

Expand Down
52 changes: 52 additions & 0 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/Quick/Quick.git", from: "6.0.0"),
.package(url: "https://github.com/Quick/Nimble.git", from: "12.0.0"),
.package(url: "https://github.com/AliSoftware/OHHTTPStubs.git", from: "9.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -25,7 +28,13 @@ let package = Package(
),
.testTarget(
name: "PostHogTests",
dependencies: ["PostHog"],
dependencies: [
"PostHog",
"Quick",
"Nimble",
"OHHTTPStubs",
.product(name: "OHHTTPStubsSwift", package: "OHHTTPStubs"),
],
Comment on lines +31 to +37

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is what had to be done to make it work in CI, in combination with swift test instead of the xcode command for some reason.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Xcode already defines those dependencies as SPM in its projects, but apparently, CI wasn't picking them up, maybe a Xcode version mismatch, an empty project worked just fine.

path: "PostHogTests"
),
]
Expand Down
124 changes: 66 additions & 58 deletions PostHog.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions PostHog/PostHogApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class PostHogApi {

do {
data = try JSONSerialization.data(withJSONObject: toSend)
} catch let error as NSError {
} catch {
Comment thread
benjackwhite marked this conversation as resolved.
Resolved
return completion(PostHogBatchUploadInfo(statusCode: nil, error: error))
}

Expand Down Expand Up @@ -109,7 +109,7 @@ class PostHogApi {

do {
data = try JSONSerialization.data(withJSONObject: toSend)
} catch let error as NSError {
} catch {
return completion(nil, error)
}

Expand All @@ -128,7 +128,7 @@ class PostHogApi {
do {
let jsonData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]
completion(jsonData, nil)
} catch let error as NSError {
} catch {
completion(nil, error)
}
}.resume()
Expand Down
4 changes: 4 additions & 0 deletions PostHog/PostHogConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import Foundation
@objc public var optOut: Bool = false
public static let defaultHost: String = "https://app.posthog.com"

// only internal
var disableReachabilityForTesting: Bool = false
var disableQueueTimerForTesting: Bool = false

@objc(apiKey:)
public init(
apiKey: String
Expand Down
5 changes: 2 additions & 3 deletions PostHog/PostHogContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class PostHogContext {
if deviceType != nil {
properties["$device_type"] = deviceType
}

#endif

return properties
Expand All @@ -87,8 +86,8 @@ class PostHogContext {
var properties: [String: Any] = [:]

#if os(iOS) || os(tvOS)
properties["$screen_width"] = UIScreen.main.bounds.width
properties["$screen_height"] = UIScreen.main.bounds.height
properties["$screen_width"] = Float(UIScreen.main.bounds.width)
properties["$screen_height"] = Float(UIScreen.main.bounds.height)
#endif

properties["$lib"] = "posthog-ios"
Expand Down
47 changes: 29 additions & 18 deletions PostHog/PostHogFeatureFlags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ class PostHogFeatureFlags {
private let featureFlagsLock = NSLock()
private var isLoadingFeatureFlags = false

private let dispatchQueue = DispatchQueue(label: "com.posthog.FeatureFlags", target: .global(qos: .utility))
private let dispatchQueue = DispatchQueue(label: "com.posthog.FeatureFlags",
target: .global(qos: .utility))

init(_ config: PostHogConfig, _ storage: PostHogStorage, _ api: PostHogApi) {
init(_ config: PostHogConfig,
_ storage: PostHogStorage,
_ api: PostHogApi)
{
self.config = config
self.storage = storage
self.api = api
Expand Down Expand Up @@ -52,7 +56,9 @@ class PostHogFeatureFlags {
let featureFlagPayloads = data?["featureFlagPayloads"] as? [String: Any]
else {
hedgeLog("Error: Decide response missing correct featureFlags format")
self.setLoading(false)

self.notifyAndRelease()

return callback()
}
let errorsWhileComputingFlags = data?["errorsWhileComputingFlags"] as? Bool ?? false
Expand All @@ -74,17 +80,21 @@ class PostHogFeatureFlags {
}
}

DispatchQueue.main.async {
NotificationCenter.default.post(name: PostHogSDK.didReceiveFeatureFlags, object: nil)
}

self.setLoading(false)
self.notifyAndRelease()

return callback()
}
}
}

private func notifyAndRelease() {
DispatchQueue.main.async {
NotificationCenter.default.post(name: PostHogSDK.didReceiveFeatureFlags, object: nil)
}

setLoading(false)
}

func getFeatureFlags() -> [String: Any]? {
var flags: [String: Any]?
featureFlagsLock.withLock {
Expand All @@ -103,9 +113,9 @@ class PostHogFeatureFlags {
let value = flags?[key]

if value != nil {
let boolValue = value as? Bool ?? false
if boolValue {
return boolValue
let boolValue = value as? Bool
if boolValue != nil {
return boolValue!
} else {
return true
}
Expand Down Expand Up @@ -135,14 +145,15 @@ class PostHogFeatureFlags {
return value
}

// The payload value is stored as a string and is not pre-parsed...
// We need to mimic the JSON.parse of JS which is what posthog-js uses
let jsonData = try? JSONSerialization.jsonObject(with: stringValue.data(using: .utf8)!, options: .fragmentsAllowed)

if jsonData == nil {
return value
do {
Comment thread
benjackwhite marked this conversation as resolved.
Resolved
// The payload value is stored as a string and is not pre-parsed...
// We need to mimic the JSON.parse of JS which is what posthog-js uses
return try JSONSerialization.jsonObject(with: stringValue.data(using: .utf8)!, options: .fragmentsAllowed)
} catch {
hedgeLog("Error parsing the object \(String(describing: value)): \(error)")
}

return jsonData
// fallbak to original value if not possible to serialize
return value
}
}
Loading