Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Next

- chore: change host to new address ([#139](https://github.com/PostHog/posthog-ios/pull/139))
- fix: rename groupProperties to groups for capture methods ([#140](https://github.com/PostHog/posthog-android/pull/140))

## 3.4.0 - 2024-05-23

Expand Down
26 changes: 15 additions & 11 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// swiftlint:disable file_length

//
// PostHogSDK.swift
// PostHogSDK
Expand Down Expand Up @@ -234,7 +236,7 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
properties: [String: Any]?,
userProperties: [String: Any]? = nil,
userPropertiesSetOnce: [String: Any]? = nil,
groupProperties: [String: Any]? = nil,
groups: [String: String]? = nil,
appendSharedProps: Bool = true) -> [String: Any]
{
var props: [String: Any] = [:]
Expand All @@ -257,10 +259,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
if userPropertiesSetOnce != nil {
props["$set_once"] = (userPropertiesSetOnce ?? [:])
}
if groupProperties != nil {
if groups != nil {
// $groups are also set via the dynamicContext
let currentGroups = props["$groups"] as? [String: Any] ?? [:]
let mergedGroups = currentGroups.merging(groupProperties ?? [:]) { current, _ in current }
let currentGroups = props["$groups"] as? [String: String] ?? [:]
let mergedGroups = currentGroups.merging(groups ?? [:]) { current, _ in current }
props["$groups"] = mergedGroups
}
}
Expand Down Expand Up @@ -396,22 +398,22 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
}

@objc public func capture(_ event: String) {
capture(event, properties: nil, userProperties: nil, userPropertiesSetOnce: nil, groupProperties: nil)
capture(event, properties: nil, userProperties: nil, userPropertiesSetOnce: nil, groups: nil)
}

@objc(captureWithEvent:properties:)
public func capture(_ event: String,
properties: [String: Any]? = nil)
{
capture(event, properties: properties, userProperties: nil, userPropertiesSetOnce: nil, groupProperties: nil)
capture(event, properties: properties, userProperties: nil, userPropertiesSetOnce: nil, groups: nil)
}

@objc(captureWithEvent:properties:userProperties:)
public func capture(_ event: String,
properties: [String: Any]? = nil,
userProperties: [String: Any]? = nil)
{
capture(event, properties: properties, userProperties: userProperties, userPropertiesSetOnce: nil, groupProperties: nil)
capture(event, properties: properties, userProperties: userProperties, userPropertiesSetOnce: nil, groups: nil)
}

@objc(captureWithEvent:properties:userProperties:userPropertiesSetOnce:)
Expand All @@ -420,7 +422,7 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
userProperties: [String: Any]? = nil,
userPropertiesSetOnce: [String: Any]? = nil)
{
capture(event, properties: properties, userProperties: userProperties, userPropertiesSetOnce: userPropertiesSetOnce, groupProperties: nil)
capture(event, properties: properties, userProperties: userProperties, userPropertiesSetOnce: userPropertiesSetOnce, groups: nil)
}

private func isOptOutState() -> Bool {
Expand All @@ -431,12 +433,12 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
return false
}

@objc(captureWithEvent:properties:userProperties:userPropertiesSetOnce:groupProperties:)
@objc(captureWithEvent:properties:userProperties:userPropertiesSetOnce:groups:)
public func capture(_ event: String,
properties: [String: Any]? = nil,
userProperties: [String: Any]? = nil,
userPropertiesSetOnce: [String: Any]? = nil,
groupProperties: [String: Any]? = nil)
groups: [String: String]? = nil)
{
if !isEnabled() {
return
Expand Down Expand Up @@ -474,7 +476,7 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
properties: sanitizeDicionary(properties),
userProperties: sanitizeDicionary(userProperties),
userPropertiesSetOnce: sanitizeDicionary(userPropertiesSetOnce),
groupProperties: sanitizeDicionary(groupProperties),
groups: groups,
appendSharedProps: !snapshotEvent)
)

Expand Down Expand Up @@ -1002,3 +1004,5 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
}
#endif
}

// swiftlint:enable file_length
20 changes: 9 additions & 11 deletions PostHogTests/PostHogSDKTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class PostHogSDKTest: QuickSpec {
properties: ["foo": "bar"],
userProperties: ["userProp": "value"],
userPropertiesSetOnce: ["userPropOnce": "value"],
groupProperties: ["groupProp": "value"])
groups: ["groupProp": "value"])

let events = getBatchedEvents(server)

Expand All @@ -76,8 +76,8 @@ class PostHogSDKTest: QuickSpec {
let setOnce = event.properties["$set_once"] as? [String: Any] ?? [:]
expect(setOnce["userPropOnce"] as? String) == "value"

let groupProps = event.properties["$groups"] as? [String: Any] ?? [:]
expect(groupProps["groupProp"] as? String) == "value"
let groupProps = event.properties["$groups"] as? [String: String] ?? [:]
expect(groupProps["groupProp"]) == "value"

sut.reset()
sut.close()
Expand Down Expand Up @@ -451,8 +451,8 @@ class PostHogSDKTest: QuickSpec {
expect(requests.count) == 1
let request = requests.first

let groups = request!["$groups"] as? [String: Any]
expect(groups!["some-type"] as? String) == "some-key"
let groups = request!["$groups"] as? [String: String]
expect(groups!["some-type"]) == "some-key"

sut.reset()
sut.close()
Expand All @@ -472,9 +472,9 @@ class PostHogSDKTest: QuickSpec {
expect(events.count) == 3
let event = events.last!

let groups = event.properties["$groups"] as? [String: Any]
expect(groups!["some-type"] as? String) == "some-key"
expect(groups!["some-type-2"] as? String) == "some-key-2"
let groups = event.properties["$groups"] as? [String: String]
expect(groups!["some-type"]) == "some-key"
expect(groups!["some-type-2"]) == "some-key-2"

sut.reset()
sut.close()
Expand Down Expand Up @@ -541,9 +541,7 @@ class PostHogSDKTest: QuickSpec {
userProperties: ["userProp": "value",
"test2": UserDefaults.standard],
userPropertiesSetOnce: ["userPropOnce": "value",
"test3": UserDefaults.standard],
groupProperties: ["groupProp": "value",
"test4": UserDefaults.standard])
"test3": UserDefaults.standard])

let events = getBatchedEvents(server)

Expand Down