Android版AppsFlyer的Unity简单接入

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

现在打开的AppsFlyer的官方帮助文档,Unity版本的接入指南已经没有了,只能找到Android版的和IOS版本的指南,就只能写一个简单的Android的接入,比较简单,仅供自己在下次使用的时候参考,只添加了上报事件的功能,它原来版本添加SDK里面的很多功能没有添加


一、Android的配置和代码

1.引入库

dependencies {
    compileOnly files('libs/CorePlayingNativeLib.aar')
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.appsflyer:af-android-sdk:6.1.4'
    implementation 'com.android.installreferrer:installreferrer:1.0'
    compile 'cn.hutool:hutool-all:5.5.7'
}

这些是阅读官网的时候发现要添加的库,第一行代码是公司自己的库,添加于本地的,这个库在这里只是提供一个获取当前的Activity的方法,不用太在意

1.代码

import java.util.Map;
import android.util.Log;
import java.util.HashMap;
import com.appsflyer.AppsFlyerLib;
import com.appsflyer.AppsFlyerConversionListener;
import com.coreplaying.coreplayingnativelib.CorePlayingNativeLib;


class AppsFlyerNativeLib {
    private static AppsFlyerNativeLib m_instance;
    private String TAG = "AppsFlyerNativeLib";

    public static AppsFlyerNativeLib getInstance()
    {
        if (m_instance == null)
            m_instance = new AppsFlyerNativeLib();
        return m_instance;
    }

    private String AF_DEV_KEY = "yourkey";
    private String currencyType = "USD";

    public void Init(String devKey,String currencyType){
        Log.d(TAG, "Init: devKey = " + devKey + "    currencyType = " + currencyType);
        if (devKey.isEmpty() == false)
            this.AF_DEV_KEY = devKey;
        if (currencyType.isEmpty() && currencyType != "")
            this.currencyType = currencyType;
        else
            this.currencyType = "USD";

        AppsFlyerConversionListener conversionListener = new AppsFlyerConversionListener() {
            @Override
            public void onConversionDataSuccess(Map<String, Object> map) {
                for(String attrName:map.keySet())
                {
                    Log.d(TAG, "attribute: " + attrName + " = " + map.get(attrName));
                }
            }

            @Override
            public void onConversionDataFail(String s) {
                Log.d(TAG, "onConversionDataFail: " + s);
            }

            @Override
            public void onAppOpenAttribution(Map<String, String> map) {
                for (String attrName:map.keySet()){
                    Log.d(TAG, "onAppOpenAttribution: " + attrName + " = " + map.get(attrName));
                }
            }
            @Override
            public void onAttributionFailure(String s) {
                Log.d(TAG, "onAttributionFailure: " + s);
            }
        };

        AppsFlyerLib.getInstance().init(AF_DEV_KEY,conversionListener, CorePlayingNativeLib.GetUnityActivity());
        AppsFlyerLib.getInstance().start(CorePlayingNativeLib.GetUnityActivity());
    }


    public void SendEvent(String eventName,HashMap<String, Object> eventValues)
    {
        AppsFlyerLib.getInstance().logEvent(CorePlayingNativeLib.GetUnityActivity(),eventName,eventValues);
    }
}

这就是安卓端的代码,只有初始化和事件上报的内容

二、Unity端的代码

1.C#

public class AndroidAppsFlyerManager
{
    private AndroidJavaObject appsFlyerObject { get; set; }
    private string eventName { get; set; } = "eventId";
    private string initKey { get; set; } = "Init";
    private string currencyType { get; set; } = "currencyType";
    private string devKeyName { get; set; } = "devKey";


    public static AndroidAppsFlyerManager Instance
    {
        get
        {
            if (m_Instance == null)
                m_Instance = new AndroidAppsFlyerManager();
            return m_Instance;
        }
    }
    private bool isInit { get; set; } = false;
    private static AndroidAppsFlyerManager m_Instance;

    private AndroidAppsFlyerManager()
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        Debug.Log("AndroidAppsFlyerManager:AndroidAppsFlyerManager");
        var packageName = "com.coreplaying.appsflyernativelib.AppsFlyerNativeLib";

        AndroidJavaClass javaClass = new AndroidJavaClass(packageName);
        if (javaClass == null)
        {
            Debug.LogError("未能获得Android对象,无法调用Affsflyer上报数据");
            return;
        }
        appsFlyerObject = javaClass.CallStatic<AndroidJavaObject>("getInstance");
        
        if (appsFlyerObject == null)
        {
            Debug.LogError("未能获得Android对象,无法调用Affsflyer上报数据");
            return;
        }
        else
        {
            Debug.Log("AndroidAppsFlyerManager:AndroidAppsFlyerManager:成功");
        }
#endif
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="currencyType">货币类型</param>
    private void Init(string devKey,string currencyType)
    {
        if (this.appsFlyerObject == null)
            return;
        Debug.Log("AndroidAppsFlyerManager:Init");
        if (string.IsNullOrEmpty(currencyType))
            currencyType = "USD";

        appsFlyerObject.Call("Init", devKey, currencyType);
    }
    public void SendEvent(string jsonData)
    {
        if (this.appsFlyerObject == null)
            return;
        Debug.Log("Unity:AndroidAppsFlyerManager:SendEvent---->" + jsonData);
        var evtDic = this.JasonToDictionary(jsonData);
        if (evtDic.Count == 0)
        {
            Debug.LogError("AndroidAppsFlyerManager:SendEvent:数据有误无法转换--->" + jsonData);
            return;
        }
        if (evtDic[this.eventName] == this.initKey)
        {
            string currency = string.Empty;
            string devKey = string.Empty;
            if (evtDic.ContainsKey(currencyType))
                currency = evtDic[currencyType];
            if (evtDic.ContainsKey(devKeyName))
                devKey = evtDic[devKeyName];
            Init(devKey,currency);
            return;
        }
        appsFlyerObject.Call("SendEvent", evtDic[this.eventName], convertDictionaryToJavaMap(evtDic));
    }

    private Dictionary<string, string> JasonToDictionary(string jasonData)
    {
        if (string.IsNullOrEmpty(jasonData))
        {
            Debug.LogError("AppEventRequest:ProcessData---->jasonData = null");
            return new Dictionary<string, string>();
        }

        List<object> items = (List<object>)MiniJSON.Json.Deserialize(jasonData);
        if (items == null)
        {
            Debug.LogError("AppEventRequest:ProcessData---->items = null");
            return new Dictionary<string, string>();
        }

        var evtDic = new Dictionary<string, string>();
        for (int i = 0; i < items.Count; ++i)
        {
            var evtList = items[i] as List<object>;
            if (evtList == null)
            {
                Debug.LogError("AppEventRequest:ProcessData:evtList == null");
                return new Dictionary<string, string>();
            }
            evtDic.Add(evtList[0].ToString(), evtList[1].ToString());
        }
        return evtDic;
    }
    private static AndroidJavaObject convertDictionaryToJavaMap(Dictionary<string, string> dictionary)
    {
        AndroidJavaObject map = new AndroidJavaObject("java.util.HashMap");
        IntPtr putMethod = AndroidJNIHelper.GetMethodID(map.GetRawClass(), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
        if (dictionary != null)
        {
            foreach (var entry in dictionary)
            {
                AndroidJNI.CallObjectMethod(map.GetRawObject(), putMethod, AndroidJNIHelper.CreateJNIArgArray(new object[] { entry.Key, entry.Value }));
            }
        }
        return map;
    }
}

初始化和上传事件的都是通过SendEvent(string json)上传的,参数为json字符串

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值