Skip to content

Commit

Permalink
Release 1.6.20
Browse files Browse the repository at this point in the history
1. 修复渠道追踪的Bug
  • Loading branch information
Yuhan ZOU committed Oct 19, 2016
1 parent 4bac333 commit eeb49aa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
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.19"
s.version = "1.6.20"
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
47 changes: 25 additions & 22 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.19"
#define VERSION @"1.6.20"

#define PROPERTY_LENGTH_LIMITATION 8191

Expand Down Expand Up @@ -365,15 +365,15 @@ - (void)enableAutoTrack {
[self _enableAutoTrack];
}

- (void)flushByType:(NSString *)type withSize:(int)flushSize andFlushMethod:(BOOL (^)(NSArray *))flushMethod {
- (void)flushByType:(NSString *)type withSize:(int)flushSize andFlushMethod:(BOOL (^)(NSArray *, NSString *))flushMethod {
while (true) {
NSArray *recordArray = [self.messageQueue getFirstRecords:flushSize withType:type];
if (recordArray == nil) {
SAError(@"Failed to get records from SQLite.");
break;
}

if ([recordArray count] == 0 || !flushMethod(recordArray)) {
if ([recordArray count] == 0 || !flushMethod(recordArray, type)) {
break;
}

Expand All @@ -386,7 +386,7 @@ - (void)flushByType:(NSString *)type withSize:(int)flushSize andFlushMethod:(BOO

- (void)_flush:(BOOL) vacuumAfterFlushing {
// 使用 Post 发送数据
BOOL (^flushByPost)(NSArray *) = ^(NSArray *recordArray) {
BOOL (^flushByPost)(NSArray *, NSString *) = ^(NSArray *recordArray, NSString *type) {
// 1. 先完成这一系列Json字符串的拼接
NSString *jsonString = [NSString stringWithFormat:@"[%@]",[recordArray componentsJoinedByString:@","]];
// 2. 使用gzip进行压缩
Expand All @@ -405,7 +405,17 @@ - (void)_flush:(BOOL) vacuumAfterFlushing {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postBody dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:@"SensorsAnalytics iOS SDK" forHTTPHeaderField:@"User-Agent"];
if ([type isEqualToString:@"SFSafariViewController"]) {
// 渠道追踪请求,需要从 UserAgent 中解析 OS 信息用于模糊匹配
dispatch_sync(dispatch_get_main_queue(), ^{
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString* userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
});
} else {
// 普通事件请求,使用标准 UserAgent
[request setValue:@"SensorsAnalytics iOS SDK" forHTTPHeaderField:@"User-Agent"];
}
if (_debugMode == SensorsAnalyticsDebugOnly) {
[request setValue:@"true" forHTTPHeaderField:@"Dry-Run"];
}
Expand Down Expand Up @@ -438,9 +448,8 @@ - (void)_flush:(BOOL) vacuumAfterFlushing {
}
} else {
SAError(@"%@", errMsg);
flushSucc = NO;
}

flushSucc = NO;
} else {
if (_debugMode != SensorsAnalyticsDebugOff) {
SAError(@"==========================================================================");
Expand Down Expand Up @@ -469,7 +478,7 @@ - (void)_flush:(BOOL) vacuumAfterFlushing {
};

// 使用 SFSafariViewController 发送数据 (>= iOS 9.0)
BOOL (^flushBySafariVC)(NSArray *) = ^(NSArray *recordArray) {
BOOL (^flushBySafariVC)(NSArray *, NSString *) = ^(NSArray *recordArray, NSString *type) {
if (self.safariRequestInProgress) {
return NO;
}
Expand Down Expand Up @@ -850,29 +859,23 @@ - (void)trackSignUp:(NSString *)newDistinctId {
- (void)trackInstallation:(NSString *)event withProperties:(NSDictionary *)propertyDict {
// 追踪渠道是特殊功能,需要同时发送 track 和 profile_set_once

// 先发送 track
NSMutableDictionary *eventProperties;
if (propertyDict == nil) {
eventProperties = [[NSMutableDictionary alloc] init];
} else {
eventProperties = [[NSMutableDictionary alloc] initWithDictionary:propertyDict];
NSMutableDictionary *properties = [[NSMutableDictionary alloc] init];
[properties setValue:@"" forKey:@"$ios_install_source"];
if (propertyDict != nil) {
[properties addEntriesFromDictionary:propertyDict];
}

[eventProperties setValue:@"" forKey:@"$ios_install_source"];
[self track:event withProperties:eventProperties withType:@"track"];
// 先发送 track
[self track:event withProperties:properties withType:@"track"];

// 再发送 profile_set_once
NSMutableDictionary *profiles = [[NSMutableDictionary alloc] init];
[profiles setValue:@"" forKey:@"$ios_install_source"];
if (propertyDict != nil) {
[profiles addEntriesFromDictionary:propertyDict];
}
[self track:nil withProperties:profiles withType:@"profile_set_once"];
[self track:nil withProperties:properties withType:@"profile_set_once"];
}

- (void)trackInstallation:(NSString *)event {
// 追踪渠道是特殊功能,需要同时发送 track 和 profile_set_once

// 通过 '$ios_install_source' 属性标记渠道追踪请求
NSDictionary *properties = @{@"$ios_install_source" : @""};

// 先发送 track
Expand Down

0 comments on commit eeb49aa

Please sign in to comment.