Skip to content

Commit

Permalink
Release 3.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujg-00 committed Sep 1, 2021
1 parent 33a562f commit d7422d8
Show file tree
Hide file tree
Showing 16 changed files with 365 additions and 2 deletions.
2 changes: 1 addition & 1 deletion SensorsAnalyticsSDK.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "SensorsAnalyticsSDK"
s.version = "3.1.4"
s.version = "3.1.5"
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}" }
Expand Down
25 changes: 25 additions & 0 deletions SensorsAnalyticsSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@
CB6EBAF522855222003CFBA8 /* ca.der.cer in Resources */ = {isa = PBXBuildFile; fileRef = CB6EBAF122855222003CFBA8 /* ca.der.cer */; };
CB6EBAF622855222003CFBA8 /* SANetworkTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CB6EBAF222855222003CFBA8 /* SANetworkTests.m */; };
F214CE58249A07DF00A2633D /* SADatabaseUnitTest.m in Sources */ = {isa = PBXBuildFile; fileRef = F214CE57249A07DF00A2633D /* SADatabaseUnitTest.m */; };
F22E1B1B26A55C8A0033A748 /* SAAppPageLeaveTracker.h in Headers */ = {isa = PBXBuildFile; fileRef = F22E1B1926A55C8A0033A748 /* SAAppPageLeaveTracker.h */; };
F22E1B1C26A55C8A0033A748 /* SAAppPageLeaveTracker.m in Sources */ = {isa = PBXBuildFile; fileRef = F22E1B1A26A55C8A0033A748 /* SAAppPageLeaveTracker.m */; };
F244AE3C26B170F000D8C60B /* UIViewController+SAPageView.h in Headers */ = {isa = PBXBuildFile; fileRef = F244AE3A26B170F000D8C60B /* UIViewController+SAPageView.h */; };
F244AE3D26B170F000D8C60B /* UIViewController+SAPageView.m in Sources */ = {isa = PBXBuildFile; fileRef = F244AE3B26B170F000D8C60B /* UIViewController+SAPageView.m */; };
F277F5BF25CF9A43009B5CE6 /* SAApplicationDelegateProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = F277F5B125CF9A43009B5CE6 /* SAApplicationDelegateProxy.m */; };
F277F5C025CF9A43009B5CE6 /* SAUNUserNotificationCenterDelegateProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = F277F5B225CF9A43009B5CE6 /* SAUNUserNotificationCenterDelegateProxy.h */; };
F277F5C125CF9A43009B5CE6 /* SANotificationUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = F277F5B325CF9A43009B5CE6 /* SANotificationUtil.m */; };
Expand Down Expand Up @@ -590,6 +594,10 @@
CB6EBAF122855222003CFBA8 /* ca.der.cer */ = {isa = PBXFileReference; lastKnownFileType = file; path = ca.der.cer; sourceTree = "<group>"; };
CB6EBAF222855222003CFBA8 /* SANetworkTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SANetworkTests.m; sourceTree = "<group>"; };
F214CE57249A07DF00A2633D /* SADatabaseUnitTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = SADatabaseUnitTest.m; path = SensorsAnalyticsTests/Tracker/SADatabaseUnitTest.m; sourceTree = SOURCE_ROOT; };
F22E1B1926A55C8A0033A748 /* SAAppPageLeaveTracker.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SAAppPageLeaveTracker.h; sourceTree = "<group>"; };
F22E1B1A26A55C8A0033A748 /* SAAppPageLeaveTracker.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SAAppPageLeaveTracker.m; sourceTree = "<group>"; };
F244AE3A26B170F000D8C60B /* UIViewController+SAPageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIViewController+SAPageView.h"; sourceTree = "<group>"; };
F244AE3B26B170F000D8C60B /* UIViewController+SAPageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+SAPageView.m"; sourceTree = "<group>"; };
F277F5B125CF9A43009B5CE6 /* SAApplicationDelegateProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SAApplicationDelegateProxy.m; sourceTree = "<group>"; };
F277F5B225CF9A43009B5CE6 /* SAUNUserNotificationCenterDelegateProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SAUNUserNotificationCenterDelegateProxy.h; sourceTree = "<group>"; };
F277F5B325CF9A43009B5CE6 /* SANotificationUtil.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SANotificationUtil.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1047,6 +1055,7 @@
A8356DB72656459A00FD64AA /* SAAppTracker.m */,
A8356DA82656459A00FD64AA /* AppClick */,
A8356D8C2656459A00FD64AA /* AppEnd */,
F22E1B1826A559DE0033A748 /* AppPageLeave */,
A8356DAB2656459A00FD64AA /* AppStart */,
A8356DB82656459A00FD64AA /* AppViewScreen */,
A8356DB22656459A00FD64AA /* ElementInfo */,
Expand Down Expand Up @@ -1278,6 +1287,17 @@
path = Resources;
sourceTree = "<group>";
};
F22E1B1826A559DE0033A748 /* AppPageLeave */ = {
isa = PBXGroup;
children = (
F22E1B1926A55C8A0033A748 /* SAAppPageLeaveTracker.h */,
F22E1B1A26A55C8A0033A748 /* SAAppPageLeaveTracker.m */,
F244AE3A26B170F000D8C60B /* UIViewController+SAPageView.h */,
F244AE3B26B170F000D8C60B /* UIViewController+SAPageView.m */,
);
path = AppPageLeave;
sourceTree = "<group>";
};
F2346B7D25E0AE5600AA3684 /* HookDelegate */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1399,6 +1419,8 @@
4DD1296A25F8E451008C0B1E /* SAViewNode.h in Headers */,
A8356DC52656459A00FD64AA /* SAAppTracker.h in Headers */,
883BAAB02669CD18008105D2 /* SAExceptionManager.h in Headers */,
F22E1B1B26A55C8A0033A748 /* SAAppPageLeaveTracker.h in Headers */,
881A41FF253D7B5000854F69 /* SATrackTimer.h in Headers */,
4DD1285925F872A4008C0B1E /* SAEnumDescription.h in Headers */,
4DD1285D25F872A4008C0B1E /* SAVisualizedConnection.h in Headers */,
A8356DCF2656459A00FD64AA /* SAGestureViewProcessorFactory.h in Headers */,
Expand Down Expand Up @@ -1427,6 +1449,7 @@
A8356DC82656459A00FD64AA /* SAScrollViewDelegateProxy.h in Headers */,
A8356DBE2656459A00FD64AA /* UIView+AutoTrack.h in Headers */,
4D41D9D325FF7E9300D856F4 /* UIViewController+SAElementPath.h in Headers */,
F244AE3C26B170F000D8C60B /* UIViewController+SAPageView.h in Headers */,
4DD1285325F872A4008C0B1E /* SAVisualizedAbstractMessage.h in Headers */,
4D2D53992591EB2100805141 /* SADatabase.h in Headers */,
45A5655F263C174300C9C41B /* SAPresetProperty.h in Headers */,
Expand Down Expand Up @@ -1640,6 +1663,7 @@
A8BCC4AC2686C42D00B72040 /* SASecretKey.m in Sources */,
A82E895C267D918100475757 /* SAECCEncryptor.m in Sources */,
A8356DD02656459A00FD64AA /* SAGeneralGestureViewProcessor.m in Sources */,
F22E1B1C26A55C8A0033A748 /* SAAppPageLeaveTracker.m in Sources */,
881A4194253D7B4F00854F69 /* SATrackTimer.m in Sources */,
A82E895D267D918100475757 /* SAECCPluginEncryptor.m in Sources */,
A8CC22352685E50C00E96A03 /* SARemoteConfigCommonOperator.m in Sources */,
Expand Down Expand Up @@ -1728,6 +1752,7 @@
A8356DCC2656459A00FD64AA /* SAGestureTarget.m in Sources */,
4D6AE7FD26086E9300A9CB08 /* SAVisualizedEventCheck.m in Sources */,
887602D52685E0AA00C5EA51 /* SAModuleManager+Visualized.m in Sources */,
F244AE3D26B170F000D8C60B /* UIViewController+SAPageView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
38 changes: 38 additions & 0 deletions SensorsAnalyticsSDK/AutoTrack/AppPageLeave/SAAppPageLeaveTracker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// SAAppPageLeaveTracker.h
// SensorsAnalyticsSDK
//
// Created by 陈玉国 on 2021/7/19.
// Copyright © 2021 Sensors Data Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//


#import "SAAppTracker.h"

NS_ASSUME_NONNULL_BEGIN

@interface SAAppPageLeaveTracker : SAAppTracker

@property (nonatomic, strong) NSMutableDictionary<NSString *, NSMutableDictionary *> *timestamp;

- (void)trackEvents;
- (void)trackPageEnter:(UIViewController *)viewController;
- (void)trackPageLeave:(UIViewController *)viewController;
- (NSDictionary *)propertiesWithViewController:(UIViewController *)viewController;
- (BOOL)shouldTrackViewController:(UIViewController *)viewController;

@end

NS_ASSUME_NONNULL_END
171 changes: 171 additions & 0 deletions SensorsAnalyticsSDK/AutoTrack/AppPageLeave/SAAppPageLeaveTracker.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
//
// SAAppPageLeaveTracker.m
// SensorsAnalyticsSDK
//
// Created by 陈玉国 on 2021/7/19.
// Copyright © 2021 Sensors Data Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#if ! __has_feature(objc_arc)
#error This file must be compiled with ARC. Either turn on ARC for the project or use -fobjc-arc flag on this file.
#endif

#import "SAAppPageLeaveTracker.h"
#import "SAAutoTrackUtils.h"
#import "SensorsAnalyticsSDK+SAAutoTrack.h"
#import "SAConstants+Private.h"
#import "SAConstants+Private.h"
#import "SAAppLifecycle.h"
#import "SensorsAnalyticsSDK+Private.h"

@interface SAAppPageLeaveTracker ()

@property (nonatomic, copy, readwrite) NSDictionary *referrerProperties;
@property (nonatomic, copy, readwrite) NSString *referrerURL;

@end

@implementation SAAppPageLeaveTracker

- (instancetype)init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appLifecycleStateWillChange:) name:kSAAppLifecycleStateWillChangeNotification object:nil];
}
return self;
}

- (NSString *)eventId {
return kSAEventNameAppPageLeave;
}

- (void)trackEvents {
[self.timestamp enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSMutableDictionary * _Nonnull obj, BOOL * _Nonnull stop) {
NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970];
NSNumber *timestamp = obj[kSAPageLeaveTimestamp];
NSTimeInterval startTimestamp = [timestamp doubleValue];
NSMutableDictionary *tempProperties = [[NSMutableDictionary alloc] initWithDictionary:obj[kSAPageLeaveAutoTrackProperties]];
NSTimeInterval duration = (currentTimestamp - startTimestamp) < 24 * 60 * 60 ? (currentTimestamp - startTimestamp) : 0;
tempProperties[kSAEventDurationProperty] = @([[NSString stringWithFormat:@"%.3f", duration] floatValue]);
[self trackWithProperties:[tempProperties copy]];
}];
}

- (void)trackPageEnter:(UIViewController *)viewController {
if (![self shouldTrackViewController:viewController]) {
return;
}
NSString *address = [NSString stringWithFormat:@"%p", viewController];
if (self.timestamp[address]) {
return;
}
NSMutableDictionary *properties = [[NSMutableDictionary alloc] init];
properties[kSAPageLeaveTimestamp] = @([[NSDate date] timeIntervalSince1970]);
properties[kSAPageLeaveAutoTrackProperties] = [self propertiesWithViewController:viewController];
self.timestamp[address] = properties;
}

- (void)trackPageLeave:(UIViewController *)viewController {
if (![self shouldTrackViewController:viewController]) {
return;
}
NSString *address = [NSString stringWithFormat:@"%p", viewController];
if (!self.timestamp[address]) {
return;
}
NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970];
NSMutableDictionary *properties = self.timestamp[address];
NSNumber *timestamp = properties[kSAPageLeaveTimestamp];
NSTimeInterval startTimestamp = [timestamp doubleValue];
NSMutableDictionary *tempProperties = [[NSMutableDictionary alloc] initWithDictionary:properties[kSAPageLeaveAutoTrackProperties]];
NSTimeInterval duration = (currentTimestamp - startTimestamp) < 24 * 60 * 60 ? (currentTimestamp - startTimestamp) : 0;
tempProperties[kSAEventDurationProperty] = @([[NSString stringWithFormat:@"%.3f", duration] floatValue]);
[self trackWithProperties:tempProperties];
self.timestamp[address] = nil;
}

- (void)trackWithProperties:(NSDictionary *)properties {
SAPresetEventObject *object = [[SAPresetEventObject alloc] initWithEventId:kSAEventNameAppPageLeave];
[SensorsAnalyticsSDK.sharedInstance asyncTrackEventObject:object properties:properties];
}

- (void)appLifecycleStateWillChange:(NSNotification *)notification {
NSDictionary *userInfo = notification.userInfo;
SAAppLifecycleState newState = [userInfo[kSAAppLifecycleNewStateKey] integerValue];
// 冷(热)启动
if (newState == SAAppLifecycleStateStart) {
[self.timestamp enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSMutableDictionary * _Nonnull obj, BOOL * _Nonnull stop) {
obj[kSAPageLeaveTimestamp] = @([[NSDate date] timeIntervalSince1970]);
}];
return;
}

// 退出
if (newState == SAAppLifecycleStateEnd) {
[self trackEvents];
}
}

- (NSDictionary *)propertiesWithViewController:(UIViewController<SAAutoTrackViewControllerProperty> *)viewController {
NSMutableDictionary *eventProperties = [[NSMutableDictionary alloc] init];
NSDictionary *autoTrackProperties = [SAAutoTrackUtils propertiesWithViewController:viewController];
[eventProperties addEntriesFromDictionary:autoTrackProperties];

NSString *currentURL;
if ([viewController conformsToProtocol:@protocol(SAScreenAutoTracker)] && [viewController respondsToSelector:@selector(getScreenUrl)]) {
UIViewController<SAScreenAutoTracker> *screenAutoTrackerController = (UIViewController<SAScreenAutoTracker> *)viewController;
currentURL = [screenAutoTrackerController getScreenUrl];
}
currentURL = [currentURL isKindOfClass:NSString.class] ? currentURL : NSStringFromClass(viewController.class);

// 添加 $url 和 $referrer 页面浏览相关属性
NSDictionary *newProperties = [self propertiesWithURL:currentURL eventProperties:eventProperties];

return newProperties;
}

- (NSDictionary *)propertiesWithURL:(NSString *)currentURL eventProperties:(NSDictionary *)eventProperties {
NSString *referrerURL = self.referrerURL;
NSMutableDictionary *newProperties = [NSMutableDictionary dictionaryWithDictionary:eventProperties];

// 客户自定义属性中包含 $url 时,以客户自定义内容为准
if (!newProperties[kSAEventPropertyScreenUrl]) {
newProperties[kSAEventPropertyScreenUrl] = currentURL;
}
// 客户自定义属性中包含 $referrer 时,以客户自定义内容为准
if (referrerURL && !newProperties[kSAEventPropertyScreenReferrerUrl]) {
newProperties[kSAEventPropertyScreenReferrerUrl] = referrerURL;
}
// $referrer 内容以最终页面浏览事件中的 $url 为准
self.referrerURL = newProperties[kSAEventPropertyScreenUrl];
self.referrerProperties = newProperties;

return newProperties;
}

- (BOOL)shouldTrackViewController:(UIViewController *)viewController {
NSDictionary *autoTrackBlackList = [self autoTrackViewControllerBlackList];
NSDictionary *appViewScreenBlackList = autoTrackBlackList[kSAEventNameAppViewScreen];
return ![self isViewController:viewController inBlackList:appViewScreenBlackList];
}

- (NSMutableDictionary<NSString *,NSMutableDictionary *> *)timestamp {
if (!_timestamp) {
_timestamp = [[NSMutableDictionary alloc] init];
}
return _timestamp;
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// UIViewController+PageView.h
// SensorsAnalyticsSDK
//
// Created by 陈玉国 on 2021/7/19.
// Copyright © 2021 Sensors Data Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIViewController (PageLeave)

- (void)sensorsdata_pageLeave_viewDidAppear:(BOOL)animated;

- (void)sensorsdata_pageLeave_viewDidDisappear:(BOOL)animated;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// UIViewController+PageView.m
// SensorsAnalyticsSDK
//
// Created by 陈玉国 on 2021/7/19.
// Copyright © 2021 Sensors Data Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#if ! __has_feature(objc_arc)
#error This file must be compiled with ARC. Either turn on ARC for the project or use -fobjc-arc flag on this file.
#endif

#import "UIViewController+SAPageView.h"
#import "SAAutoTrackManager.h"


@implementation UIViewController (PageLeave)

- (void)sensorsdata_pageLeave_viewDidAppear:(BOOL)animated {
SAAppPageLeaveTracker *tracker = [SAAutoTrackManager sharedInstance].appPageLeaveTracker;
[tracker trackPageEnter:self];
[self sensorsdata_pageLeave_viewDidAppear:animated];
}

- (void)sensorsdata_pageLeave_viewDidDisappear:(BOOL)animated {
SAAppPageLeaveTracker *tracker = [SAAutoTrackManager sharedInstance].appPageLeaveTracker;
[tracker trackPageLeave:self];
[self sensorsdata_pageLeave_viewDidDisappear:animated];
}



@end
2 changes: 2 additions & 0 deletions SensorsAnalyticsSDK/AutoTrack/SAAutoTrackManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import "SAModuleProtocol.h"
#import "SAAppClickTracker.h"
#import "SAAppViewScreenTracker.h"
#import "SAAppPageLeaveTracker.h"

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -33,6 +34,7 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, strong) SAAppClickTracker *appClickTracker;
@property (nonatomic, strong) SAAppViewScreenTracker *appViewScreenTracker;
@property (nonatomic, strong) SAAppPageLeaveTracker *appPageLeaveTracker;

+ (SAAutoTrackManager *)sharedInstance;

Expand Down
Loading

0 comments on commit d7422d8

Please sign in to comment.