others-AppsFlyer第三方统计
build.gradle 引入库
dependencies { implementation 'com.appsflyer:af-android-sdk:5.4.0' implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0' implementation 'com.android.installreferrer:installreferrer:2.1' }权限 及 广播 的 清单配置 AndroidManifest.xml
<manifest> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- Optional : --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <application> <!-- appsflyer --> <receiver android:name="com.appsflyer.SingleInstallBroadcastReceiver" android:exported="true"> <intent-filter> <action android:name="com.android.vending.INSTALL_REFERRER" /> </intent-filter> </receiver> </application> </manifest>混淆配置 proguard-user.txt
######### appsflyer ######### -keep public class com.android.installreferrer.** { *; } -keep class com.appsflyer.** { *; } -dontwarn com.android.installreferrerjava 代码
package com.yang.androidaar.other; import android.content.Context; import android.util.Log; import com.appsflyer.AppsFlyerConversionListener; import com.appsflyer.AppsFlyerLib; import com.yang.androidaar.Define; import com.yang.androidaar.JsonTool; import com.yang.androidaar.MyCode.ECode; import com.yang.androidaar.Tools; import com.yang.androidaar.model.JsonBean.CEventInfo; import java.util.HashMap; import java.util.Map; public class AppsflyerHelper { static AppsflyerHelper instance = null; public static AppsflyerHelper getIns() { if (instance == null) { instance = new AppsflyerHelper(); } return instance; } private static final String TAG = "--- AppsflyerHelper"; private String mAfJson = null; private boolean mIsCall = false; public void init(Context context, boolean isDebug, Define.JsonRunnable task) { try { // af 回调 innerInit(context, isDebug, (code, afJson) -> { if (code != ECode.Ok) { return; } mAfJson = afJson; }); } catch (Exception e) { e.printStackTrace(); callTask(task); } } private void innerInit(Context context, boolean isDebug, Define.CodeRunnable task) { String devKey = Tools.GetStringVaule(context, "af_dev_key"); // 获取 af 后台的秘钥 AppsFlyerConversionListener conversionListener = new AppsFlyerConversionListener() { @Override public void onConversionDataSuccess(Map<String, Object> conversionData) { // 转化回调 task.run(ECode.Ok, JsonTool.toJson(conversionData)); } @Override public void onConversionDataFail(String errorMessage) { task.run(ECode.LoginError, errorMessage); } @Override public void onAppOpenAttribution(Map<String, String> attributionData) { // 深度连接的回调: https://support.appsflyer.com/hc/zh-cn/articles/213766183-%E5%BC%80%E5%8F%91%E8%80%85Unity-Plugin-V4%E5%AF%B9%E6%8E%A5%E6%8C%87%E5%8D%97#%E6%A0%B8%E5%BF%83api Log.d(TAG, "onAppOpenAttribution: "); } @Override public void onAttributionFailure(String errorMessage) { Log.d(TAG, "onAttributionFailure: "); } }; // af 初始化, 在 onCreate 的时候就调用 AppsFlyerLib.getInstance().init(devKey, conversionListener, context); AppsFlyerLib.getInstance().startTracking(context); AppsFlyerLib.getInstance().setDebugLog(isDebug); } public void logEvent(Context context, String jsonMsg, Define.CodeRunnable task) { CEventInfo ei = JsonTool.toObject(jsonMsg, CEventInfo.class); logEvent(context, ei, task); } public void logEvent(Context context, CEventInfo ei, Define.CodeRunnable task) { if (ei == null || ei.name.length() == 0) { return; } AppsFlyerLib.getInstance().trackEvent(context, ei.name, ei.params); } public String getInfo() { Map<String, Object> args = new HashMap<>(); args.put("AfJson", mAfJson); return JsonTool.toJson(args); } private void callTask(Define.JsonRunnable task) { if (mIsCall || task == null) { return; } mIsCall = true; task.run(mAfJson); } }Google Play 应用的短链推广
后台配置
onConversionDataSuccess 中可以获取到对应的参数
需要 Facebook 对应的 app id 接收 服务条款, 才能在 af 的 onConversionDataSuccess 回调中, 接收到 campaign 相关数据
链接: https://www.facebook.com/ads/manage/advanced_mobile_measurement/app_based_tos?appid=123123 (123123 是 app id)
参考: 设备层级数据
官方文档: 适用于移动设备的服务器到服务器事件API
测试用例
# appsflyer def test_afEvent(self): pkgName = "com.aaa.bbb" # 安卓包名 secret = "axYvBxxxxxxxxxxxxx" # dev key afuid = "1602570356839-6524xxxxxxxxxxxxxx" # af uid url = "https://api2.appsflyer.com/inappevent/{}".format(pkgName) headers = { "authentication": secret, "Content-Type": "application/json", } data = { "appsflyer_id": afuid, # af uid "eventName": "test_yx01_af_purchase", # 事件 "eventCurrency": "INR", # 币种 "eventValue": utils.beautyJson({ "af_revenue": "100", # 收益 }, indent=None), } code, rspDct = utils.httpPost(url, dct=data, headers=headers) print("--- code: {}".format(code)) print("--- rsp: {}".format(utils.beautyJson(rspDct)))