Skip to content

Commit a951be2

Browse files
陈玉国陈玉国
authored andcommitted
Release 4.5.21
1 parent 520ac45 commit a951be2

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed

SensorsAnalyticsSDK.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "SensorsAnalyticsSDK"
3-
s.version = "4.5.20"
3+
s.version = "4.5.21"
44
s.summary = "The official iOS SDK of Sensors Analytics."
55
s.homepage = "http://www.sensorsdata.cn"
66
s.source = { :git => 'https://github.com/sensorsdata/sa-sdk-ios.git', :tag => "v#{s.version}" }

SensorsAnalyticsSDK/Core/Builder/SAIdentifier.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ extern NSString * const kSALoginIdSpliceKey;
130130
/// 用于合并 H5 传过来的业务 ID
131131
- (NSDictionary *)mergeH5Identities:(NSDictionary *)identities eventType:(SAEventType)eventType;
132132

133+
134+
/// ID3 reset anonymous identity
135+
- (void)resetAnonymousIdentity:(nullable NSString *)identity;
136+
133137
@end
134138

135139
NS_ASSUME_NONNULL_END

SensorsAnalyticsSDK/Core/Builder/SAIdentifier.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,4 +692,31 @@ - (void)archiveIdentities:(NSDictionary *)identities {
692692
}
693693
}
694694

695+
- (void)resetAnonymousIdentity:(NSString *)identity {
696+
if (self.loginId) {
697+
SALogError(@"you should not use this function when login");
698+
return;
699+
}
700+
NSString *anonymousId = identity;
701+
if (!anonymousId || anonymousId.length == 0) {
702+
anonymousId = [NSUUID UUID].UUIDString;
703+
}
704+
dispatch_async(self.queue, ^{
705+
//set anonymous id
706+
self.anonymousId = anonymousId;
707+
[self archiveAnonymousId:anonymousId];
708+
//set anonymousId in identities
709+
NSMutableDictionary *identities = [self.identities mutableCopy];
710+
if (identities[kSAIdentitiesUniqueID]) {
711+
identities[kSAIdentitiesUniqueID] = anonymousId;
712+
}
713+
if (identities[kSAIdentitiesUUID]) {
714+
identities[kSAIdentitiesUUID] = anonymousId;
715+
}
716+
self.identities = identities;
717+
[self archiveIdentities:identities];
718+
[[NSNotificationCenter defaultCenter] postNotificationName:SA_TRACK_RESETANONYMOUSID_NOTIFICATION object:nil];
719+
});
720+
}
721+
695722
@end

SensorsAnalyticsSDK/Core/SensorsAnalyticsSDK+Public.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ extern NSString * const SensorsAnalyticsIdentityKeyEmail;
219219
*/
220220
- (void)unbind:(NSString *)key value:(NSString *)value;
221221

222+
/// ID3 reset anonymous identity
223+
- (void)resetAnonymousIdentity:(nullable NSString *)identity;
224+
222225
#pragma mark - trackTimer
223226
/**
224227
开始事件计时

SensorsAnalyticsSDK/Core/SensorsAnalyticsSDK.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
#import "SALimitKeyManager.h"
6565
#import "NSDictionary+SACopyProperties.h"
6666

67-
#define VERSION @"4.5.20"
67+
#define VERSION @"4.5.21"
6868

6969
void *SensorsAnalyticsQueueTag = &SensorsAnalyticsQueueTag;
7070

@@ -669,6 +669,16 @@ - (void)unbind:(NSString *)key value:(NSString *)value {
669669
});
670670
}
671671

672+
-(void)resetAnonymousIdentity:(NSString *)identity {
673+
if (identity && ![identity isKindOfClass:[NSString class]]) {
674+
SALogError(@"anonymous identity should be string");
675+
return;
676+
}
677+
dispatch_async(self.serialQueue, ^{
678+
[self.identifier resetAnonymousIdentity:identity];
679+
});
680+
}
681+
672682
- (void)track:(NSString *)event {
673683
[self track:event withProperties:nil];
674684
}

SensorsAnalyticsTests/Builder/SAIdentifierTest.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,4 +580,17 @@ - (void)testH5EventForNativeInitial {
580580
XCTAssertNotNil(native[kIDFV]);
581581
}
582582

583+
- (void)testResetAnonymousIdentity {
584+
NSString *distinctId = _identifier.distinctId;
585+
[_identifier resetAnonymousIdentity:nil];
586+
XCTAssertTrue(![distinctId isEqualToString:_identifier.distinctId]);
587+
NSString *newDistinctId = _identifier.distinctId;
588+
[_identifier resetAnonymousIdentity:@"dedea-deada-dadaed-deded"];
589+
XCTAssertTrue(![newDistinctId isEqualToString:_identifier.distinctId]);
590+
XCTAssertTrue([@"dedea-deada-dadaed-deded" isEqualToString:_identifier.distinctId]);
591+
[_identifier loginWithKey:@"testKey" loginId:@"testLoginId"];
592+
[_identifier resetAnonymousIdentity:@"dedea-deada-dadaed-deded"];
593+
XCTAssertTrue([_identifier.distinctId isEqualToString:@"testKey+testLoginId"]);
594+
}
595+
583596
@end

0 commit comments

Comments
 (0)