Commit a68c9258 authored by 刘鹿杰's avatar 刘鹿杰

全局添加referer_link参数

parent baf057d6
...@@ -13,11 +13,9 @@ ...@@ -13,11 +13,9 @@
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ {
Phobos.sharedClient.getTopController = ^UIViewController * _Nonnull{
[Phobos sharedClient].getTopController = ^UIViewController * _Nonnull{
return [UIViewController new]; return [UIViewController new];
}; };
// Override point for customization after application launch. // Override point for customization after application launch.
return YES; return YES;
} }
......
...@@ -349,7 +349,7 @@ static NSString *sdkVersion = @"110"; ...@@ -349,7 +349,7 @@ static NSString *sdkVersion = @"110";
- (void)onPVStart:(UIResponder<PhobosPVProtocol> *)page { - (void)onPVStart:(UIResponder<PhobosPVProtocol> *)page {
// 必须在此处调用一下referer,因为onControllerStart // 必须在此处调用一下referer,因为onControllerStart
[page initReferer]; [page initReferer];
[page initReferrerPosition]; [page initRefererLink];
[page initReferrerIdIfNil]; [page initReferrerIdIfNil];
[page initReferrerTabName]; [page initReferrerTabName];
page.inTime = [PhobosUtil currentTime]; page.inTime = [PhobosUtil currentTime];
...@@ -359,9 +359,6 @@ static NSString *sdkVersion = @"110"; ...@@ -359,9 +359,6 @@ static NSString *sdkVersion = @"110";
if (![PhobosUtil isNonEmpty:page.pageName] || !page.needLogPV) { if (![PhobosUtil isNonEmpty:page.pageName] || !page.needLogPV) {
return; return;
} }
// 如果referrer_position存在,添加 referrerPosition 参数
[self addReferrerPosition:page];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
@try { @try {
[dict setObject:[PhobosUtil currentTime] forKey:@"out"]; [dict setObject:[PhobosUtil currentTime] forKey:@"out"];
...@@ -369,11 +366,13 @@ static NSString *sdkVersion = @"110"; ...@@ -369,11 +366,13 @@ static NSString *sdkVersion = @"110";
[dict setObject:page.pageName forKey:@"page_name"]; [dict setObject:page.pageName forKey:@"page_name"];
[dict setObject:page.businessId?:@"" forKey:@"business_id"]; [dict setObject:page.businessId?:@"" forKey:@"business_id"];
[dict setObject:page.referer?:@"" forKey:@"referrer"]; [dict setObject:page.referer?:@"" forKey:@"referrer"];
[dict setObject:page.refererLink ? : @[] forKey:@"referer_link"];
[dict setObject:@(0) forKey:@"fake"]; [dict setObject:@(0) forKey:@"fake"];
[dict setObject:page.referrerId ? : @"" forKey:@"referrer_id"]; [dict setObject:page.referrerId ? : @"" forKey:@"referrer_id"];
[dict setObject:page.extraParam ? : @"" forKey:@"extra_param"]; [dict setObject:page.extraParam ? : @"" forKey:@"extra_param"];
[dict setObject:page.referrerTabName ? : @"" forKey:@"referrer_tab_name"]; [dict setObject:page.referrerTabName ? : @"" forKey:@"referrer_tab_name"];
[dict setObject:page.isPush.intValue ? @(page.isPush.intValue) : @(0) forKey:@"is_push"]; [dict setObject:page.isPush.intValue ? @(page.isPush.intValue) : @(0) forKey:@"is_push"];
if (page.inTime.length > 0) { if (page.inTime.length > 0) {
// 页面显示时间为空时不记录页面pv事件 // 页面显示时间为空时不记录页面pv事件
[Phobos track:@"page_view" attributes:dict]; [Phobos track:@"page_view" attributes:dict];
...@@ -411,13 +410,10 @@ static NSString *sdkVersion = @"110"; ...@@ -411,13 +410,10 @@ static NSString *sdkVersion = @"110";
* @since 0.0.1 * @since 0.0.1
*/ */
- (NSDictionary *)prepareDictionaryForEvent:(NSString *)eventId attributes:(NSDictionary *)attributes{ - (NSDictionary *)prepareDictionaryForEvent:(NSString *)eventId attributes:(NSDictionary *)attributes{
// 点击事件 referrer_position NSArray *refererLink = sharedClient.visibleController.refererLink;
NSMutableDictionary *attributesParams = [NSMutableDictionary dictionaryWithDictionary:attributes]; NSMutableDictionary *attributesParams = [NSMutableDictionary dictionaryWithDictionary:attributes];
NSString *referrerPosition = sharedClient.visibleController.referrerPosition; [attributesParams setValue:refererLink ? : @[] forKey:@"referer_Link"];
if (![eventId isEqualToString:@"page_view"] && ![referrerPosition isEqualToString:@""]) { attributes = attributesParams;
[attributesParams setValue:referrerPosition forKey:@"referrer_position"];
}
[self catchNullForEvent:eventId attributes:attributes]; [self catchNullForEvent:eventId attributes:attributes];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
@try { @try {
...@@ -694,44 +690,4 @@ static NSString *sdkVersion = @"110"; ...@@ -694,44 +690,4 @@ static NSString *sdkVersion = @"110";
} }
} }
// 字符串转字典
- (NSDictionary *)dictionaryWithJsonString:(NSString *)pageExtraParam {
NSData *jsonData = [pageExtraParam dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err) {
NSLog(@"json解析失败:%@",err);
return nil;
}
return dic;
}
// 字典转字符串
- (NSString *)jsonStringWithDictionary:(NSDictionary *)dictionary {
NSError *parseError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&parseError];
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
// 添加 ReferrerPosition 逻辑封装
- (NSString *)addReferrerPosition:(UIResponder<PhobosPVProtocol> *)page{
// 如果referrer_position存在,添加 referrerPosition 参数
if (![page.referrerPosition isEqualToString:@""]) {
if (![page.extraParam isEqualToString:@""]) {
NSMutableDictionary *extraParamDict = [self dictionaryWithJsonString:page.extraParam];
// 当为用户贴时 page.extraParam = user_post, 此时不是字典。
// 防止page.extraParam为其它格式数据,当解析字典不成功时 不再添加 referrer_position
if (extraParamDict == nil) { return page.extraParam; }
[extraParamDict setValue:page.referrerPosition forKey:@"referrer_position"];
page.extraParam = [self jsonStringWithDictionary:extraParamDict];
}else{
page.extraParam = [self jsonStringWithDictionary:@{@"referrer_position" : page.referrerPosition}];
}
}
return page.extraParam;
}
@end @end
...@@ -76,9 +76,9 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -76,9 +76,9 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, copy) NSString *isPush; @property (nonatomic, copy) NSString *isPush;
/** /**
获取上一个页面的referrer_position by 7.20.0 如果有则添加, 没有则不添加 获取上一个页面链路的page_name link by 7.20.0 如果有此页面有page_name则添加 , 没有添加""
*/ */
@property (nonatomic, copy) NSString *referrerPosition; @property (nonatomic, copy) NSArray *refererLink;
@end @end
......
...@@ -16,9 +16,9 @@ ...@@ -16,9 +16,9 @@
- (void)initReferer; - (void)initReferer;
/** /**
只适用于当为AI落地页的时候, 链路中页面浏览PV 添加 ReferrerPosition 参数 // 只适用于链路中页面浏览PVLink 添加 refererLink 参数
*/ */
- (void)initReferrerPosition; - (void)initRefererLink;
/** /**
此方法在onPvStart时调用,如果发现已经有值了,不会给referrerId再次赋值 此方法在onPvStart时调用,如果发现已经有值了,不会给referrerId再次赋值
......
...@@ -45,9 +45,9 @@ ...@@ -45,9 +45,9 @@
} }
} }
// 只适用于当为AI落地页的时候, 链路中页面浏览PV 添加 ReferrerPosition 参数 // 只适用于链路中页面浏览PVLink 添加 refererLink 参数
- (void)initReferrerPosition { - (void)initRefererLink {
if ([self.referrerPosition isEqualToString:@""] && [self isKindOfClass:[UIViewController class]]) { if ([self isKindOfClass:[UIViewController class]]) {
// 分present与navigation两种情况 // 分present与navigation两种情况
UIViewController *me = (UIViewController *)self; UIViewController *me = (UIViewController *)self;
if (me.presentingViewController != nil) { if (me.presentingViewController != nil) {
...@@ -55,9 +55,13 @@ ...@@ -55,9 +55,13 @@
// 所以此处要判断,如果是navigation弹出,最后还是要定位到topViewController // 所以此处要判断,如果是navigation弹出,最后还是要定位到topViewController
if ([me.presentingViewController isKindOfClass:[UINavigationController class]]) { if ([me.presentingViewController isKindOfClass:[UINavigationController class]]) {
UIViewController *top = ((UINavigationController *)me.presentingViewController).topViewController; UIViewController *top = ((UINavigationController *)me.presentingViewController).topViewController;
objc_setAssociatedObject(self, @selector(referrerPosition), top.referrerPosition, OBJC_ASSOCIATION_COPY); NSMutableArray *tempRefererLink = [NSMutableArray arrayWithArray:top.refererLink];
[tempRefererLink addObject:top.pageName];
objc_setAssociatedObject(self, @selector(pageLink), tempRefererLink, OBJC_ASSOCIATION_COPY);
} else { } else {
objc_setAssociatedObject(self, @selector(referrerPosition), me.presentingViewController.referrerPosition, OBJC_ASSOCIATION_COPY); NSMutableArray *tempRefererLink = [NSMutableArray arrayWithArray:me.presentingViewController.refererLink];
[tempRefererLink addObject:me.presentingViewController.pageName];
objc_setAssociatedObject(self, @selector(pageLink), tempRefererLink, OBJC_ASSOCIATION_COPY);
} }
} else { } else {
NSArray *navigationPool = ((UIViewController *)self).navigationController.viewControllers; NSArray *navigationPool = ((UIViewController *)self).navigationController.viewControllers;
...@@ -66,18 +70,20 @@ ...@@ -66,18 +70,20 @@
return ; return ;
} }
UIViewController *controller = navigationPool[refererIndex]; UIViewController *controller = navigationPool[refererIndex];
objc_setAssociatedObject(self, @selector(referrerPosition), controller.referrerPosition, OBJC_ASSOCIATION_COPY); NSMutableArray *tempRefererLink = [NSMutableArray arrayWithArray:controller.refererLink];
[tempRefererLink addObject:controller.pageName];
objc_setAssociatedObject(self, @selector(refererLink), tempRefererLink, OBJC_ASSOCIATION_COPY);
} }
} }
} }
- (void)setReferrerPosition:(NSString *)referrerPosition { - (void)setRefererLink:(NSArray *)refererLink {
objc_setAssociatedObject(self, @selector(referrerPosition), referrerPosition, OBJC_ASSOCIATION_COPY); objc_setAssociatedObject(self, @selector(refererLink), refererLink, OBJC_ASSOCIATION_COPY);
} }
- (NSString *)referrerPosition { -(NSArray *)refererLink {
NSString *referrerPosition = objc_getAssociatedObject(self, @selector(referrerPosition)); NSArray *refererLink = objc_getAssociatedObject(self, @selector(refererLink));
return referrerPosition == nil ? @"" : referrerPosition; return refererLink;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment