Skip to content

Commit

Permalink
refactor: refactor to only adding distinctid to only one overload
Browse files Browse the repository at this point in the history
  • Loading branch information
ioannisj committed Oct 16, 2024
1 parent ad472d7 commit 36d4638
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 87 deletions.
30 changes: 10 additions & 20 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -478,38 +478,28 @@ let maxRetryDelay = 30.0
capture(event, properties: nil, userProperties: nil, userPropertiesSetOnce: nil, groups: nil)
}

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

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

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

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

private func isOptOutState() -> Bool {
Expand All @@ -522,7 +512,7 @@ let maxRetryDelay = 30.0

@objc(captureWithEvent:distinctId:properties:userProperties:userPropertiesSetOnce:groups:)
public func capture(_ event: String,
distinctId distinctIdParam: String? = nil,
distinctId: String? = nil,
properties: [String: Any]? = nil,
userProperties: [String: Any]? = nil,
userPropertiesSetOnce: [String: Any]? = nil,
Expand Down Expand Up @@ -552,15 +542,15 @@ let maxRetryDelay = 30.0
}
}

let distinctId = distinctIdParam ?? getDistinctId()
let eventDistinctId = distinctId ?? getDistinctId()

// if the user isn't identified but passed userProperties, userPropertiesSetOnce or groups,
// we should still enable person processing since this is intentional
if userProperties?.isEmpty == false || userPropertiesSetOnce?.isEmpty == false || groups?.isEmpty == false {
requirePersonProcessing()
}

let properties = buildProperties(distinctId: distinctId,
let properties = buildProperties(distinctId: eventDistinctId,
properties: sanitizeDicionary(properties),
userProperties: sanitizeDicionary(userProperties),
userPropertiesSetOnce: sanitizeDicionary(userPropertiesSetOnce),
Expand All @@ -570,7 +560,7 @@ let maxRetryDelay = 30.0

let posthogEvent = PostHogEvent(
event: event,
distinctId: distinctId,
distinctId: eventDistinctId,
properties: sanitizedProperties
)

Expand Down
68 changes: 1 addition & 67 deletions PostHogTests/PostHogSDKTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,73 +88,7 @@ class PostHogSDKTest: QuickSpec {
sut.close()
}

it("captures the capture event with capture(event,distinctId) overload") {
let sut = self.getSut()

sut.capture("event1",
distinctId: "the_custom_distinct_id")

let events = getBatchedEvents(server)

expect(events.count) == 1
expect(events.first!.distinctId) == "the_custom_distinct_id"

sut.reset()
sut.close()
}

it("captures the capture event with capture(event,distinctId, properties) overload") {
let sut = self.getSut()

sut.capture("event",
distinctId: "the_custom_distinct_id",
properties: ["foo": "bar"])

let events = getBatchedEvents(server)

expect(events.count) == 1
expect(events.first!.distinctId) == "the_custom_distinct_id"

sut.reset()
sut.close()
}

it("captures the capture event with capture(event,distinctId, properties, userProperties) overload") {
let sut = self.getSut()

sut.capture("event",
distinctId: "the_custom_distinct_id",
properties: ["foo": "bar"],
userProperties: ["userProp": "value"])

let events = getBatchedEvents(server)

expect(events.count) == 1
expect(events.first!.distinctId) == "the_custom_distinct_id"

sut.reset()
sut.close()
}

it("captures the capture event with capture(event,distinctId, properties, userProperties, userPropertiesSetOnce) overload") {
let sut = self.getSut()

sut.capture("event",
distinctId: "the_custom_distinct_id",
properties: ["foo": "bar"],
userProperties: ["userProp": "value"],
userPropertiesSetOnce: ["userPropOnce": "value"])

let events = getBatchedEvents(server)

expect(events.count) == 1
expect(events.first!.distinctId) == "the_custom_distinct_id"

sut.reset()
sut.close()
}

it("captures the capture event with capture(event,distinctId, properties, userProperties, userPropertiesSetOnce, group) overload") {
it("captures the capture event with a custom distinctId") {
let sut = self.getSut()

sut.capture("event",
Expand Down

0 comments on commit 36d4638

Please sign in to comment.