Skip to content

Commit

Permalink
Release 1.6.12
Browse files Browse the repository at this point in the history
1. 增加 Protocol SAScreenAutoTracker,允许用户使用 AutoTrack
功能时,为页面添加自定义属性和URL Schema
2. 增加宏 SENSORS_ANALYTICS_DISABLE_VTRACK,屏蔽可视化埋点功能
3. 由于 IOS 10 的限制,默认屏蔽基于 COOKIE 的渠道追踪,可以通过
SENSORS_ANALYTICS_IOS_MATCHING_WITH_COOKIE 打开 Cookie 匹配
  • Loading branch information
Yuhan ZOU committed Sep 23, 2016
1 parent fa97811 commit f819b40
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 18 deletions.
3 changes: 2 additions & 1 deletion SensorsAnalyticsSDK/HelloSensorsAnalytics/DemoController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#define DemoController_h

#import <UIKit/UIKit.h>
#import "SensorsAnalyticsSDK.h"

@interface DemoController : UITableViewController
@interface DemoController : UITableViewController<SAScreenAutoTracker>

@end

Expand Down
10 changes: 8 additions & 2 deletions SensorsAnalyticsSDK/HelloSensorsAnalytics/DemoController.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@

#import "zlib.h"

#import "SensorsAnalyticsSDK.h"

#import "DemoController.h"

@implementation DemoController

- (NSDictionary *)getTrackProperties {
return @{@"shuxing" : @"Gaga"};
}

- (NSString *)getScreenUrl {
return @"WoShiYiGeURL";
}

- (void)testTrack {
[[SensorsAnalyticsSDK sharedInstance] track:@"testTrack" withProperties:@{@"test": @"test"}];
}
Expand Down
2 changes: 1 addition & 1 deletion SensorsAnalyticsSDK/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 = "1.6.11"
s.version = "1.6.12"
s.summary = "The offical 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
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@
"SENSORS_ANALYTICS_IDFA=1",
"$(inherited)",
"SENSORS_ANALYTICS_ENABLE_LOG=1",
"SENSORS_ANALYTICS_DISABLE_VTRACK=1",
);
INFOPLIST_FILE = HelloSensorsAnalytics/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
Expand Down
21 changes: 21 additions & 0 deletions SensorsAnalyticsSDK/SensorsAnalyticsSDK/SensorsAnalyticsSDK.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ typedef NS_ENUM(NSInteger, SensorsAnalyticsAppPushService) {
SensorsAnalyticsAppPushXiaomi,
};

/**
* @abstract
* 自动追踪(AutoTrack)中,实现该 Protocal 的 Controller 对象可以通过接口向自动采集的事件中加入属性
*
* @discussion
* 属性的约束请参考 <code>track:withProperties:</code>
*/
@protocol SAAutoTracker

@required
-(NSDictionary *)getTrackProperties;

@end

@protocol SAScreenAutoTracker<SAAutoTracker>

@required
-(NSString *) getScreenUrl;

@end

/**
* @class
* SensorsAnalyticsSDK类
Expand Down
104 changes: 90 additions & 14 deletions SensorsAnalyticsSDK/SensorsAnalyticsSDK/SensorsAnalyticsSDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#import <WebKit/WebKit.h>
#endif

#define VERSION @"1.6.11"
#define VERSION @"1.6.12"

#define PROPERTY_LENGTH_LIMITATION 8191

Expand All @@ -52,6 +52,10 @@
NSString* const RESUME_FROM_BACKGROUND_PROPERTY = @"$resume_from_background";
// App 浏览页面名称
NSString* const SCREEN_NAME_PROPERTY = @"$screen_name";
// App 浏览页面 Url
NSString* const SCREEN_URL_PROPERTY = @"$url";
// App 浏览页面 Referrer Url
NSString* const SCREEN_REFERRER_URL_PROPERTY = @"$referrer";
// App 推送相关:
NSString* const APP_PUSH_ID_PROPERTY_BAIDU = @"$app_push_id_baidu";
NSString* const APP_PUSH_ID_PROPERTY_JIGUANG = @"$app_push_id_jiguang";
Expand Down Expand Up @@ -110,6 +114,7 @@ @implementation SensorsAnalyticsSDK {
NSDateFormatter *_dateFormatter;
BOOL _autoTrack; // 自动采集事件
BOOL _appRelaunched; // App 从后台恢复
NSString *_referrerScreenUrl;
}

static SensorsAnalyticsSDK *sharedInstance = nil;
Expand Down Expand Up @@ -223,8 +228,7 @@ - (instancetype)initWithServerURL:(NSString *)serverURL
_vtrackWindow = nil;
_autoTrack = NO;
_appRelaunched = NO;


_referrerScreenUrl = nil;

_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
Expand Down Expand Up @@ -253,7 +257,9 @@ - (instancetype)initWithServerURL:(NSString *)serverURL

[self setUpListeners];

#ifndef SENSORS_ANALYTICS_DISABLE_VTRACK
[self executeEventBindings:self.eventBindings];
#endif

// XXX: App Active 的时候会获取配置,此处不需要获取
// [self checkForConfigure];
Expand All @@ -268,6 +274,7 @@ - (instancetype)initWithServerURL:(NSString *)serverURL
}

- (void)enableEditingVTrack {
#ifndef SENSORS_ANALYTICS_DISABLE_VTRACK
dispatch_async(dispatch_get_main_queue(), ^{
// 5 秒
self.vtrackConnectorTimer = [NSTimer scheduledTimerWithTimeInterval:10
Expand All @@ -276,6 +283,41 @@ - (void)enableEditingVTrack {
userInfo:nil
repeats:YES];
});
#endif
}

- (void)showUpWebView:(id)webView {
SADebug(@"showUpWebView");
if (webView == nil) {
SADebug(@"showUpWebView == nil");
}
NSString *js = [NSString stringWithFormat:@"sensorsdata_app_js_bridge_call_js('%@')", [self webViewJavascriptBridgeCallbackInfo]];
if ([webView isKindOfClass:[UIWebView class]] == YES) {//UIWebView
SADebug(@"showUpWebView: UIWebView");
[webView stringByEvaluatingJavaScriptFromString:js];
}
#if defined(supportsWKWebKit )
else if([webView isKindOfClass:[WKWebView class]] == YES) {//WKWebView
SADebug(@"showUpWebView: WKWebView");
[webView evaluateJavaScript:js completionHandler:^(id _Nullable response, NSError * _Nullable error) {
NSLog(@"response: %@ error: %@", response, error);
}];
}
#endif
else{
SADebug(@"showUpWebView: not UIWebView or WKWebView");
return;
}
}

- (NSString *)webViewJavascriptBridgeCallbackInfo {
JSONUtil *_jsonUtil = [[JSONUtil alloc] init];
NSMutableDictionary *libProperties = [[NSMutableDictionary alloc] init];
[libProperties setValue:@"iOS" forKey:@"type"];
[libProperties setValue:[self distinctId] forKey:@"distinct_id"];
NSData* jsonData = [_jsonUtil JSONSerializeObject:libProperties];
NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
return [jsonString copy];
}

- (void)showUpWebView:(id)webView {
Expand Down Expand Up @@ -504,7 +546,11 @@ - (void)_flush:(BOOL) vacuumAfterFlushing {
};

[self flushByType:@"Post" withSize:(_debugMode == SensorsAnalyticsDebugOff ? 50 : 1) andFlushMethod:flushByPost];
#ifdef SENSORS_ANALYTICS_IOS_MATCHING_WITH_COOKIE
[self flushByType:@"SFSafariViewController" withSize:(_debugMode == SensorsAnalyticsDebugOff ? 50 : 1) andFlushMethod:flushBySafariVC];
#else
[self flushByType:@"SFSafariViewController" withSize:(_debugMode == SensorsAnalyticsDebugOff ? 50 : 1) andFlushMethod:flushByPost];
#endif

if (![self.messageQueue vacuum]) {
SAError(@"failed to VACUUM SQLite.");
Expand Down Expand Up @@ -1273,19 +1319,47 @@ - (void)setUpListeners {

void (^block)(id, SEL, id) = ^(id obj, SEL sel, NSNumber* a) {
if (_autoTrack) {
Class klass = [(UITableViewController *)obj class];
if (klass) {
NSString *screenName = NSStringFromClass(klass);
if (![screenName isEqualToString:@"SFBrowserRemoteViewController"] &&
![screenName isEqualToString:@"SFSafariViewController"] &&
![screenName isEqualToString:@"UIInputWindowController"] &&
![screenName isEqualToString:@"UINavigationController"] &&
![screenName isEqualToString:@"UIApplicationRotationFollowingControllerNoTouches"]) {
[self track:APP_VIEW_SCREEN_EVENT withProperties:@{
SCREEN_NAME_PROPERTY : NSStringFromClass(klass)
}];
UIViewController *controller = (UIViewController *)obj;
if (!controller) {
return;
}

Class klass = [controller class];
if (!klass) {
return;
}

NSString *screenName = NSStringFromClass(klass);
if ([screenName isEqualToString:@"SFBrowserRemoteViewController"] ||
[screenName isEqualToString:@"SFSafariViewController"] ||
[screenName isEqualToString:@"UIInputWindowController"] ||
[screenName isEqualToString:@"UINavigationController"] ||
[screenName isEqualToString:@"UIApplicationRotationFollowingControllerNoTouches"]) {
return;
}

NSMutableDictionary *properties = [[NSMutableDictionary alloc] init];
[properties setValue:NSStringFromClass(klass) forKey:SCREEN_NAME_PROPERTY];

if ([controller conformsToProtocol:@protocol(SAAutoTracker)]) {
UIViewController<SAAutoTracker> *autoTrackerController = (UIViewController<SAAutoTracker> *)controller;
[properties addEntriesFromDictionary:[autoTrackerController getTrackProperties]];
}

if ([controller conformsToProtocol:@protocol(SAScreenAutoTracker)]) {
UIViewController<SAScreenAutoTracker> *screenAutoTrackerController = (UIViewController<SAScreenAutoTracker> *)controller;
NSString *currentScreenUrl = [screenAutoTrackerController getScreenUrl];

[properties setValue:currentScreenUrl forKey:SCREEN_URL_PROPERTY];
@synchronized(_referrerScreenUrl) {
if (_referrerScreenUrl) {
[properties setValue:_referrerScreenUrl forKey:SCREEN_REFERRER_URL_PROPERTY];
}
_referrerScreenUrl = currentScreenUrl;
}
}

[self track:APP_VIEW_SCREEN_EVENT withProperties:properties];
}
};

Expand Down Expand Up @@ -1338,9 +1412,11 @@ - (void)applicationDidBecomeActive:(NSNotification *)notification {
[self trackTimer:APP_END_EVENT withTimeUnit:SensorsAnalyticsTimeUnitSeconds];
}

#ifndef SENSORS_ANALYTICS_DISABLE_VTRACK
if (self.checkForEventBindingsOnActive) {
[self checkForConfigure];
}
#endif

[self startFlushTimer];
}
Expand Down

0 comments on commit f819b40

Please sign in to comment.