第一部分:基础SDK集成
【说明】 完成第一部分基础SDK集成,就可以实现APP的用户统计,启动次数统计,使用时长统计,PV统计,APP崩溃的统计。如果您需要更多的自定义统计,请参考第二部分的“自定义代码集成”。
1.1 使用Android Studio导入
⾸先需要将 pttracksdk_v**.aar ⽂件放⼊Module 的 libs ⽬录下。然后在 build.gradle 配置⽂件中加⼊代码:
//由于SDK基于java 8开发,所以在App gradle编译配置项android块中需要增加如下配置
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
repositories{
flatDir{
dirs 'libs'
}
}
dependencies {
implementation(name:'pttracksdk-v3.0.0',ext:'aar')
}
- //由于SDK基于java 8开发,所以在App gradle编译配置项android块中需要增加如下配置
- android {
- compileOptions {
- sourceCompatibility JavaVersion.VERSION_1_8
- targetCompatibility JavaVersion.VERSION_1_8
- }
- }
- repositories{
- flatDir{
- dirs 'libs'
- }
- }
- dependencies {
-
- implementation(name:'pttracksdk-v3.0.0',ext:'aar')
- }
//由于SDK基于java 8开发,所以在App gradle编译配置项android块中需要增加如下配置
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
repositories{
flatDir{
dirs 'libs'
}
}
dependencies {
implementation(name:'pttracksdk-v3.0.0',ext:'aar')
}
权限 |
用途 |
INTERNET |
允许程序联网和发送统计数据的权限。 |
ACCESS_NETWORK_STATE |
允许应用检测网络连接状态,在网络异常状态下避免数据发送,节省流量和电量。 |
READ_PHONE_STATE |
允许应用以只读的方式访问手机设备的信息,通过获取的信息来唯一标识用户。 |
ACCESS_WIFI_STATE |
获取设备的MAC地址,同样用来标识唯一用户。 |
AndroidManifest.xml 中的代码如下:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
注意:上面4个权限都是数据采集必须的,请务必都添加这些权限,否则将可能导致数据采集出现异常
在项目的Application.java中添加如下代码,如果您的项目没有Application.java,请首先创建一个。
JAVA版本:
public class PtApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
/**
*其中"0345045caf264c4d"为您在我们的网站上创建APP时生成的AppKey,"360 Market"为您要发布APP的渠道. this 为当前的ApplicationContext对象
*/
PtAgent.getInstance().startWithAppKeyAndChannel("0345045caf264c4d", "360Market", this);
}
}
Kotlin版本:
PtAgent.getInstance().startWithAppKeyAndChannel("0345045caf264c4d", "360Market", this)
- JAVA版本:
- public class PtApplication extends Application {
- @Override
- public void onCreate() {
- super.onCreate();
- /**
- *其中"0345045caf264c4d"为您在我们的网站上创建APP时生成的AppKey,"360 Market"为您要发布APP的渠道. this 为当前的ApplicationContext对象
- */
- PtAgent.getInstance().startWithAppKeyAndChannel("0345045caf264c4d", "360Market", this);
- }
- }
- Kotlin版本:
-
- PtAgent.getInstance().startWithAppKeyAndChannel("0345045caf264c4d", "360Market", this)
JAVA版本:
public class PtApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
/**
*其中"0345045caf264c4d"为您在我们的网站上创建APP时生成的AppKey,"360 Market"为您要发布APP的渠道. this 为当前的ApplicationContext对象
*/
PtAgent.getInstance().startWithAppKeyAndChannel("0345045caf264c4d", "360Market", this);
}
}
Kotlin版本:
PtAgent.getInstance().startWithAppKeyAndChannel("0345045caf264c4d", "360Market", this)
只需要部署这么一行代码,就可以实现APP的用户统计,启动次数统计,使用时长统计,PV统计,APP崩溃统计。如果你需要更多的统计,请参考下面的‘自定义事件’
注意:每台设备仅记录⾸次安装激活的渠道,如果该设备再次安装其他渠道包, 则数据仍会被记录在初始的安装渠道上。
第二部分:自定义代码集成
在程序中想要采集事件的地⽅添加如下代码,第一个参数为事件名称,如果想要为当前事件绑定自定义参数,可通过第二个参数来实现,字段完全自定义, 字段值不能为数组或者Map,只能是字符串或者数字类型:
JAVA 版本:
/**
* eventName为事件名称;
* attributes 为事件的⾃定义变量。
*/
HashMap<String, Object> attributes = new HashMap<>();
attributes.put("price", 100);
attributes.put("productName": "");
PtAgent.getInstance().trackEvent("eventName",attributes);
Kotlin 版本:
val attr = HashMap<String, Any>()
attr.put("price", 100)
attr.put("productName", "")
PtAgent.getInstance().trackEvent("eventName", attr)
- JAVA 版本:
- /**
- * eventName为事件名称;
- * attributes 为事件的⾃定义变量。
- */
- HashMap<String, Object> attributes = new HashMap<>();
- attributes.put("price", 100);
- attributes.put("productName": "");
- PtAgent.getInstance().trackEvent("eventName",attributes);
- Kotlin 版本:
- val attr = HashMap<String, Any>()
- attr.put("price", 100)
- attr.put("productName", "")
- PtAgent.getInstance().trackEvent("eventName", attr)
JAVA 版本:
/**
* eventName为事件名称;
* attributes 为事件的⾃定义变量。
*/
HashMap<String, Object> attributes = new HashMap<>();
attributes.put("price", 100);
attributes.put("productName": "");
PtAgent.getInstance().trackEvent("eventName",attributes);
Kotlin 版本:
val attr = HashMap<String, Any>()
attr.put("price", 100)
attr.put("productName", "")
PtAgent.getInstance().trackEvent("eventName", attr)
注意:如果在代码不同位置部署两个eventName⼀样的事件名,系统会视为 同⼀个事件,数据会进⾏合并处理。
用于统计用户注册事件,第一个参数为用户注册基本信息,示例中的字段均为ptmind内部字段,不可更改,可选填使用。
如需自定义字段,可通过第二个参数来完全自定义。
JAVA 版本:
JSONObject user_register_info = new JSONObject();
user_register_info.put("pt_account_id", "");
user_register_info.put("pt_gender", "");
user_register_info.put("pt_age", "");
user_register_info.put("pt_register_country", "");
user_register_info.put("pt_register_subdivision", "");
user_register_info.put("pt_register_city", "");
JSONObject user_consumer_attributes = new JSONObject();
attributes.put("phone", "18929392223");
attributes.put("email", "name@mail.com");
PtAgent.getInstance().trackUserRegister(user_register_info, user_consumer_attributes);
Kotlin版本:
val user_register_info = JSONObject()
user_register_info.put("pt_account_id", "")
user_register_info.put("pt_gender", "")
user_register_info.put("pt_age", "")
user_register_info.put("pt_register_country", "")
user_register_info.put("pt_register_subdivision", "")
user_register_info.put("pt_register_city", "")
val consumer_properties = JSONObject()
consumer_properties.put("key1", "value1")
PtAgent.getInstance().trackUserRegister(user_register_info, consumer_properties)
- JAVA 版本:
- JSONObject user_register_info = new JSONObject();
- user_register_info.put("pt_account_id", "");
- user_register_info.put("pt_gender", "");
- user_register_info.put("pt_age", "");
- user_register_info.put("pt_register_country", "");
- user_register_info.put("pt_register_subdivision", "");
- user_register_info.put("pt_register_city", "");
- JSONObject user_consumer_attributes = new JSONObject();
- attributes.put("phone", "18929392223");
- attributes.put("email", "name@mail.com");
- PtAgent.getInstance().trackUserRegister(user_register_info, user_consumer_attributes);
- Kotlin版本:
- val user_register_info = JSONObject()
- user_register_info.put("pt_account_id", "")
- user_register_info.put("pt_gender", "")
- user_register_info.put("pt_age", "")
- user_register_info.put("pt_register_country", "")
- user_register_info.put("pt_register_subdivision", "")
- user_register_info.put("pt_register_city", "")
- val consumer_properties = JSONObject()
- consumer_properties.put("key1", "value1")
- PtAgent.getInstance().trackUserRegister(user_register_info, consumer_properties)
JAVA 版本:
JSONObject user_register_info = new JSONObject();
user_register_info.put("pt_account_id", "");
user_register_info.put("pt_gender", "");
user_register_info.put("pt_age", "");
user_register_info.put("pt_register_country", "");
user_register_info.put("pt_register_subdivision", "");
user_register_info.put("pt_register_city", "");
JSONObject user_consumer_attributes = new JSONObject();
attributes.put("phone", "18929392223");
attributes.put("email", "name@mail.com");
PtAgent.getInstance().trackUserRegister(user_register_info, user_consumer_attributes);
Kotlin版本:
val user_register_info = JSONObject()
user_register_info.put("pt_account_id", "")
user_register_info.put("pt_gender", "")
user_register_info.put("pt_age", "")
user_register_info.put("pt_register_country", "")
user_register_info.put("pt_register_subdivision", "")
user_register_info.put("pt_register_city", "")
val consumer_properties = JSONObject()
consumer_properties.put("key1", "value1")
PtAgent.getInstance().trackUserRegister(user_register_info, consumer_properties)
统计用户登录信息,第一个参数为用户主账号信息,第二个参数为用户自定义参数,字段值为完全自定义。
Java版本:
HashMap<String, Object> attributes = new HashMap();
attributes.put("phone": "18929392223");
attributes.put("email": "name@mail.com");
//userAccount为当前用户登录账号
PtAgent.getInstance().trackUserLogin("userAccount", attributes);
Kotlin版本:
val attr = HashMap<String, Any>()
attr.put("phone", 13882939384)
attr.put("email", "")
PtAgent.getInstance().trackUserLogin("guangfa.miao", attr)
- Java版本:
- HashMap<String, Object> attributes = new HashMap();
- attributes.put("phone": "18929392223");
- attributes.put("email": "name@mail.com");
- //userAccount为当前用户登录账号
- PtAgent.getInstance().trackUserLogin("userAccount", attributes);
- Kotlin版本:
- val attr = HashMap<String, Any>()
- attr.put("phone", 13882939384)
- attr.put("email", "")
- PtAgent.getInstance().trackUserLogin("guangfa.miao", attr)
Java版本:
HashMap<String, Object> attributes = new HashMap();
attributes.put("phone": "18929392223");
attributes.put("email": "name@mail.com");
//userAccount为当前用户登录账号
PtAgent.getInstance().trackUserLogin("userAccount", attributes);
Kotlin版本:
val attr = HashMap<String, Any>()
attr.put("phone", 13882939384)
attr.put("email", "")
PtAgent.getInstance().trackUserLogin("guangfa.miao", attr)
用于统计一个事件的行为时长,比如pv加载时长,pv阅读时长,各种事件时长。该行为的统计需要结合两个接口来共同实现。详见代码示例
Java版本:
/**
*事件开始接口,用于记录某个事件的开始时间,事件名不能为空。
*/
PtAgent.getInstance().beginEvent("eventName");
/**
*事件结束接口,用于计算某个事件结束的时间,事件名不能为空,并且必须与ptBeginEvent接口的事件名完全一致,否则不进行任何统计。
*第二个参数为事件的自定义参数,字段可完全自定义,用于统计关于该事件时长行为的属性
*/
HashMap properties= new HashMap();
properties.put("type","game");
PtAgent.getInstance().endEvent("eventName",parameters);
Kotlin版本:
PtAgent.getInstance().beginEvent("eventName")
val parameters = HashMap<String, Any>()
parameters.put("key1", 100)
parameters.put("key2", "")
PtAgent.getInstance().endEvent("eventName", parameters)
- Java版本:
- /**
- *事件开始接口,用于记录某个事件的开始时间,事件名不能为空。
- */
- PtAgent.getInstance().beginEvent("eventName");
- /**
- *事件结束接口,用于计算某个事件结束的时间,事件名不能为空,并且必须与ptBeginEvent接口的事件名完全一致,否则不进行任何统计。
- *第二个参数为事件的自定义参数,字段可完全自定义,用于统计关于该事件时长行为的属性
- */
- HashMap properties= new HashMap();
- properties.put("type","game");
- PtAgent.getInstance().endEvent("eventName",parameters);
- Kotlin版本:
- PtAgent.getInstance().beginEvent("eventName")
- val parameters = HashMap<String, Any>()
- parameters.put("key1", 100)
- parameters.put("key2", "")
- PtAgent.getInstance().endEvent("eventName", parameters)
Java版本:
/**
*事件开始接口,用于记录某个事件的开始时间,事件名不能为空。
*/
PtAgent.getInstance().beginEvent("eventName");
/**
*事件结束接口,用于计算某个事件结束的时间,事件名不能为空,并且必须与ptBeginEvent接口的事件名完全一致,否则不进行任何统计。
*第二个参数为事件的自定义参数,字段可完全自定义,用于统计关于该事件时长行为的属性
*/
HashMap properties= new HashMap();
properties.put("type","game");
PtAgent.getInstance().endEvent("eventName",parameters);
Kotlin版本:
PtAgent.getInstance().beginEvent("eventName")
val parameters = HashMap<String, Any>()
parameters.put("key1", 100)
parameters.put("key2", "")
PtAgent.getInstance().endEvent("eventName", parameters)
用于统计自定义PV事件
Java版本:
HashMap<String, Object> attributes = new HashMap<>();
attributes.put("key1", 111);
attributes.put("key2", "");
PtAgent.getInstance().trackPVForCustomView("customView", attributes);
Kotlin版本:
val attr = HashMap<String, Any>()
attr.put("", 100)
attr.put("", "")
PtAgent.getInstance().trackPVForCustomView("customView", attr)
- Java版本:
- HashMap<String, Object> attributes = new HashMap<>();
- attributes.put("key1", 111);
- attributes.put("key2", "");
- PtAgent.getInstance().trackPVForCustomView("customView", attributes);
- Kotlin版本:
- val attr = HashMap<String, Any>()
- attr.put("", 100)
- attr.put("", "")
- PtAgent.getInstance().trackPVForCustomView("customView", attr)
Java版本:
HashMap<String, Object> attributes = new HashMap<>();
attributes.put("key1", 111);
attributes.put("key2", "");
PtAgent.getInstance().trackPVForCustomView("customView", attributes);
Kotlin版本:
val attr = HashMap<String, Any>()
attr.put("", 100)
attr.put("", "")
PtAgent.getInstance().trackPVForCustomView("customView", attr)
用于统计自定义错误信息,适用场景比如:登录失败,购买失败等:
Java版本:
PtAgent.getInstance().trackException("10010", "Failed to login with wrong password");
Kotlin版本:
PtAgent.getInstance().trackException("10010", "Failed to login with wrong password")
注意:第一个参数为自定义error code, 其值不能为"Crash". "Crash"作为ptmind SDK自动捕获崩溃信息所用字段
- Java版本:
- PtAgent.getInstance().trackException("10010", "Failed to login with wrong password");
- Kotlin版本:
- PtAgent.getInstance().trackException("10010", "Failed to login with wrong password")
- 注意:第一个参数为自定义error code, 其值不能为"Crash". "Crash"作为ptmind SDK自动捕获崩溃信息所用字段
Java版本:
PtAgent.getInstance().trackException("10010", "Failed to login with wrong password");
Kotlin版本:
PtAgent.getInstance().trackException("10010", "Failed to login with wrong password")
注意:第一个参数为自定义error code, 其值不能为"Crash". "Crash"作为ptmind SDK自动捕获崩溃信息所用字段
第三部分:其他
如果您的APP属于Hybrid,混合了Android 原生开发与Web开发,当希望在web部分采集信息时,
在javascript 脚本里部署如下代码:
首先在你初始化 WebView 的地方,加上如下代码,用于为当前webView注册ptmind javascrpit bridge监听模块:
//mWebView为刚刚初始化的WebView控件
PtAgent.getInstance().registerJavascriptBridgeForWebView(mWebView);
- //mWebView为刚刚初始化的WebView控件
- PtAgent.getInstance().registerJavascriptBridgeForWebView(mWebView);
//mWebView为刚刚初始化的WebView控件
PtAgent.getInstance().registerJavascriptBridgeForWebView(mWebView);
然后根据你的实际需求,选择性的部署如下代码:
(1)自定义事件采集
if(window.PtAgentWebViewJSBridge){
var paras = {name:'shirt',price:1.0};
//第一个参数为事件名称,第二个参数为事件参数(字符化后的json串)
PtAgentWebViewJSBridge.PTTrackEvent('buy',JSON.stringify(paras));
}
- if(window.PtAgentWebViewJSBridge){
- var paras = {name:'shirt',price:1.0};
- //第一个参数为事件名称,第二个参数为事件参数(字符化后的json串)
- PtAgentWebViewJSBridge.PTTrackEvent('buy',JSON.stringify(paras));
- }
if(window.PtAgentWebViewJSBridge){
var paras = {name:'shirt',price:1.0};
//第一个参数为事件名称,第二个参数为事件参数(字符化后的json串)
PtAgentWebViewJSBridge.PTTrackEvent('buy',JSON.stringify(paras));
}
(2)虚拟pv采集
if(window.PtAgentWebViewJSBridge){
var paras = {'key1': '', 'key2': ''}
//参数为自定义的页面名称
PtAgentWebViewJSBridge.PTPackVirtualPageView("pageName", JSON.stringify(paras));
}
- if(window.PtAgentWebViewJSBridge){
- var paras = {'key1': '', 'key2': ''}
- //参数为自定义的页面名称
- PtAgentWebViewJSBridge.PTPackVirtualPageView("pageName", JSON.stringify(paras));
- }
if(window.PtAgentWebViewJSBridge){
var paras = {'key1': '', 'key2': ''}
//参数为自定义的页面名称
PtAgentWebViewJSBridge.PTPackVirtualPageView("pageName", JSON.stringify(paras));
}
(3)错误信息采集
if(window.PtAgentWebViewJSBridge){
//第一个参数为异常描述,第二个参数为异常CODE
PtAgentWebViewJSBridge.PTTrackException('this is a exception','100001');
}
- if(window.PtAgentWebViewJSBridge){
- //第一个参数为异常描述,第二个参数为异常CODE
- PtAgentWebViewJSBridge.PTTrackException('this is a exception','100001');
- }
if(window.PtAgentWebViewJSBridge){
//第一个参数为异常描述,第二个参数为异常CODE
PtAgentWebViewJSBridge.PTTrackException('this is a exception','100001');
}