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

埋点库添加referrer_Position参数, 方便 AI 落地页埋点链路监控

parent 2a751ef1
......@@ -7,12 +7,16 @@
//
#import "GMAppDelegate.h"
#import "GMPhobosController.h"
@implementation GMAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[Phobos sharedClient].getTopController = ^UIViewController *{
return [UIViewController new];
};
return YES;
}
......
......@@ -35,7 +35,7 @@ PODS:
- GMKit/Protocol (1.1.3):
- Masonry (= 1.1.0)
- SDWebImage (= 3.7.6)
- GMPhobos (1.3.0):
- GMPhobos (1.3.4):
- GMCache
- GMKit
- Masonry (1.1.0)
......@@ -63,7 +63,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
GMCache: 09a3029c96fe130e3a21faef70b3d9d2ce92d639
GMKit: 35f788243cceeddf3e13c5226b3ea0b5e08e2117
GMPhobos: c1dd33760f8d4243e6bf562a115899e66c6ec7d2
GMPhobos: 1ee4b12cccd1df054bf9539357c5fc60a2d64a68
Masonry: 678fab65091a9290e40e2832a55e7ab731aad201
SDWebImage: c325cf02c30337336b95beff20a13df489ec0ec9
TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed
......
......@@ -8,7 +8,7 @@
Pod::Spec.new do |s|
s.name = "GMPhobos"
s.version = "1.3.4"
s.version = "1.3.5"
s.summary = "GM statistic data sdk"
s.description = <<-DESC
......
......@@ -298,6 +298,7 @@ static dispatch_queue_t _normalQueue;
// 必须在此处调用一下referer,因为onControllerStart
[page initReferer];
[page initReferrerIdIfNil];
[page initReferrerPosition];
[page initReferrerTabName];
page.inTime = [PhobosUtil currentTime];
}
......@@ -306,6 +307,8 @@ static dispatch_queue_t _normalQueue;
if (![PhobosUtil isNonEmpty:page.pageName] || !page.needLogPV) {
return;
}
// 如果referrer_position存在,添加 referrerPosition 参数
[self addReferrerPosition:page];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
@try {
......@@ -353,6 +356,12 @@ static dispatch_queue_t _normalQueue;
* @since 0.0.1
*/
- (NSDictionary *)prepareDictionaryForEvent:(NSString *)eventName attributes:(NSDictionary *)attributes{
// 点击事件 referrer_position
NSMutableDictionary *attributesParams = [NSMutableDictionary dictionaryWithDictionary:attributes];
NSString *referrerPosition = _sharedClient.visibleController.referrerPosition;
if (![eventName isEqualToString:@"page_view"] && ![referrerPosition isEqualToString:@""]) {
[attributesParams setValue:referrerPosition forKey:@"referrer_position"];
}
[self catchNullForEvent:eventName attributes:attributes];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
@try {
......@@ -634,6 +643,46 @@ static dispatch_queue_t _normalQueue;
}
}
// 字符串转字典
- (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
@implementation Phobos (UtilTest)
......
......@@ -75,5 +75,10 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, copy) NSString *isPush;
/**
获取上一个页面的referrer_position by 7.20.0 如果有则添加, 没有则不添加
*/
@property (nonatomic, copy) NSString *referrerPosition;
@end
NS_ASSUME_NONNULL_END
......@@ -15,6 +15,11 @@
*/
- (void)initReferer;
/**
只适用于当为AI落地页的时候, 链路中页面浏览PV 添加 ReferrerPosition 参数
*/
- (void)initReferrerPosition;
/**
此方法在onPvStart时调用,如果发现已经有值了,不会给referrerId再次赋值
*/
......
......@@ -45,6 +45,41 @@
}
}
// 只适用于当为AI落地页的时候, 链路中页面浏览PV 添加 ReferrerPosition 参数
- (void)initReferrerPosition {
if ([self.referrerPosition isEqualToString:@""] && [self isKindOfClass:[UIViewController class]]) {
// 分present与navigation两种情况
UIViewController *me = (UIViewController *)self;
if (me.presentingViewController != nil) {
// app全局只有一个navigation,发现此时用navigation.topViewController presentViewController时,最终使用的是navigation弹出的
// 所以此处要判断,如果是navigation弹出,最后还是要定位到topViewController
if ([me.presentingViewController isKindOfClass:[UINavigationController class]]) {
UIViewController *top = ((UINavigationController *)me.presentingViewController).topViewController;
objc_setAssociatedObject(self, @selector(referrerPosition), top.referrerPosition, OBJC_ASSOCIATION_COPY);
} else {
objc_setAssociatedObject(self, @selector(referrerPosition), me.presentingViewController.referrerPosition, OBJC_ASSOCIATION_COPY);
}
} else {
NSArray *navigationPool = ((UIViewController *)self).navigationController.viewControllers;
NSInteger refererIndex = navigationPool.count - 2;
if (refererIndex < 0 ) {
return ;
}
UIViewController *controller = navigationPool[refererIndex];
objc_setAssociatedObject(self, @selector(referrerPosition), controller.referrerPosition, OBJC_ASSOCIATION_COPY);
}
}
}
- (void)setReferrerPosition:(NSString *)referrerPosition {
objc_setAssociatedObject(self, @selector(referrerPosition), referrerPosition, OBJC_ASSOCIATION_COPY);
}
- (NSString *)referrerPosition {
NSString *referrerPosition = objc_getAssociatedObject(self, @selector(referrerPosition));
return referrerPosition == nil ? @"" : referrerPosition;
}
/**
此方法在onPvStart时调用,给referrerTabName赋值
*/
......
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