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

全局添加referer_link参数

parent baf057d6
......@@ -13,11 +13,9 @@
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Phobos sharedClient].getTopController = ^UIViewController * _Nonnull{
Phobos.sharedClient.getTopController = ^UIViewController * _Nonnull{
return [UIViewController new];
};
// Override point for customization after application launch.
return YES;
}
......
......@@ -349,7 +349,7 @@ static NSString *sdkVersion = @"110";
- (void)onPVStart:(UIResponder<PhobosPVProtocol> *)page {
// 必须在此处调用一下referer,因为onControllerStart
[page initReferer];
[page initReferrerPosition];
[page initRefererLink];
[page initReferrerIdIfNil];
[page initReferrerTabName];
page.inTime = [PhobosUtil currentTime];
......@@ -359,9 +359,6 @@ static NSString *sdkVersion = @"110";
if (![PhobosUtil isNonEmpty:page.pageName] || !page.needLogPV) {
return;
}
// 如果referrer_position存在,添加 referrerPosition 参数
[self addReferrerPosition:page];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
@try {
[dict setObject:[PhobosUtil currentTime] forKey:@"out"];
......@@ -369,11 +366,13 @@ static NSString *sdkVersion = @"110";
[dict setObject:page.pageName forKey:@"page_name"];
[dict setObject:page.businessId?:@"" forKey:@"business_id"];
[dict setObject:page.referer?:@"" forKey:@"referrer"];
[dict setObject:page.refererLink ? : @[] forKey:@"referer_link"];
[dict setObject:@(0) forKey:@"fake"];
[dict setObject:page.referrerId ? : @"" forKey:@"referrer_id"];
[dict setObject:page.extraParam ? : @"" forKey:@"extra_param"];
[dict setObject:page.referrerTabName ? : @"" forKey:@"referrer_tab_name"];
[dict setObject:page.isPush.intValue ? @(page.isPush.intValue) : @(0) forKey:@"is_push"];
if (page.inTime.length > 0) {
// 页面显示时间为空时不记录页面pv事件
[Phobos track:@"page_view" attributes:dict];
......@@ -411,13 +410,10 @@ static NSString *sdkVersion = @"110";
* @since 0.0.1
*/
- (NSDictionary *)prepareDictionaryForEvent:(NSString *)eventId attributes:(NSDictionary *)attributes{
// 点击事件 referrer_position
NSArray *refererLink = sharedClient.visibleController.refererLink;
NSMutableDictionary *attributesParams = [NSMutableDictionary dictionaryWithDictionary:attributes];
NSString *referrerPosition = sharedClient.visibleController.referrerPosition;
if (![eventId isEqualToString:@"page_view"] && ![referrerPosition isEqualToString:@""]) {
[attributesParams setValue:referrerPosition forKey:@"referrer_position"];
}
[attributesParams setValue:refererLink ? : @[] forKey:@"referer_Link"];
attributes = attributesParams;
[self catchNullForEvent:eventId attributes:attributes];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
@try {
......@@ -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
......@@ -76,9 +76,9 @@ NS_ASSUME_NONNULL_BEGIN
@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
......
......@@ -16,9 +16,9 @@
- (void)initReferer;
/**
只适用于当为AI落地页的时候, 链路中页面浏览PV 添加 ReferrerPosition 参数
// 只适用于链路中页面浏览PVLink 添加 refererLink 参数
*/
- (void)initReferrerPosition;
- (void)initRefererLink;
/**
此方法在onPvStart时调用,如果发现已经有值了,不会给referrerId再次赋值
......
......@@ -45,9 +45,9 @@
}
}
// 只适用于当为AI落地页的时候, 链路中页面浏览PV 添加 ReferrerPosition 参数
- (void)initReferrerPosition {
if ([self.referrerPosition isEqualToString:@""] && [self isKindOfClass:[UIViewController class]]) {
// 只适用于链路中页面浏览PVLink 添加 refererLink 参数
- (void)initRefererLink {
if ([self isKindOfClass:[UIViewController class]]) {
// 分present与navigation两种情况
UIViewController *me = (UIViewController *)self;
if (me.presentingViewController != nil) {
......@@ -55,9 +55,13 @@
// 所以此处要判断,如果是navigation弹出,最后还是要定位到topViewController
if ([me.presentingViewController isKindOfClass:[UINavigationController class]]) {
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 {
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 {
NSArray *navigationPool = ((UIViewController *)self).navigationController.viewControllers;
......@@ -66,18 +70,20 @@
return ;
}
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 {
objc_setAssociatedObject(self, @selector(referrerPosition), referrerPosition, OBJC_ASSOCIATION_COPY);
- (void)setRefererLink:(NSArray *)refererLink {
objc_setAssociatedObject(self, @selector(refererLink), refererLink, OBJC_ASSOCIATION_COPY);
}
- (NSString *)referrerPosition {
NSString *referrerPosition = objc_getAssociatedObject(self, @selector(referrerPosition));
return referrerPosition == nil ? @"" : referrerPosition;
-(NSArray *)refererLink {
NSArray *refererLink = objc_getAssociatedObject(self, @selector(refererLink));
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