123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667 |
- using UnityEngine;
- using System.Runtime.InteropServices;
- using AOT;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Collections;
- public class Tracking: MonoBehaviour
- {
- private static Tracking _instance = null;
- //private static Regex regex = new Regex("^([a-zA-Z])([a-zA-Z0-9_]){0,63}$");
- public static Tracking Instance {
- get {
- if (!_instance) {
- _instance = FindObjectOfType (typeof(Tracking)) as Tracking;
- if (!_instance) {
- GameObject am = new GameObject ("Tracking");
- _instance = am.AddComponent (typeof(Tracking)) as Tracking;
- }
- }
- return _instance;
- }
- }
- private static List<Action> waitingTaskList = new List<Action>();
-
- private static List<Action> executingTaskList = new List<Action>();
- public static void PostTask(Action task)
- {
- lock (waitingTaskList)
- {
- waitingTaskList.Add(task);
- }
- }
- private void Update()
- {
- lock (waitingTaskList)
- {
- if (waitingTaskList.Count > 0)
- {
- executingTaskList.AddRange(waitingTaskList);
- waitingTaskList.Clear();
- }
- }
- for (int i = 0; i < executingTaskList.Count; ++i)
- {
- Action task = executingTaskList[i];
- try
- {
- task();
- }
- catch (Exception e)
- {
- Debug.LogError(e.Message, this);
- }
- }
- executingTaskList.Clear();
- }
- void Awake ()
- {
- DontDestroyOnLoad (this);
- }
- public delegate void DeferredDeeplinkCallBack(string result);
- public delegate void AttributionCalllback(string result, int status);
- private DeferredDeeplinkCallBack deferredDeeplinCallback_private = null;
- private AttributionCalllback attributionCalllback_private = null;
- #if UNITY_IOS && !UNITY_EDITOR
- internal delegate void _internalDeferredDeeplinkCallBack(string result);
- internal delegate void _internalAttributionCalllback(string result, int status);
- [DllImport ("__Internal")]
- private static extern void _internalInitWithAppKeyAndChannel_Tracking (string appKey, string channelId);
- [DllImport ("__Internal")]
- private static extern void _preInit_newer (string appKey);
- [DllImport ("__Internal")]
- private static extern void _internalInitWithAppKeyAndChannel_Tracking_newer (string appKey, string channelId,string caid1,string caid2,string oid,string install_params,string startup_params);
- [DllImport ("__Internal")]
- private static extern void _internalSetRegisterWithAccountID_Tracking_newer (string account,string params_string);
- [DllImport ("__Internal")]
- private static extern void _internalSetLoginWithAccountIDAndServerId_Tracking_newer (string account,string serverId,string params_string);
- [DllImport ("__Internal")]
- private static extern void _internalSetRyzf_Tracking_newer (string ryTID, string ryzfType, string hbType, float hbAmount,string params_string);
- [DllImport ("__Internal")]
- private static extern void _internalSetDD_Tracking_newer (string ryTID, string hbType, float hbAmount,string params_string);
- [DllImport ("__Internal")]
- private static extern void _internalSetEvent_Tracking_newer (string EventName,string params_string);
- [DllImport ("__Internal")]
- private static extern void _internalSetTrackViewDuration_Tracking_newer (string viewID,long duration,string params_string);
- [DllImport ("__Internal")]
- private static extern void _internalSetAdShow_Tracking_newer(string adPlatform,string adid,int success,string params_string);
- [DllImport ("__Internal")]
- private static extern void _internalSetAdClick_Tracking_newer(string adPlatform,string adid,string params_string);
- [DllImport ("__Internal")]
- private static extern void _internalSetTrackAppDuration_Tracking_newer(long duration,string params_string);
- [DllImport ("__Internal")]
- private static extern string _internalGetDeviceId_Tracking ();
- [DllImport ("__Internal")]
- private static extern void _internalSetPrintLog_Tracking (bool printLog);
- [DllImport ("__Internal")]
- private static extern void _internalSetASAEnable_Tracking (bool enable);
-
- [DllImport("__Internal")]
- private static extern void _internal_SetAttributionCalllbackDelegate_Tracing(_internalAttributionCalllback callback);
- [DllImport("__Internal")]
- private static extern void _internal_SetDeferredDeeplinkCallBack_Tracking(_internalDeferredDeeplinkCallBack callback);
- [DllImport ("__Internal")]
- private static extern void _internalSetIPAdds6Enable_Tracking (bool enable);
- #endif
- #if UNITY_ANDROID && !UNITY_EDITOR
- public static AndroidJavaObject getApplicationContext ()
- {
-
- using (AndroidJavaClass jc = new AndroidJavaClass ("com.unity3d.player.UnityPlayer")) {
- using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject> ("currentActivity")) {
- return jo.Call<AndroidJavaObject> ("getApplicationContext");
- }
- }
-
- return null;
- }
- #endif
- /// <summary>
- /// 预初始化方法
- /// </summary>
- public void preInit(string appKey)
- {
- #if UNITY_IOS && !UNITY_EDITOR
-
- _preInit_newer (appKey);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- using (AndroidJavaClass TrackingIO = new AndroidJavaClass("com.reyun.tracking.sdk.Tracking"))
- {
- TrackingIO.CallStatic("preInit", getApplicationContext(), appKey);
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- /// <summary>
- /// 初始化方法
- /// </summary>
- /// <param name="appKey">appKey</param>
- /// <param name="channelId">标识推广渠道的字符</param>
- /// <param name="caid1">caid1 ios 专用字段,广告协会caid字段,默认为空</param>
- /// <param name="caid2">caid2 ios 专用字段,广告协会caid字段,默认为空</param>
- /// <param name="startupParams">自定义startup参数</param>
- /// <param name="installParams">自定义install参数</param>
- public void init (string appKey, string channelId,string caid1 = null,string caid2 = null,
- Dictionary<string,object> startupParams = null, Dictionary<string, object> installParams = null)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- string paramstring_install = DictionaryToJsonString(installParams);
- if (paramstring_install == null) {
- paramstring_install = "{}";
- }
- string paramstring_startup = DictionaryToJsonString(startupParams);
- if (paramstring_startup == null) {
- paramstring_startup = "{}";
- }
- _internalInitWithAppKeyAndChannel_Tracking_newer (appKey, channelId,caid1,caid2,null, paramstring_install,paramstring_startup);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- string paramstring_install = DictionaryToJsonString(installParams);
- if (paramstring_install == null) {
- paramstring_install = "{}";
- }
- string paramstring_startup = DictionaryToJsonString(startupParams);
- if (paramstring_startup == null) {
- paramstring_startup = "{}";
- }
- using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.tracking.sdk.Tracking")) {
- TrackingIO.CallStatic ("initWithKeyAndChannelId", getApplicationContext (), appKey, channelId,paramstring_startup,paramstring_install,null,false);
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- /// <summary>
- /// 玩家服务器注册
- /// </summary>
- /// <param name="account">账号ID</param>
- ///
- public void register (string account, Dictionary<string, object> customParams = null)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- _internalSetRegisterWithAccountID_Tracking_newer(account,paramstring);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- if (paramstring == null) {
- paramstring = "{}";
- }
- using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.tracking.sdk.Tracking")) {
- TrackingIO.CallStatic ("setRegisterWithAccountID", account,paramstring);
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- /// <summary>
- /// 玩家的账号登陆服务器
- /// </summary>
- /// <param name="account">账号</param>
- public void login (string account,string serverId = null, Dictionary<string, object> customParams = null)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- _internalSetLoginWithAccountIDAndServerId_Tracking_newer (account,serverId,paramstring);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- if (paramstring == null) {
- paramstring = "{}";
- }
- using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.tracking.sdk.Tracking")) {
- TrackingIO.CallStatic ("setLoginSuccessBusiness", account,serverId,paramstring);
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- /// <summary>
- /// 玩家的充值数据
- /// </summary>
- /// <param name="ryTID">交易的流水号</param>
- /// <param name="ryzfType">支付类型</param>
- /// <param name="hbType">货币类型</param>
- /// <param name="hbAmount">支付的真实货币的金额</param>
- public void setryzf (string ryTID, string ryzfType, string hbType, float hbAmount, Dictionary<string, object> customParams = null)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- _internalSetRyzf_Tracking_newer (ryTID, ryzfType, hbType, hbAmount,paramstring);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- if (paramstring == null) {
- paramstring = "{}";
- }
- using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.tracking.sdk.Tracking")) {
- TrackingIO.CallStatic ("setPayment", ryTID, ryzfType, hbType, hbAmount,paramstring);
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- /// <summary>
- /// 玩家的订单数据
- /// </summary>
- /// <param name="ryTID">交易的流水号</param>
- /// <param name="hbType">货币类型</param>
- /// <param name="hbAmount">支付的真实货币的金额</param>
- public void setDD (string ryTID, string hbType, float hbAmount, Dictionary<string, object> customParams = null)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- _internalSetDD_Tracking_newer (ryTID,hbType,hbAmount,paramstring);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- if (paramstring == null) {
- paramstring = "{}";
- }
- using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.tracking.sdk.Tracking")) {
- TrackingIO.CallStatic ("setOrder", ryTID, hbType, hbAmount,paramstring);
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- /// <summary>
- /// 统计玩家的自定义事件
- /// </summary>
- /// <param name="eventName">事件名 必须为event_1到event_30</param>
- public void setEvent (string eventName, Dictionary<string, object> customParams = null)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- _internalSetEvent_Tracking_newer (eventName,paramstring);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- if (paramstring == null) {
- paramstring = "{}";
- }
- using (AndroidJavaClass reyun = new AndroidJavaClass ("com.reyun.tracking.sdk.Tracking")) {
- reyun.CallStatic ("setEvent", eventName, paramstring);
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- /// <summary>
- /// 监测页面展示时长
- /// </summary>
- /// <param name="pageID">页面唯一标识</param>
- /// <param name="duration">页面展示时长</param>
- public void setTrackViewDuration(string pageID,long duration, Dictionary<string, object> customParams = null)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- _internalSetTrackViewDuration_Tracking_newer(pageID,duration,paramstring);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- if (paramstring == null) {
- paramstring = "{}";
- }
- using (AndroidJavaClass reyun = new AndroidJavaClass("com.reyun.tracking.sdk.Tracking"))
- {
- reyun.CallStatic("setPageDuration", pageID, duration*1000,paramstring);
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- /// <summary>
- /// //广告展示时调用
- /// </summary>
- /// <param name="adPlatform">广告平台缩写,如穿山甲广告平台传入 "csj"</param>
- /// <param name="adId">广告位ID</param>
- /// /// <param name="playSuccess">是否展示成功</param>
- public void setTrackAdShow(string adPlatform,string adId,bool playSuccess, Dictionary<string, object> customParams = null)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- int successInt = playSuccess ? 1 : 2;
- _internalSetAdShow_Tracking_newer(adPlatform,adId,successInt,paramstring);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- string successString = playSuccess ? "1" : "2";
- string paramstring = DictionaryToJsonString(customParams);
- if (paramstring == null) {
- paramstring = "{}";
- }
- using (AndroidJavaClass reyun = new AndroidJavaClass("com.reyun.tracking.sdk.Tracking"))
- {
- reyun.CallStatic("setAdShow", adPlatform, adId,successString,paramstring);
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- /// <summary>
- /// //广告点击时调用
- /// </summary>
- /// <param name="adPlatform">广告平台缩写,如穿山甲广告平台传入 "csj"</param>
- /// <param name="adId">广告位ID</param>
- public void setTrackAdClick(string adPlatform, string adId, Dictionary<string, object> customParams = null)
- {
-
- #if UNITY_IOS && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- _internalSetAdClick_Tracking_newer(adPlatform,adId,paramstring);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- if (paramstring == null) {
- paramstring = "{}";
- }
- using (AndroidJavaClass reyun = new AndroidJavaClass("com.reyun.tracking.sdk.Tracking"))
- {
- reyun.CallStatic("setAdClick", adPlatform, adId,paramstring);
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- /// <summary>
- /// 统计APP运行时长
- /// </summary>
- /// <param name="duration">时间长度</param>
- public void setTrackAppDuration(long duration, Dictionary<string, object> customParams = null)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- _internalSetTrackAppDuration_Tracking_newer(duration,paramstring);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- string paramstring = DictionaryToJsonString(customParams);
- if (paramstring == null) {
- paramstring = "{}";
- }
- using (AndroidJavaClass reyun = new AndroidJavaClass("com.reyun.tracking.sdk.Tracking"))
- {
- reyun.CallStatic("setAppDuration", duration*1000,paramstring);
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- /// <summary>
- /// 获取用户的设备ID信息
- /// </summary>
- public string getDeviceId ()
- {
- #if UNITY_IOS && !UNITY_EDITOR
- return _internalGetDeviceId_Tracking ();
- #elif UNITY_ANDROID && !UNITY_EDITOR
- string str = "unknown";
- using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.tracking.sdk.Tracking")) {
- str = TrackingIO.CallStatic<string> ("getDeviceId");
- }
- return str;
- #else
- Debug.LogError("Current platform not implemented!");
- return "unknown";
- #endif
- }
- /// 开启日志打印
- public void setPrintLog (bool print)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- _internalSetPrintLog_Tracking (print);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.tracking.sdk.Tracking")) {
- TrackingIO.CallStatic("setDebugMode", print);
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- public void setASAEnable(bool enable)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- _internalSetASAEnable_Tracking (enable);
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- #if UNITY_IOS && !UNITY_EDITOR
- [MonoPInvokeCallback(typeof(_internalDeferredDeeplinkCallBack))]
- private static void OnDeeplinkCallback(string msg)
- {
- PostTask(() =>
- {
- if (Tracking.Instance.deferredDeeplinCallback_private != null && msg != null)
- {
- Tracking.Instance.deferredDeeplinCallback_private.Invoke(msg);
- }
- });
- }
- [MonoPInvokeCallback(typeof(_internalAttributionCalllback))]
- private static void OnAttibutionCallback(string result, int status)
- {
- PostTask(() =>
- {
- if (Tracking.Instance.attributionCalllback_private != null)
- {
- Tracking.Instance.attributionCalllback_private.Invoke(result, status);
- }
- });
- }
- #endif
- #region iOS install Attribution
- public void setAttributionCalllbackDelegate(AttributionCalllback callback)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- Tracking.Instance.attributionCalllback_private = callback;
- _internal_SetAttributionCalllbackDelegate_Tracing(OnAttibutionCallback);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- Tracking.Instance.attributionCalllback_private = callback;
- using (AndroidJavaClass TrackingIO = new AndroidJavaClass("com.reyun.tracking.sdk.Tracking"))
- {
- TrackingIO.CallStatic("setAttributionQueryListener", new AndroidAttributionQuery());
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- #endregion
- #region iOS deferred deeplink
- public void setDeferredDeeplinkCalllbackDelegate(DeferredDeeplinkCallBack callback)
- {
- #if UNITY_IOS && !UNITY_EDITOR
- Tracking.Instance.deferredDeeplinCallback_private = callback;
- _internal_SetDeferredDeeplinkCallBack_Tracking(OnDeeplinkCallback);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- Tracking.Instance.deferredDeeplinCallback_private = callback;
- using (AndroidJavaClass TrackingIO = new AndroidJavaClass("com.reyun.tracking.sdk.Tracking"))
- {
- TrackingIO.CallStatic("setDeepLinkListener", new AndroidDeferredDeeplink());
- }
- #else
- Debug.LogError("Current platform not implemented!");
- #endif
- }
- #endregion
- #if UNITY_ANDROID && !UNITY_EDITOR
- private sealed class AndroidAttributionQuery : AndroidJavaProxy {
- public AndroidAttributionQuery() : base("com.reyun.tracking.utils.IAttributionQueryListener")
- {
- }
- public void onComplete(int paramInt, String param_string) {
- Tracking.PostTask(() =>
- {
- if (Tracking.Instance.attributionCalllback_private != null)
- {
- Tracking.Instance.attributionCalllback_private.Invoke(param_string, paramInt);
- }
- });
- }
-
- }
- private sealed class AndroidDeferredDeeplink : AndroidJavaProxy
- {
- public AndroidDeferredDeeplink() : base("com.reyun.tracking.utils.IDeepLinkListener")
- {
- }
- public void onComplete(bool isSuccess, String dpUrl, String dpPath)
- {
- Tracking.PostTask(() =>
- {
- if (Tracking.Instance.deferredDeeplinCallback_private != null)
- {
- string jsonString = "";
- if (isSuccess)
- {
- jsonString += "{\"dp_url\":\"" + (dpUrl != null? dpUrl : "") + "\",";
- jsonString += "\"dp_path\":\"" + (dpPath != null ? dpPath : "") + "\"}";
- }
- else
- {
- jsonString = "{}";
- }
- Tracking.Instance.deferredDeeplinCallback_private.Invoke(jsonString);
- }
- });
- }
- }
- #endif
- private static String DictionaryToJsonString (Dictionary<string, object> dictionary)
- {
- string result = null;
- try
- {
- if (!CheckDictionary(dictionary)) {
- return result;
- }
- result = MiniJSON.Json.Serialize(dictionary);
- }
- catch (Exception e)
- {
- Debug.LogError("ToJsonString error:" + e.Message);
- }
- return result;
- }
- private static bool CheckDictionary(Dictionary<string, object> dictionary)
- {
- if (dictionary == null || dictionary.Count == 0)
- {
- return false;
- }
- foreach (var entry in dictionary)
- {
- if (entry.Key == null)
- {
- return false;
- }
- if (entry.Value == null)
- {
- return false;
- }
- if (entry.Value is string stringvalue)
- {
- if (stringvalue == null || stringvalue.Length == 0)
- {
- return false;
- }
- }
- }
- return true;
- }
- //private static bool isNumber(object value)
- //{
- // if (value == null)
- // {
- // return false;
- // }
- // if (value is float || value is int
- // || value is uint
- // || value is long
- // || value is sbyte
- // || value is byte
- // || value is short
- // || value is ushort
- // || value is ulong
- // || value is double
- // || value is decimal)
- // {
- // return true;
- // }
- // return false;
- //}
- }
|