using System; using System.Collections.Generic; using ThinkingAnalytics.Utils; using UnityEngine; using UnityEngine.SceneManagement; namespace ThinkingAnalytics.Wrapper { public partial class ThinkingAnalyticsWrapper { public static MonoBehaviour sMono; private static IDynamicSuperProperties mDynamicSuperProperties; private static Dictionary mAutoTrackEventCallbacks = new Dictionary(); private static Dictionary> mAutoTrackProperties = new Dictionary>(); private static Dictionary mAutoTrackEventInfos = new Dictionary(); private static System.Random rnd = new System.Random(); private static string default_appId = null; // add Dictionary to Dictionary public static void AddDictionary(Dictionary originalDic, Dictionary subDic) { foreach (KeyValuePair kv in subDic) { originalDic[kv.Key] = kv.Value; } } private static string serilize(Dictionary data) { return TD_MiniJSON.Serialize(data, getTimeString); } public static void ShareInstance(ThinkingAnalyticsAPI.Token token, MonoBehaviour mono, bool initRequired = true) { sMono = mono; if (string.IsNullOrEmpty(default_appId)) default_appId = token.appid; if (initRequired) init(token); } public static void EnableLog(bool enable) { enableLog(enable); } public static void SetVersionInfo(string version) { setVersionInfo("Unity", version); } public static void Identify(string uniqueId, string appId) { identify(uniqueId, appId); } public static string GetDistinctId(string appId) { return getDistinctId(appId); } public static void Login(string accountId, string appId) { login(accountId, appId); } public static void Logout(string appId) { logout(appId); } public static void EnableAutoTrack(AUTO_TRACK_EVENTS events, Dictionary properties, string appId) { UpdateAutoTrackSceneInfos(events, appId); SetAutoTrackProperties(events, properties, appId); enableAutoTrack(events, properties, appId); if ((events & AUTO_TRACK_EVENTS.APP_SCENE_LOAD) != 0) { TrackSceneLoad(SceneManager.GetActiveScene(), appId); } } public static void EnableAutoTrack(AUTO_TRACK_EVENTS events, IAutoTrackEventCallback eventCallback, string appId) { UpdateAutoTrackSceneInfos(events, appId); mAutoTrackEventCallbacks[appId] = eventCallback; //mAutoTrackEventCallback = eventCallback; enableAutoTrack(events, eventCallback, appId); if ((events & AUTO_TRACK_EVENTS.APP_SCENE_LOAD) != 0) { TrackSceneLoad(SceneManager.GetActiveScene(), appId); } } private static string AUTO_TRACK_EVENTS_APP_SCENE_LOAD = "APP_SCENE_LOAD"; private static string AUTO_TRACK_EVENTS_APP_SCENE_UNLOAD = "APP_SCENE_UNLOAD"; public static void SetAutoTrackProperties(AUTO_TRACK_EVENTS events, Dictionary properties, string appId) { if ((events & AUTO_TRACK_EVENTS.APP_SCENE_LOAD) != 0) { if (mAutoTrackProperties.ContainsKey(AUTO_TRACK_EVENTS_APP_SCENE_LOAD)) { AddDictionary(mAutoTrackProperties[AUTO_TRACK_EVENTS_APP_SCENE_LOAD], properties); } else mAutoTrackProperties[AUTO_TRACK_EVENTS_APP_SCENE_LOAD] = properties; } if ((events & AUTO_TRACK_EVENTS.APP_SCENE_UNLOAD) != 0) { if (mAutoTrackProperties.ContainsKey(AUTO_TRACK_EVENTS_APP_SCENE_UNLOAD)) { AddDictionary(mAutoTrackProperties[AUTO_TRACK_EVENTS_APP_SCENE_UNLOAD], properties); } else mAutoTrackProperties[AUTO_TRACK_EVENTS_APP_SCENE_UNLOAD] = properties; } setAutoTrackProperties(events, properties, appId); } public static void TrackSceneLoad(Scene scene, string appId = "") { Dictionary properties = new Dictionary() { { "#scene_name", scene.name }, { "#scene_path", scene.path } }; if (mAutoTrackProperties.ContainsKey(AUTO_TRACK_EVENTS_APP_SCENE_LOAD)) { AddDictionary(properties, mAutoTrackProperties[AUTO_TRACK_EVENTS_APP_SCENE_LOAD]); } if (string.IsNullOrEmpty(appId)) { foreach (var kv in mAutoTrackEventInfos) { if (mAutoTrackEventCallbacks.ContainsKey(kv.Key)) { AddDictionary(properties, mAutoTrackEventCallbacks[kv.Key].AutoTrackEventCallback((int)AUTO_TRACK_EVENTS.APP_SCENE_LOAD, properties)); } if ((kv.Value & AUTO_TRACK_EVENTS.APP_SCENE_LOAD) != 0) { Track("ta_scene_loaded", properties, kv.Key); } if ((kv.Value & AUTO_TRACK_EVENTS.APP_SCENE_UNLOAD) != 0) { TimeEvent("ta_scene_unloaded", kv.Key); } } } else { if (mAutoTrackEventCallbacks.ContainsKey(appId)) { AddDictionary(properties, mAutoTrackEventCallbacks[appId].AutoTrackEventCallback((int)AUTO_TRACK_EVENTS.APP_SCENE_LOAD, properties)); } Track("ta_scene_loaded", properties, appId); TimeEvent("ta_scene_unloaded", appId); } } public static void TrackSceneUnload(Scene scene, string appId = "") { Dictionary properties = new Dictionary() { { "#scene_name", scene.name }, { "#scene_path", scene.path } }; if (mAutoTrackProperties.ContainsKey(AUTO_TRACK_EVENTS_APP_SCENE_UNLOAD)) { AddDictionary(properties, mAutoTrackProperties[AUTO_TRACK_EVENTS_APP_SCENE_UNLOAD]); } foreach (var kv in mAutoTrackEventInfos) { if (mAutoTrackEventCallbacks.ContainsKey(kv.Key)) { AddDictionary(properties, mAutoTrackEventCallbacks[kv.Key].AutoTrackEventCallback((int)AUTO_TRACK_EVENTS.APP_SCENE_UNLOAD, properties)); } if ((kv.Value & AUTO_TRACK_EVENTS.APP_SCENE_UNLOAD) != 0) { Track("ta_scene_unloaded", properties, kv.Key); } } } private static void UpdateAutoTrackSceneInfos(AUTO_TRACK_EVENTS events, string appId = "") { if (string.IsNullOrEmpty(appId)) appId = default_appId; mAutoTrackEventInfos[appId] = events; } private static Dictionary getFinalEventProperties(Dictionary properties) { TD_PropertiesChecker.CheckProperties(properties); if (null != mDynamicSuperProperties) { Dictionary finalProperties = new Dictionary(); TD_PropertiesChecker.MergeProperties(mDynamicSuperProperties.GetDynamicSuperProperties(), finalProperties); TD_PropertiesChecker.MergeProperties(properties, finalProperties); return finalProperties; } else { return properties; } } public static void Track(string eventName, Dictionary properties, string appId) { TD_PropertiesChecker.CheckString(eventName); track(eventName, getFinalEventProperties(properties), appId); } public static void Track(string eventName, Dictionary properties, DateTime datetime, string appId) { TD_PropertiesChecker.CheckString(eventName); track(eventName, getFinalEventProperties(properties), datetime, appId); } public static void Track(string eventName, Dictionary properties, DateTime datetime, TimeZoneInfo timeZone, string appId) { TD_PropertiesChecker.CheckString(eventName); track(eventName, getFinalEventProperties(properties), datetime, timeZone, appId); } public static void TrackForAll(string eventName, Dictionary properties) { TD_PropertiesChecker.CheckString(eventName); trackForAll(eventName, getFinalEventProperties(properties)); } public static void Track(ThinkingAnalyticsEvent taEvent, string appId) { if (null == taEvent || null == taEvent.EventType) { if(TD_Log.GetEnable()) TD_Log.w("Ignoring invalid TA event"); return; } if (taEvent.EventTime == null) { if(TD_Log.GetEnable()) TD_Log.w("ppp null..."); } TD_PropertiesChecker.CheckString(taEvent.EventName); TD_PropertiesChecker.CheckProperties(taEvent.Properties); track(taEvent, appId); } public static void QuickTrack(string eventName, Dictionary properties, string appId) { if ("SceneView" == eventName) { if (properties == null) { properties = new Dictionary() { }; } Scene scene = SceneManager.GetActiveScene(); if (scene != null) { properties.Add("#scene_name", scene.name); properties.Add("#scene_path", scene.path); } Track("ta_scene_view", properties, appId); } else if ("AppClick" == eventName) { if (properties == null) { properties = new Dictionary() { }; } Track("ta_app_click", properties, appId); } } public static void SetSuperProperties(Dictionary superProperties, string appId) { TD_PropertiesChecker.CheckProperties(superProperties); setSuperProperties(superProperties, appId); } public static void UnsetSuperProperty(string superPropertyName, string appId) { TD_PropertiesChecker.CheckString(superPropertyName); unsetSuperProperty(superPropertyName, appId); } public static void ClearSuperProperty(string appId) { clearSuperProperty(appId); } public static void TimeEvent(string eventName, string appId) { TD_PropertiesChecker.CheckString(eventName); timeEvent(eventName, appId); } public static void TimeEventForAll(string eventName) { TD_PropertiesChecker.CheckString(eventName); timeEventForAll(eventName); } public static Dictionary GetSuperProperties(string appId) { return getSuperProperties(appId); } public static Dictionary GetPresetProperties(string appId) { return getPresetProperties(appId); } public static void UserSet(Dictionary properties, string appId) { TD_PropertiesChecker.CheckProperties(properties); userSet(properties, appId); } public static void UserSet(Dictionary properties, DateTime dateTime, string appId) { TD_PropertiesChecker.CheckProperties(properties); userSet(properties, dateTime, appId); } public static void UserSetOnce(Dictionary properties, string appId) { TD_PropertiesChecker.CheckProperties(properties); userSetOnce(properties, appId); } public static void UserSetOnce(Dictionary properties, DateTime dateTime, string appId) { TD_PropertiesChecker.CheckProperties(properties); userSetOnce(properties, dateTime, appId); } public static void UserUnset(List properties, string appId) { TD_PropertiesChecker.CheckProperties(properties); userUnset(properties, appId); } public static void UserUnset(List properties, DateTime dateTime, string appId) { TD_PropertiesChecker.CheckProperties(properties); userUnset(properties, dateTime, appId); } public static void UserAdd(Dictionary properties, string appId) { TD_PropertiesChecker.CheckProperties(properties); userAdd(properties, appId); } public static void UserAdd(Dictionary properties, DateTime dateTime, string appId) { TD_PropertiesChecker.CheckProperties(properties); userAdd(properties, dateTime, appId); } public static void UserAppend(Dictionary properties, string appId) { TD_PropertiesChecker.CheckProperties(properties); userAppend(properties, appId); } public static void UserAppend(Dictionary properties, DateTime dateTime, string appId) { TD_PropertiesChecker.CheckProperties(properties); userAppend(properties, dateTime, appId); } public static void UserUniqAppend(Dictionary properties, string appId) { TD_PropertiesChecker.CheckProperties(properties); userUniqAppend(properties, appId); } public static void UserUniqAppend(Dictionary properties, DateTime dateTime, string appId) { TD_PropertiesChecker.CheckProperties(properties); userUniqAppend(properties, dateTime, appId); } public static void UserDelete(string appId) { userDelete(appId); } public static void UserDelete(DateTime dateTime, string appId) { userDelete(dateTime, appId); } public static void Flush(string appId) { flush(appId); } public static void SetNetworkType(ThinkingAnalyticsAPI.NetworkType networkType) { setNetworkType(networkType); } public static string GetDeviceId() { return getDeviceId(); } public static void SetDynamicSuperProperties(IDynamicSuperProperties dynamicSuperProperties, string appId) { if (!TD_PropertiesChecker.CheckProperties(dynamicSuperProperties.GetDynamicSuperProperties())) { if(TD_Log.GetEnable()) TD_Log.d("TA.Wrapper(" + appId + ") - Cannot set dynamic super properties due to invalid properties."); } mDynamicSuperProperties = dynamicSuperProperties; setDynamicSuperProperties(dynamicSuperProperties, appId); } public static void SetTrackStatus(TA_TRACK_STATUS status, string appId) { setTrackStatus(status, appId); } public static void OptOutTracking(string appId) { optOutTracking(appId); } public static void OptOutTrackingAndDeleteUser(string appId) { optOutTrackingAndDeleteUser(appId); } public static void OptInTracking(string appId) { optInTracking(appId); } public static void EnableTracking(bool enabled, string appId) { enableTracking(enabled, appId); } public static string CreateLightInstance() { return createLightInstance(); } public static void CalibrateTime(long timestamp) { calibrateTime(timestamp); } public static void CalibrateTimeWithNtp(string ntpServer) { calibrateTimeWithNtp(ntpServer); } public static void EnableThirdPartySharing(TAThirdPartyShareType shareType, Dictionary properties = null, string appId = "") { if (null == properties) properties = new Dictionary(); enableThirdPartySharing(shareType, properties, appId); } } }