Skip to content

Commit

Permalink
feat(iOS): add optional distinctId parameter to capture methods (#216)
Browse files Browse the repository at this point in the history
* feat(iOS): add optional distinctId parameter to capture methods

* chore: update CHANGELOG.md

* fix: test names

* fix: linting

* chore: update CHANGELOG.md

* refactor: refactor to only adding distinctid to only one overload

* chore: update PostHogObjCExample
  • Loading branch information
ioannisj authored Oct 16, 2024
1 parent 9d6cf7f commit 069c369
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- add optional distinctId parameter to capture methods ([#216](https://github.com/PostHog/posthog-ios/pull/216))

## 3.13.0 - 2024-10-14

- recording: session replay respect feature flag variants ([#209](https://github.com/PostHog/posthog-ios/pull/209))
Expand Down
37 changes: 19 additions & 18 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ let maxRetryDelay = 30.0
return
}

guard let queue = queue, let storageManager = config.storageManager else {
guard let queue, let storageManager = config.storageManager else {
return
}
let oldDistinctId = getDistinctId()
Expand Down Expand Up @@ -482,15 +482,15 @@ let maxRetryDelay = 30.0
public func capture(_ event: String,
properties: [String: Any]? = nil)
{
capture(event, properties: properties, userProperties: nil, userPropertiesSetOnce: nil, groups: nil)
capture(event, distinctId: nil, 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, groups: nil)
capture(event, distinctId: nil, properties: properties, userProperties: userProperties, userPropertiesSetOnce: nil, groups: nil)
}

@objc(captureWithEvent:properties:userProperties:userPropertiesSetOnce:)
Expand All @@ -499,7 +499,7 @@ let maxRetryDelay = 30.0
userProperties: [String: Any]? = nil,
userPropertiesSetOnce: [String: Any]? = nil)
{
capture(event, 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 @@ -510,8 +510,9 @@ let maxRetryDelay = 30.0
return false
}

@objc(captureWithEvent:properties:userProperties:userPropertiesSetOnce:groups:)
@objc(captureWithEvent:distinctId:properties:userProperties:userPropertiesSetOnce:groups:)
public func capture(_ event: String,
distinctId: String? = nil,
properties: [String: Any]? = nil,
userProperties: [String: Any]? = nil,
userPropertiesSetOnce: [String: Any]? = nil,
Expand All @@ -525,7 +526,7 @@ let maxRetryDelay = 30.0
return
}

guard let queue = queue else {
guard let queue else {
return
}

Expand All @@ -541,15 +542,15 @@ let maxRetryDelay = 30.0
}
}

let distinctId = 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 @@ -559,13 +560,13 @@ let maxRetryDelay = 30.0

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

// Session Replay has its own queue
if snapshotEvent {
guard let replayQueue = replayQueue else {
guard let replayQueue else {
return
}
replayQueue.add(posthogEvent)
Expand All @@ -589,7 +590,7 @@ let maxRetryDelay = 30.0
return
}

guard let queue = queue else {
guard let queue else {
return
}

Expand Down Expand Up @@ -629,7 +630,7 @@ let maxRetryDelay = 30.0
return
}

guard let queue = queue else {
guard let queue else {
return
}

Expand All @@ -648,7 +649,7 @@ let maxRetryDelay = 30.0
}

private func groups(_ newGroups: [String: String]) -> [String: String] {
guard let storage = storage else {
guard let storage else {
return [:]
}

Expand Down Expand Up @@ -684,7 +685,7 @@ let maxRetryDelay = 30.0
return
}

guard let queue = queue else {
guard let queue else {
return
}

Expand Down Expand Up @@ -747,7 +748,7 @@ let maxRetryDelay = 30.0
return
}

guard let featureFlags = featureFlags, let storageManager = config.storageManager else {
guard let featureFlags, let storageManager = config.storageManager else {
return
}

Expand All @@ -768,7 +769,7 @@ let maxRetryDelay = 30.0
return nil
}

guard let featureFlags = featureFlags else {
guard let featureFlags else {
return nil
}

Expand All @@ -786,7 +787,7 @@ let maxRetryDelay = 30.0
return false
}

guard let featureFlags = featureFlags else {
guard let featureFlags else {
return false
}

Expand All @@ -804,7 +805,7 @@ let maxRetryDelay = 30.0
return nil
}

guard let featureFlags = featureFlags else {
guard let featureFlags else {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
690FF04A2AE7DB3700A0B06B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 690FF0492AE7DB3700A0B06B /* Assets.xcassets */; };
690FF04D2AE7DB3700A0B06B /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 690FF04C2AE7DB3700A0B06B /* Preview Assets.xcassets */; };
690FF05A2AE7DD7500A0B06B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 690FF0592AE7DD7500A0B06B /* AppDelegate.swift */; };
DA8D37492CBF99D1005EBD27 /* PostHog in Frameworks */ = {isa = PBXBuildFile; productRef = DA8D37482CBF99D1005EBD27 /* PostHog */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -28,6 +29,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
DA8D37492CBF99D1005EBD27 /* PostHog in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -87,6 +89,7 @@
);
name = PostHogExampleWithSPM;
packageProductDependencies = (
DA8D37482CBF99D1005EBD27 /* PostHog */,
);
productName = PostHogExampleWithSPM;
productReference = 690FF0422AE7DB3600A0B06B /* PostHogExampleWithSPM.app */;
Expand Down Expand Up @@ -117,7 +120,7 @@
);
mainGroup = 690FF0392AE7DB3600A0B06B;
packageReferences = (
690FF05B2AE7E03B00A0B06B /* XCLocalSwiftPackageReference ".." */,
DA8D37472CBF99D1005EBD27 /* XCLocalSwiftPackageReference "../../posthog-ios" */,
);
productRefGroup = 690FF0432AE7DB3600A0B06B /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -359,11 +362,18 @@
/* End XCConfigurationList section */

/* Begin XCLocalSwiftPackageReference section */
690FF05B2AE7E03B00A0B06B /* XCLocalSwiftPackageReference ".." */ = {
DA8D37472CBF99D1005EBD27 /* XCLocalSwiftPackageReference "../../posthog-ios" */ = {
isa = XCLocalSwiftPackageReference;
relativePath = ..;
relativePath = "../../posthog-ios";
};
/* End XCLocalSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
DA8D37482CBF99D1005EBD27 /* PostHog */ = {
isa = XCSwiftPackageProductDependency;
productName = PostHog;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 690FF03A2AE7DB3600A0B06B /* Project object */;
}
28 changes: 25 additions & 3 deletions PostHogObjCExample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,31 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
object:nil];

PostHogConfig *config = [[PostHogConfig alloc] apiKey:@"_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI"];
config.preloadFeatureFlags = NO;
config.preloadFeatureFlags = YES;
[[PostHogSDK shared] debug:YES];
[[PostHogSDK shared] setup:config];

NSString *event = @"theEvent";
NSString *distinctId = @"theCustomDistinctId";
NSDictionary *properties = @{@"source": @"iOS App", @"state": @"running"};
NSDictionary *userProperties = @{@"userAlive": @YES, @"userAge": @50};
NSDictionary *userPropertiesSetOnce = @{@"signupDate": @"2024-10-16"};
NSDictionary *groups = @{@"groupName": @"developers"};

[[PostHogSDK shared] captureWithEvent:event
distinctId:distinctId
properties:properties
userProperties:userProperties
userPropertiesSetOnce:userPropertiesSetOnce
groups:groups
];

[[PostHogSDK shared] captureWithEvent:event
properties:properties
userProperties:userProperties
userPropertiesSetOnce:userPropertiesSetOnce
];

// NSLog(@"getDistinctId: %@", [[PostHogSDK shared] getDistinctId]);
// NSLog(@"getAnonymousId: %@", [[PostHogSDK shared] getAnonymousId]);
//
Expand Down Expand Up @@ -72,8 +94,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
// [[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props];
// [[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props userProperties:userProps];
// [[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props userProperties:userProps userPropertiesSetOnce:userPropsOnce];
// [[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props userProperties:userProps userPropertiesSetOnce:userPropsOnce groupProperties:groupProps];
//
// [[PostHogSDK shared] captureWithEvent:@"theEvent" distinctId:@"custom_distinct_id" properties:props userProperties:userProps userPropertiesSetOnce:userPropsOnce groupProperties:groupProps];
//
// [[PostHogSDK shared] groupWithType:@"theType" key:@"theKey"];
// [[PostHogSDK shared] groupWithType:@"theType" key:@"theKey" groupProperties:groupProps];
//
Expand Down
19 changes: 19 additions & 0 deletions PostHogTests/PostHogSDKTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ class PostHogSDKTest: QuickSpec {
sut.close()
}

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

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

let events = getBatchedEvents(server)

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

sut.reset()
sut.close()
}

it("captures an identify event") {
let sut = self.getSut()

Expand Down

0 comments on commit 069c369

Please sign in to comment.