Skip to content

Commit 069c369

Browse files
authored
feat(iOS): add optional distinctId parameter to capture methods (#216)
* 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
1 parent 9d6cf7f commit 069c369

File tree

5 files changed

+78
-24
lines changed

5 files changed

+78
-24
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Next
22

3+
- add optional distinctId parameter to capture methods ([#216](https://github.com/PostHog/posthog-ios/pull/216))
4+
35
## 3.13.0 - 2024-10-14
46

57
- recording: session replay respect feature flag variants ([#209](https://github.com/PostHog/posthog-ios/pull/209))

PostHog/PostHogSDK.swift

+19-18
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ let maxRetryDelay = 30.0
442442
return
443443
}
444444

445-
guard let queue = queue, let storageManager = config.storageManager else {
445+
guard let queue, let storageManager = config.storageManager else {
446446
return
447447
}
448448
let oldDistinctId = getDistinctId()
@@ -482,15 +482,15 @@ let maxRetryDelay = 30.0
482482
public func capture(_ event: String,
483483
properties: [String: Any]? = nil)
484484
{
485-
capture(event, properties: properties, userProperties: nil, userPropertiesSetOnce: nil, groups: nil)
485+
capture(event, distinctId: nil, properties: properties, userProperties: nil, userPropertiesSetOnce: nil, groups: nil)
486486
}
487487

488488
@objc(captureWithEvent:properties:userProperties:)
489489
public func capture(_ event: String,
490490
properties: [String: Any]? = nil,
491491
userProperties: [String: Any]? = nil)
492492
{
493-
capture(event, properties: properties, userProperties: userProperties, userPropertiesSetOnce: nil, groups: nil)
493+
capture(event, distinctId: nil, properties: properties, userProperties: userProperties, userPropertiesSetOnce: nil, groups: nil)
494494
}
495495

496496
@objc(captureWithEvent:properties:userProperties:userPropertiesSetOnce:)
@@ -499,7 +499,7 @@ let maxRetryDelay = 30.0
499499
userProperties: [String: Any]? = nil,
500500
userPropertiesSetOnce: [String: Any]? = nil)
501501
{
502-
capture(event, properties: properties, userProperties: userProperties, userPropertiesSetOnce: userPropertiesSetOnce, groups: nil)
502+
capture(event, distinctId: nil, properties: properties, userProperties: userProperties, userPropertiesSetOnce: userPropertiesSetOnce, groups: nil)
503503
}
504504

505505
private func isOptOutState() -> Bool {
@@ -510,8 +510,9 @@ let maxRetryDelay = 30.0
510510
return false
511511
}
512512

513-
@objc(captureWithEvent:properties:userProperties:userPropertiesSetOnce:groups:)
513+
@objc(captureWithEvent:distinctId:properties:userProperties:userPropertiesSetOnce:groups:)
514514
public func capture(_ event: String,
515+
distinctId: String? = nil,
515516
properties: [String: Any]? = nil,
516517
userProperties: [String: Any]? = nil,
517518
userPropertiesSetOnce: [String: Any]? = nil,
@@ -525,7 +526,7 @@ let maxRetryDelay = 30.0
525526
return
526527
}
527528

528-
guard let queue = queue else {
529+
guard let queue else {
529530
return
530531
}
531532

@@ -541,15 +542,15 @@ let maxRetryDelay = 30.0
541542
}
542543
}
543544

544-
let distinctId = getDistinctId()
545+
let eventDistinctId = distinctId ?? getDistinctId()
545546

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

552-
let properties = buildProperties(distinctId: distinctId,
553+
let properties = buildProperties(distinctId: eventDistinctId,
553554
properties: sanitizeDicionary(properties),
554555
userProperties: sanitizeDicionary(userProperties),
555556
userPropertiesSetOnce: sanitizeDicionary(userPropertiesSetOnce),
@@ -559,13 +560,13 @@ let maxRetryDelay = 30.0
559560

560561
let posthogEvent = PostHogEvent(
561562
event: event,
562-
distinctId: distinctId,
563+
distinctId: eventDistinctId,
563564
properties: sanitizedProperties
564565
)
565566

566567
// Session Replay has its own queue
567568
if snapshotEvent {
568-
guard let replayQueue = replayQueue else {
569+
guard let replayQueue else {
569570
return
570571
}
571572
replayQueue.add(posthogEvent)
@@ -589,7 +590,7 @@ let maxRetryDelay = 30.0
589590
return
590591
}
591592

592-
guard let queue = queue else {
593+
guard let queue else {
593594
return
594595
}
595596

@@ -629,7 +630,7 @@ let maxRetryDelay = 30.0
629630
return
630631
}
631632

632-
guard let queue = queue else {
633+
guard let queue else {
633634
return
634635
}
635636

@@ -648,7 +649,7 @@ let maxRetryDelay = 30.0
648649
}
649650

650651
private func groups(_ newGroups: [String: String]) -> [String: String] {
651-
guard let storage = storage else {
652+
guard let storage else {
652653
return [:]
653654
}
654655

@@ -684,7 +685,7 @@ let maxRetryDelay = 30.0
684685
return
685686
}
686687

687-
guard let queue = queue else {
688+
guard let queue else {
688689
return
689690
}
690691

@@ -747,7 +748,7 @@ let maxRetryDelay = 30.0
747748
return
748749
}
749750

750-
guard let featureFlags = featureFlags, let storageManager = config.storageManager else {
751+
guard let featureFlags, let storageManager = config.storageManager else {
751752
return
752753
}
753754

@@ -768,7 +769,7 @@ let maxRetryDelay = 30.0
768769
return nil
769770
}
770771

771-
guard let featureFlags = featureFlags else {
772+
guard let featureFlags else {
772773
return nil
773774
}
774775

@@ -786,7 +787,7 @@ let maxRetryDelay = 30.0
786787
return false
787788
}
788789

789-
guard let featureFlags = featureFlags else {
790+
guard let featureFlags else {
790791
return false
791792
}
792793

@@ -804,7 +805,7 @@ let maxRetryDelay = 30.0
804805
return nil
805806
}
806807

807-
guard let featureFlags = featureFlags else {
808+
guard let featureFlags else {
808809
return nil
809810
}
810811

PostHogExampleWithSPM/PostHogExampleWithSPM.xcodeproj/project.pbxproj

+13-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
690FF04A2AE7DB3700A0B06B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 690FF0492AE7DB3700A0B06B /* Assets.xcassets */; };
1313
690FF04D2AE7DB3700A0B06B /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 690FF04C2AE7DB3700A0B06B /* Preview Assets.xcassets */; };
1414
690FF05A2AE7DD7500A0B06B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 690FF0592AE7DD7500A0B06B /* AppDelegate.swift */; };
15+
DA8D37492CBF99D1005EBD27 /* PostHog in Frameworks */ = {isa = PBXBuildFile; productRef = DA8D37482CBF99D1005EBD27 /* PostHog */; };
1516
/* End PBXBuildFile section */
1617

1718
/* Begin PBXFileReference section */
@@ -28,6 +29,7 @@
2829
isa = PBXFrameworksBuildPhase;
2930
buildActionMask = 2147483647;
3031
files = (
32+
DA8D37492CBF99D1005EBD27 /* PostHog in Frameworks */,
3133
);
3234
runOnlyForDeploymentPostprocessing = 0;
3335
};
@@ -87,6 +89,7 @@
8789
);
8890
name = PostHogExampleWithSPM;
8991
packageProductDependencies = (
92+
DA8D37482CBF99D1005EBD27 /* PostHog */,
9093
);
9194
productName = PostHogExampleWithSPM;
9295
productReference = 690FF0422AE7DB3600A0B06B /* PostHogExampleWithSPM.app */;
@@ -117,7 +120,7 @@
117120
);
118121
mainGroup = 690FF0392AE7DB3600A0B06B;
119122
packageReferences = (
120-
690FF05B2AE7E03B00A0B06B /* XCLocalSwiftPackageReference ".." */,
123+
DA8D37472CBF99D1005EBD27 /* XCLocalSwiftPackageReference "../../posthog-ios" */,
121124
);
122125
productRefGroup = 690FF0432AE7DB3600A0B06B /* Products */;
123126
projectDirPath = "";
@@ -359,11 +362,18 @@
359362
/* End XCConfigurationList section */
360363

361364
/* Begin XCLocalSwiftPackageReference section */
362-
690FF05B2AE7E03B00A0B06B /* XCLocalSwiftPackageReference ".." */ = {
365+
DA8D37472CBF99D1005EBD27 /* XCLocalSwiftPackageReference "../../posthog-ios" */ = {
363366
isa = XCLocalSwiftPackageReference;
364-
relativePath = ..;
367+
relativePath = "../../posthog-ios";
365368
};
366369
/* End XCLocalSwiftPackageReference section */
370+
371+
/* Begin XCSwiftPackageProductDependency section */
372+
DA8D37482CBF99D1005EBD27 /* PostHog */ = {
373+
isa = XCSwiftPackageProductDependency;
374+
productName = PostHog;
375+
};
376+
/* End XCSwiftPackageProductDependency section */
367377
};
368378
rootObject = 690FF03A2AE7DB3600A0B06B /* Project object */;
369379
}

PostHogObjCExample/AppDelegate.m

+25-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,31 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
2828
object:nil];
2929

3030
PostHogConfig *config = [[PostHogConfig alloc] apiKey:@"_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI"];
31-
config.preloadFeatureFlags = NO;
31+
config.preloadFeatureFlags = YES;
3232
[[PostHogSDK shared] debug:YES];
3333
[[PostHogSDK shared] setup:config];
34+
35+
NSString *event = @"theEvent";
36+
NSString *distinctId = @"theCustomDistinctId";
37+
NSDictionary *properties = @{@"source": @"iOS App", @"state": @"running"};
38+
NSDictionary *userProperties = @{@"userAlive": @YES, @"userAge": @50};
39+
NSDictionary *userPropertiesSetOnce = @{@"signupDate": @"2024-10-16"};
40+
NSDictionary *groups = @{@"groupName": @"developers"};
41+
42+
[[PostHogSDK shared] captureWithEvent:event
43+
distinctId:distinctId
44+
properties:properties
45+
userProperties:userProperties
46+
userPropertiesSetOnce:userPropertiesSetOnce
47+
groups:groups
48+
];
49+
50+
[[PostHogSDK shared] captureWithEvent:event
51+
properties:properties
52+
userProperties:userProperties
53+
userPropertiesSetOnce:userPropertiesSetOnce
54+
];
55+
3456
// NSLog(@"getDistinctId: %@", [[PostHogSDK shared] getDistinctId]);
3557
// NSLog(@"getAnonymousId: %@", [[PostHogSDK shared] getAnonymousId]);
3658
//
@@ -72,8 +94,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
7294
// [[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props];
7395
// [[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props userProperties:userProps];
7496
// [[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props userProperties:userProps userPropertiesSetOnce:userPropsOnce];
75-
// [[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props userProperties:userProps userPropertiesSetOnce:userPropsOnce groupProperties:groupProps];
76-
//
97+
// [[PostHogSDK shared] captureWithEvent:@"theEvent" distinctId:@"custom_distinct_id" properties:props userProperties:userProps userPropertiesSetOnce:userPropsOnce groupProperties:groupProps];
98+
//
7799
// [[PostHogSDK shared] groupWithType:@"theType" key:@"theKey"];
78100
// [[PostHogSDK shared] groupWithType:@"theType" key:@"theKey" groupProperties:groupProps];
79101
//

PostHogTests/PostHogSDKTest.swift

+19
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ class PostHogSDKTest: QuickSpec {
8888
sut.close()
8989
}
9090

91+
it("captures the capture event with a custom distinctId") {
92+
let sut = self.getSut()
93+
94+
sut.capture("event",
95+
distinctId: "the_custom_distinct_id",
96+
properties: ["foo": "bar"],
97+
userProperties: ["userProp": "value"],
98+
userPropertiesSetOnce: ["userPropOnce": "value"],
99+
groups: ["groupProp": "value"])
100+
101+
let events = getBatchedEvents(server)
102+
103+
expect(events.count) == 1
104+
expect(events.first!.distinctId) == "the_custom_distinct_id"
105+
106+
sut.reset()
107+
sut.close()
108+
}
109+
91110
it("captures an identify event") {
92111
let sut = self.getSut()
93112

0 commit comments

Comments
 (0)