react native 百度统计Android端的集成可参考:
https://www.jianshu.com/p/cc354c6a81d5 希望能够让同学们少走些弯路。
百度统计的集成还是蛮简单的,主要分下面一些步骤:
步骤
- sdk的集成
- 交互类
- sdk的配置以及初始化
- RN 端调用
1. sdk 的集成
官网下载地址:
https://mtj.baidu.com/web/sdk/index
我这里选择的是手动埋点的方式。
官网的 技术文档介绍:
https://mtj.baidu.com/static/userguide/book/ios/sdk/manual.html
官网介绍的已经很清楚,比友盟的集成清晰很多,大家按照文档操作就可以,很顺利就能完成集成。
2.交互类
git上有个开源的交互类 大家可进行参考修改
https://github.com/BaiduMobileAnalysis/baidumobstat-react-native/blob/master/ios/RCTBaiduMobStat/RCTBaiduMobStat/RCTBaiduMobStat.m
//
// RCTBaiduMobStat.m
// rent
//
// Created by 姜森林 on 2018/8/24.
// Copyright © 2018年 Facebook. All rights reserved.
//
#import "RCTBaiduMobStat.h"
#import <React/RCTLog.h>
@implementation RCTBaiduMobStat
RCT_EXPORT_MODULE(BaiduMTJ);
RCT_EXPORT_METHOD(onEvent:(NSString *)eventId eventLabel:(NSString *)eventLabel) {
[[BaiduMobStat defaultStat] logEvent:eventId eventLabel: eventLabel];
}
RCT_EXPORT_METHOD(onEventDuration:(NSString *)eventId eventLabel:(NSString *)eventLabel durationTime:(nonnull NSNumber *)duration) {
[[BaiduMobStat defaultStat] logEventWithDurationTime:eventId eventLabel: eventLabel durationTime:[duration unsignedLongValue]];
}
RCT_EXPORT_METHOD(onEventStart:(NSString *)eventId eventLabel:(NSString *)eventLabel) {
[[BaiduMobStat defaultStat] eventStart:eventId eventLabel: eventLabel];
}
RCT_EXPORT_METHOD(onEventEnd:(NSString *)eventId eventLabel:(NSString *)eventLabel) {
[[BaiduMobStat defaultStat] eventEnd:eventId eventLabel: eventLabel];
}
RCT_EXPORT_METHOD(onEventWithAttributes:(NSString *)eventId eventLabel:(NSString *)eventLabel attributes:(NSDictionary *)attributes) {
[[BaiduMobStat defaultStat] logEvent:eventId eventLabel: eventLabel attributes:attributes];
}
RCT_EXPORT_METHOD(onEventDurationWithAttributes:(NSString *)eventId eventLabel:(NSString *)eventLabel durationTime:(nonnull NSNumber *)duration attributes:(NSDictionary *)attributes) {
[[BaiduMobStat defaultStat] logEventWithDurationTime:eventId eventLabel: eventLabel durationTime:[duration unsignedLongValue] attributes:attributes];
}
RCT_EXPORT_METHOD(onEventEndWithAttributes:(NSString *)eventId eventLabel:(NSString *)eventLabel attributes:(NSDictionary *)attributes) {
[[BaiduMobStat defaultStat] logEvent:eventId eventLabel: eventLabel attributes:attributes];
}
RCT_EXPORT_METHOD(onPageStart:(NSString *)name) {
[[BaiduMobStat defaultStat] pageviewStartWithName:name];
}
RCT_EXPORT_METHOD(onPageEnd:(NSString *)name) {
[[BaiduMobStat defaultStat] pageviewEndWithName:name];
}
@end
3.初始化工作
#import "BaiduMobStat.h"
/**
* 百度移动统计启动代码
*/
[[BaiduMobStat defaultStat] startWithAppId:@"xxx"];
[[BaiduMobStat defaultStat] setEnableDebugOn: YES];
/***********************************************/
4.RN 端调用
import { NativeModules } from 'react-native';
const BaiduMTJ = NativeModules.BaiduMTJ;
export const onPageStart = pageName => {
//用于统计单个自定义页面的起始和onPageEnd同时使用,不可单独使用
return BaiduMTJ.onPageStart(pageName);
};
export const onPageEnd = pageName => {
//用于统计单个Activity页面结束时间
return BaiduMTJ.onPageEnd(pageName);
};
export const onEvent = (eventId, label) => {
//用于统计自定义事件的发生次数
// console.log(BaiduMTJ.onEvent(eventId, label));
return BaiduMTJ.onEvent(eventId, label);
};
像这些统计百度啊 友盟啊 集成方式都差不多,遇到什么问题大家仔细看下错误提示 就能解决。
Android 可参考 :
https://www.jianshu.com/p/cc354c6a81d5