Skip to content

Instantly share code, notes, and snippets.

@zhangzhibin
Last active May 20, 2021 10:32
Show Gist options
  • Save zhangzhibin/c9b8b250f6d06d213f1ec9f2087643b9 to your computer and use it in GitHub Desktop.
Save zhangzhibin/c9b8b250f6d06d213f1ec9f2087643b9 to your computer and use it in GitHub Desktop.
Helper for TA Android SDK, 数数科技TA 安卓SDK 调用封装。 contact me at https://xmanyou.com
package sdkhelper;
import android.content.Context;
import android.util.Log;
import com.adjust.sdk.Adjust;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import cn.thinkingdata.android.TDConfig;
import cn.thinkingdata.android.ThinkingAnalyticsSDK;
public class TAHelper {
public static final String TAG = "TAHelper";
public static final String TA_APP_ID="<TA项目的APP ID>";
public static final String TA_SERVER_URL="<TA 数据采集地址>";
public static ThinkingAnalyticsSDK ta = null;
// 初始化并设置TA SDK
public static void onCreate(Context context, boolean enableAutoTracking, boolean bindAdjust, boolean debugMode){
// 获取 TDConfig 实例
TDConfig config = TDConfig.getInstance(context, TA_APP_ID, TA_SERVER_URL);
// 初始化 SDK
ta = ThinkingAnalyticsSDK.sharedInstance(config);
// 是否开启调试日志
ThinkingAnalyticsSDK.enableTrackLog(debugMode);
// 设置adjust的回调参数(填入TA所需的属性值用于标识用户)
if(bindAdjust){
setAdjustSessionCallbackParameter();
setAjustId(Adjust.getAdid());
}
// 开启自动采集数据
enableAutoTrack(enableAutoTracking);
}
// 设置adjust回传事件的公共属性,便于TA服务器在接收adjust事件时能识别用户
// 关于TA的用户id规则:
// https://doc.thinkingdata.cn/ta-manual/latest/installation/pre_installation/user_identify.html
public static void setAdjustSessionCallbackParameter(){
if(ta == null){
Log.d(TAG, "setAdjustSessionCallbackParameter failed, TA not inited!");
return;
}
// 设置访客id:ta_distinct_id
final String KEY_DISTINCT_ID = "ta_distinct_id";
String distinctId = ta.getDistinctId();
Adjust.addSessionCallbackParameter(KEY_DISTINCT_ID,
distinctId);
Log.d(TAG, "setAdjustSessionCallbackParameter, ta_distinct_id = " + distinctId);
// // 设置账号id: ta_account_id,如果该游戏没有账号系统,则不需要
// final String KEY_ACCOUNT_ID = "ta_account_id";
// String accountId = "{your_account_id}";
// Adjust.addSessionCallbackParameter(KEY_ACCOUNT_ID,
// accountId);
// Log.d(TAG, "setAdjustSessionCallbackParameter, ta_account_id = empty");
}
// 开启自动追踪事件
// 参考:https://doc.thinkingdata.cn/ta-manual/latest/installation/installation_menu/client_sdk/android_sdk_installation/android_sdk_autotrack.html
public static void enableAutoTrack(boolean enable){
Log.d(TAG, "enableAutoTrack: " + enable);
if(!enable){
// 如果不需要自动采集,则直接返回
// TASDK 没有提供关闭开关
return;
}
List<ThinkingAnalyticsSDK.AutoTrackEventType> eventTypeList = new ArrayList<>();
//APP安装事件
eventTypeList.add(ThinkingAnalyticsSDK.AutoTrackEventType.APP_INSTALL);
//APP启动事件
eventTypeList.add(ThinkingAnalyticsSDK.AutoTrackEventType.APP_START);
//APP关闭事件
eventTypeList.add(ThinkingAnalyticsSDK.AutoTrackEventType.APP_END);
// //APP浏览页面事件
// eventTypeList.add(ThinkingAnalyticsSDK.AutoTrackEventType.APP_VIEW_SCREEN);
// //APP点击控件事件
// eventTypeList.add(ThinkingAnalyticsSDK.AutoTrackEventType.APP_CLICK);
//APP崩溃事件
eventTypeList.add(ThinkingAnalyticsSDK.AutoTrackEventType.APP_CRASH);
//开启自动采集事件
ta.enableAutoTrack(eventTypeList);
}
// 关联adjust的adid
private static void setAjustId(String adid){
Log.d(TAG, "setAjustId: " + adid);
if (adid == null || adid == "") {
return;
}
//设置公共事件属性: adid
try {
// 设置用户的adid属性
JSONObject adidInfo = new JSONObject();
adidInfo.put("adid", adid);
ta.user_set(adidInfo);
// 为所有事件添加adid字段
JSONObject superProperties = ta.getSuperProperties();
if(superProperties == null){
superProperties = new JSONObject();
}
superProperties.put("adid", adid);
ta.setSuperProperties(superProperties);
} catch (org.json.JSONException e) {
Log.e(TAG, "setAjustId Error: " + e.getLocalizedMessage());
e.printStackTrace();
}
}
// 手动采集事件(事件打点)
// https://doc.thinkingdata.cn/ta-manual/latest/installation/installation_menu/client_sdk/android_sdk_installation/android_sdk_installation.html#_3-1-%E5%8F%91%E9%80%81%E4%BA%8B%E4%BB%B6
public static void logEvent(String eventName, JSONObject properties){
if(properties == null){
ta.track(eventName);
}else{
ta.track(eventName, properties);
}
}
// 手动采集事件(传入json字符串)
public static void logEvent(String eventName, String jsonParam){
JSONObject properties = null;
if(jsonParam!=null && jsonParam.length()>0){
try{
properties = new JSONObject(jsonParam);
}catch (org.json.JSONException e){
Log.d(TAG, "logEvent: Parse Json failed: " + e.getLocalizedMessage());
}
}
logEvent(eventName, properties);
}
// 记录一个事件的时长
// https://doc.thinkingdata.cn/ta-manual/latest/installation/installation_menu/client_sdk/android_sdk_installation/android_sdk_installation.html#_3-4-%E8%AE%B0%E5%BD%95%E4%BA%8B%E4%BB%B6%E6%97%B6%E9%95%BF
// 开始计时器事件,并上报启动事件
// 注意:相同timer只能一个在计时,如果没有及时停止计时(调用stopTimerEvent),则会覆盖上一个计时器,并重新计时
public static void startTimeEvent(String timerEventName, String startEventName, JSONObject properties){
ta.timeEvent(timerEventName);
// 是否需要上传开始事件
if(startEventName!=null && startEventName.length()>0){
logEvent(startEventName, properties);
}
}
public static void startTimeEvent(String timerEventName, String startEventName, String jsonParam){
JSONObject properties = null;
if(jsonParam!=null && jsonParam.length()>0){
try{
properties = new JSONObject(jsonParam);
}catch (org.json.JSONException e){
Log.d(TAG, "logEvent: Parse Json failed: " + e.getLocalizedMessage());
}
}
startTimeEvent(timerEventName, startEventName, properties);
}
// 结束计时,并上传事件
// 注意:相同timer只能一个在计时,所以要及时停止计时,否则重复调用start的话,计时器会被重置
public static void stopTimeEvent(String timerEventName, JSONObject properties){
logEvent(timerEventName, properties);
}
public static void stopTimeEvent(String timerEventName, String jsonParam){
logEvent(timerEventName, jsonParam);
}
}
// 使用
// 在MainActivity中
// Adjust初始化结束后
// 添加数数TA初始化
// TAHelper.onCreate(this, true, true, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment