From a951be258fd815531e666f155dd25cf0787f663b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E7=8E=89=E5=9B=BD?= Date: Fri, 27 Oct 2023 18:04:38 +0800 Subject: [PATCH] Release 4.5.21 --- SensorsAnalyticsSDK.podspec | 2 +- .../Core/Builder/SAIdentifier.h | 4 +++ .../Core/Builder/SAIdentifier.m | 27 +++++++++++++++++++ .../Core/SensorsAnalyticsSDK+Public.h | 3 +++ .../Core/SensorsAnalyticsSDK.m | 12 ++++++++- .../Builder/SAIdentifierTest.m | 13 +++++++++ 6 files changed, 59 insertions(+), 2 deletions(-) diff --git a/SensorsAnalyticsSDK.podspec b/SensorsAnalyticsSDK.podspec index fadaec78..24343c2c 100644 --- a/SensorsAnalyticsSDK.podspec +++ b/SensorsAnalyticsSDK.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "SensorsAnalyticsSDK" - s.version = "4.5.20" + s.version = "4.5.21" s.summary = "The official iOS SDK of Sensors Analytics." s.homepage = "http://www.sensorsdata.cn" s.source = { :git => 'https://github.com/sensorsdata/sa-sdk-ios.git', :tag => "v#{s.version}" } diff --git a/SensorsAnalyticsSDK/Core/Builder/SAIdentifier.h b/SensorsAnalyticsSDK/Core/Builder/SAIdentifier.h index 01dd7e59..c30323ab 100644 --- a/SensorsAnalyticsSDK/Core/Builder/SAIdentifier.h +++ b/SensorsAnalyticsSDK/Core/Builder/SAIdentifier.h @@ -130,6 +130,10 @@ extern NSString * const kSALoginIdSpliceKey; /// 用于合并 H5 传过来的业务 ID - (NSDictionary *)mergeH5Identities:(NSDictionary *)identities eventType:(SAEventType)eventType; + +/// ID3 reset anonymous identity +- (void)resetAnonymousIdentity:(nullable NSString *)identity; + @end NS_ASSUME_NONNULL_END diff --git a/SensorsAnalyticsSDK/Core/Builder/SAIdentifier.m b/SensorsAnalyticsSDK/Core/Builder/SAIdentifier.m index 3dc12e51..0c04fa8f 100644 --- a/SensorsAnalyticsSDK/Core/Builder/SAIdentifier.m +++ b/SensorsAnalyticsSDK/Core/Builder/SAIdentifier.m @@ -692,4 +692,31 @@ - (void)archiveIdentities:(NSDictionary *)identities { } } +- (void)resetAnonymousIdentity:(NSString *)identity { + if (self.loginId) { + SALogError(@"you should not use this function when login"); + return; + } + NSString *anonymousId = identity; + if (!anonymousId || anonymousId.length == 0) { + anonymousId = [NSUUID UUID].UUIDString; + } + dispatch_async(self.queue, ^{ + //set anonymous id + self.anonymousId = anonymousId; + [self archiveAnonymousId:anonymousId]; + //set anonymousId in identities + NSMutableDictionary *identities = [self.identities mutableCopy]; + if (identities[kSAIdentitiesUniqueID]) { + identities[kSAIdentitiesUniqueID] = anonymousId; + } + if (identities[kSAIdentitiesUUID]) { + identities[kSAIdentitiesUUID] = anonymousId; + } + self.identities = identities; + [self archiveIdentities:identities]; + [[NSNotificationCenter defaultCenter] postNotificationName:SA_TRACK_RESETANONYMOUSID_NOTIFICATION object:nil]; + }); +} + @end diff --git a/SensorsAnalyticsSDK/Core/SensorsAnalyticsSDK+Public.h b/SensorsAnalyticsSDK/Core/SensorsAnalyticsSDK+Public.h index 01a3997a..25325c45 100644 --- a/SensorsAnalyticsSDK/Core/SensorsAnalyticsSDK+Public.h +++ b/SensorsAnalyticsSDK/Core/SensorsAnalyticsSDK+Public.h @@ -219,6 +219,9 @@ extern NSString * const SensorsAnalyticsIdentityKeyEmail; */ - (void)unbind:(NSString *)key value:(NSString *)value; +/// ID3 reset anonymous identity +- (void)resetAnonymousIdentity:(nullable NSString *)identity; + #pragma mark - trackTimer /** 开始事件计时 diff --git a/SensorsAnalyticsSDK/Core/SensorsAnalyticsSDK.m b/SensorsAnalyticsSDK/Core/SensorsAnalyticsSDK.m index 8cf6ba96..5289af8d 100755 --- a/SensorsAnalyticsSDK/Core/SensorsAnalyticsSDK.m +++ b/SensorsAnalyticsSDK/Core/SensorsAnalyticsSDK.m @@ -64,7 +64,7 @@ #import "SALimitKeyManager.h" #import "NSDictionary+SACopyProperties.h" -#define VERSION @"4.5.20" +#define VERSION @"4.5.21" void *SensorsAnalyticsQueueTag = &SensorsAnalyticsQueueTag; @@ -669,6 +669,16 @@ - (void)unbind:(NSString *)key value:(NSString *)value { }); } +-(void)resetAnonymousIdentity:(NSString *)identity { + if (identity && ![identity isKindOfClass:[NSString class]]) { + SALogError(@"anonymous identity should be string"); + return; + } + dispatch_async(self.serialQueue, ^{ + [self.identifier resetAnonymousIdentity:identity]; + }); +} + - (void)track:(NSString *)event { [self track:event withProperties:nil]; } diff --git a/SensorsAnalyticsTests/Builder/SAIdentifierTest.m b/SensorsAnalyticsTests/Builder/SAIdentifierTest.m index 991bab06..d7183456 100644 --- a/SensorsAnalyticsTests/Builder/SAIdentifierTest.m +++ b/SensorsAnalyticsTests/Builder/SAIdentifierTest.m @@ -580,4 +580,17 @@ - (void)testH5EventForNativeInitial { XCTAssertNotNil(native[kIDFV]); } +- (void)testResetAnonymousIdentity { + NSString *distinctId = _identifier.distinctId; + [_identifier resetAnonymousIdentity:nil]; + XCTAssertTrue(![distinctId isEqualToString:_identifier.distinctId]); + NSString *newDistinctId = _identifier.distinctId; + [_identifier resetAnonymousIdentity:@"dedea-deada-dadaed-deded"]; + XCTAssertTrue(![newDistinctId isEqualToString:_identifier.distinctId]); + XCTAssertTrue([@"dedea-deada-dadaed-deded" isEqualToString:_identifier.distinctId]); + [_identifier loginWithKey:@"testKey" loginId:@"testLoginId"]; + [_identifier resetAnonymousIdentity:@"dedea-deada-dadaed-deded"]; + XCTAssertTrue([_identifier.distinctId isEqualToString:@"testKey+testLoginId"]); +} + @end