APP接入友盟数据埋点

2022-01-11  本文已影响0人  AR24

1. 接入友盟SDK

使用cocoapods接入

pod 'UMCommon'

2.初始化SDK

+ (void)initUMSDK{
    // 设置友盟appkey
    [UMConfigure initWithAppkey:key channel:@"App Store"];
    [UMConfigure setEncryptEnabled:YES];
    // 集成测试
    [UMConfigure setLogEnabled:YES];
    [MobClick profileSignInWithPUID:[GVUserDefaults standardUserDefaults].loginName];
    // 统计浏览时间时,需要手动统计
    [MobClick setAutoPageEnabled:NO];
}

3.使用SDK记录点击事件

+ (void)UMEventNoDefaultParamsForId:(NSString *)eventId params:(NSDictionary *)params{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:params];
    [MobClick event:eventId attributes:dic];
}

+ (void)UMEventForId:(NSString *)eventId params:(NSDictionary *_Nullable)params{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:params];
    dic[@"user"] = [GVUserDefaults standardUserDefaults].hp_phone;
    dic[@"click_time"] = [TimeFormatterTool timeForDate:[NSDate date] withFormatter:@"yyyy-MM-dd HH:mm:ss"];
    dic[@"name"] = eventId;
    [MobClick event:eventId attributes:dic];
}

+ (void)UMEventPageViewForId:(NSString *)eventId params:(NSDictionary *_Nullable)params{
    if ([Global_Function pageViewSeconds] == 0.0) {
        return;
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:params];
    dic[@"user"] = [GVUserDefaults standardUserDefaults].hp_phone;
    dic[@"stay_time"] = [NSString stringWithFormat:@"%.03f",[Global_Function pageViewSeconds]];
    dic[@"name"] = eventId;
    [MobClick event:eventId attributes:dic];
}

+ (void)UMEventPageViewForId:(NSString *)eventId time:(float)time params:(NSDictionary *_Nullable)params{
    if (time == 0.0) {
        return;
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:params];
    dic[@"user"] = [GVUserDefaults standardUserDefaults].hp_phone;
    dic[@"stay_time"] = [NSString stringWithFormat:@"%.03f",time];
    dic[@"name"] = eventId;
    [MobClick event:eventId attributes:dic];
}
+ (NSString *)timeForDate:(NSDate *)date withFormatter:(NSString *)formatter{
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
    formatter1.dateFormat = formatter;
    NSString *timeStr = [formatter1  stringFromDate:date];
    return timeStr;
}

4.如何统计页面浏览时间

如果控制的好的话,可以只用一个全局变量来存储时间,在viewWillAppear中刷新这个时间就可以了,在viewWillDisappear中计算全局变量和[NSDate date]时间差即可。但是要注意一下,tabbar的控制器切换A->B控制器,是先调用B的viewWillAppear再调用A的viewWillDisappear,所有这种情况需要独立变量计算时间。

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.pageDate = [NSDate date];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    float sec = [Tools calSecondsBetween:self.pageDate second:[NSDate date]];
    [Global_Function UMEventPageViewForId:Home_PageView_HomePage time:sec params:nil];
}

5.测试数据埋点是否正常

使用友盟实时日志,按照上面的步骤完成手机配置,就可以使用固定手机进行实时测试了。

上一篇 下一篇

猜你喜欢

热点阅读