/* * Copyright 2019, ThinkingData, Inc Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. SDK VERSION:2.6.1 */ #if !(UNITY_5_4_OR_NEWER) #define DISABLE_TA #warning "Your Unity version is not supported by us - ThinkingAnalyticsSDK disabled" #endif using System; using System.Collections.Generic; using ThinkingAnalytics.Utils; using ThinkingAnalytics.Wrapper; using UnityEngine; using ThinkingAnalytics.TAException; using UnityEngine.SceneManagement; namespace ThinkingAnalytics { [DisallowMultipleComponent] public class ThinkingAnalyticsAPI : MonoBehaviour { #region settings [System.Serializable] public struct Token { public string appid; public string serverUrl; public TAMode mode; public TATimeZone timeZone; public string timeZoneId; public bool enableEncrypt; // enable data encryption, default is false (iOS/Android only) public int encryptVersion; // secret key version (iOS/Android only) public string encryptPublicKey; // public secret key (iOS/Android only) public SSLPinningMode pinningMode; // SSL Pinning mode, default is NONE (iOS/Android only) public bool allowInvalidCertificates; // allow invalid certificates, default is false (iOS/Android only) public bool validatesDomainName; // enable to verify domain name, default is true (iOS/Android only) private string instanceName; // instances name public Token(string appId, string serverUrl, TAMode mode = TAMode.NORMAL, TATimeZone timeZone = TATimeZone.Local, string timeZoneId = null, string instanceName = null) { this.appid = appId.Replace(" ", ""); this.serverUrl = serverUrl; this.mode = mode; this.timeZone = timeZone; this.timeZoneId = timeZoneId; this.enableEncrypt = false; this.encryptVersion = 0; this.encryptPublicKey = null; this.pinningMode = SSLPinningMode.NONE; this.allowInvalidCertificates = false; this.validatesDomainName = true; if (!string.IsNullOrEmpty(instanceName)) { instanceName = instanceName.Replace(" ", ""); } this.instanceName = instanceName; } public string GetInstanceName() { return this.instanceName; } public string getTimeZoneId() { #if UNITY_STANDALONE_WIN switch (timeZone) { case TATimeZone.UTC: return "UTC"; case TATimeZone.Asia_Shanghai: return "China Standard Time"; case TATimeZone.Asia_Tokyo: return "Tokyo Standard Time"; case TATimeZone.America_Los_Angeles: return "Pacific Standard Time"; case TATimeZone.America_New_York: return "Eastern Standard Time"; case TATimeZone.Other: return timeZoneId; default: break; } #else switch (timeZone) { case TATimeZone.UTC: return "UTC"; case TATimeZone.Asia_Shanghai: return "Asia/Shanghai"; case TATimeZone.Asia_Tokyo: return "Asia/Tokyo"; case TATimeZone.America_Los_Angeles: return "America/Los_Angeles"; case TATimeZone.America_New_York: return "America/New_York"; case TATimeZone.Other: return timeZoneId; default: break; } #endif return null; } } public enum TATimeZone { Local, UTC, Asia_Shanghai, Asia_Tokyo, America_Los_Angeles, America_New_York, Other = 100 } public enum TAMode { NORMAL = 0, DEBUG = 1, DEBUG_ONLY = 2 } public enum NetworkType { DEFAULT = 1, WIFI = 2, ALL = 3 } [Header("Configuration")] [Tooltip("Enable Start SDK Manually")] public bool startManually = true; [Tooltip("Enable Log")] public bool enableLog = true; [Tooltip("Sets the Network Type")] public NetworkType networkType = NetworkType.DEFAULT; [Header("Project")] [Tooltip("Project Setting, APP ID is given when the project is created")] [HideInInspector] public Token[] tokens = new Token[1]; #endregion /// /// Whether to enable logs /// /// enable logs public static void EnableLog(bool enable, string appId = "") { if (sThinkingAnalyticsAPI != null) { sThinkingAnalyticsAPI.enableLog = enable; TD_Log.EnableLog(enable); ThinkingAnalyticsWrapper.EnableLog(enable); } } /// /// Set custom distinct ID, to replace the distinct ID generated by the system /// /// distinct ID /// project ID (optional) public static void Identify(string distinctId, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.Identify(distinctId, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { distinctId, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Returns the current distinct ID /// /// distinct ID /// project ID (optional) public static string GetDistinctId(string appId = "") { if (tracking_enabled) { return ThinkingAnalyticsWrapper.GetDistinctId(appId); } return null; } /// /// Set account ID. This method does not upload Login events /// /// account ID /// project ID (optional) public static void Login(string account, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.Login(account, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { account, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Clear account ID. This method does not upload Logout events /// /// project ID (optional) public static void Logout(string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.Logout(appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Enable auto-tracking /// /// auto-tracking events /// properties for auto-tracking events (optional) /// project ID (optional) public static void EnableAutoTrack(AUTO_TRACK_EVENTS events, Dictionary properties = null, string appId = "") { if (tracking_enabled) { if (properties == null) { properties = new Dictionary(); } ThinkingAnalyticsWrapper.EnableAutoTrack(events, properties, appId); if ((events & AUTO_TRACK_EVENTS.APP_CRASH) != 0 && !TD_PublicConfig.DisableCSharpException) { ThinkingSDKExceptionHandler.RegisterTAExceptionHandler(properties); } if ((events & AUTO_TRACK_EVENTS.APP_SCENE_LOAD) != 0) { SceneManager.sceneLoaded -= ThinkingAnalyticsAPI.OnSceneLoaded; SceneManager.sceneLoaded += ThinkingAnalyticsAPI.OnSceneLoaded; } if ((events & AUTO_TRACK_EVENTS.APP_SCENE_UNLOAD) != 0) { SceneManager.sceneUnloaded -= ThinkingAnalyticsAPI.OnSceneUnloaded; SceneManager.sceneUnloaded += ThinkingAnalyticsAPI.OnSceneUnloaded; } } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { events, properties, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Enable auto-tracking /// /// auto-tracking events /// callback for auto-tracking events (optional) /// project ID (optional) public static void EnableAutoTrack(AUTO_TRACK_EVENTS events, IAutoTrackEventCallback eventCallback, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.EnableAutoTrack(events, eventCallback, appId); if ((events & AUTO_TRACK_EVENTS.APP_CRASH) != 0 && !TD_PublicConfig.DisableCSharpException) { ThinkingSDKExceptionHandler.RegisterTAExceptionHandler(eventCallback); } if ((events & AUTO_TRACK_EVENTS.APP_SCENE_LOAD) != 0) { SceneManager.sceneLoaded -= ThinkingAnalyticsAPI.OnSceneLoaded; SceneManager.sceneLoaded += ThinkingAnalyticsAPI.OnSceneLoaded; } if ((events & AUTO_TRACK_EVENTS.APP_SCENE_UNLOAD) != 0) { SceneManager.sceneUnloaded -= ThinkingAnalyticsAPI.OnSceneUnloaded; SceneManager.sceneUnloaded += ThinkingAnalyticsAPI.OnSceneUnloaded; } } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { events, eventCallback, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Set properties for auto-tracking events /// /// auto-tracking events /// properties for auto-tracking events /// project ID (optional) public static void SetAutoTrackProperties(AUTO_TRACK_EVENTS events, Dictionary properties, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.SetAutoTrackProperties(events, properties, appId); if ((events & AUTO_TRACK_EVENTS.APP_CRASH) != 0 && !TD_PublicConfig.DisableCSharpException) { ThinkingSDKExceptionHandler.SetAutoTrackProperties(properties); } } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { events, properties, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Track a Event /// /// event name /// project ID (optional) public static void Track(string eventName, string appId = "") { Track(eventName, null, appId); } /// /// Track a Event /// /// the event name /// properties for the event /// project ID (optional) public static void Track(string eventName, Dictionary properties, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.Track(eventName, properties, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { eventName, properties, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Track a Event /// /// the event name /// properties for the event /// date for the event /// project ID (optional) [Obsolete("Method is deprecated, please use Track(string eventName, Dictionary properties, DateTime date, TimeZoneInfo timeZone, string appId = \"\") instead.")] public static void Track(string eventName, Dictionary properties, DateTime date, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.Track(eventName, properties, date, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { eventName, properties, date, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Track a Event /// /// the event name /// properties for the event /// date for the event /// time zone for the event /// project ID (optional) public static void Track(string eventName, Dictionary properties, DateTime date, TimeZoneInfo timeZone, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.Track(eventName, properties, date, timeZone, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { eventName, properties, date, timeZone, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Track a Special Event (First Event/Updatable Event/Overwritable Event) /// /// the special event /// project ID (optional) public static void Track(ThinkingAnalyticsEvent analyticsEvent, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.Track(analyticsEvent, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { analyticsEvent, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Quickly track a Special Event /// /// the event name, 'SceneView' for scene view event, 'AppClick' for click event /// event properties /// public static void QuickTrack(string eventName, Dictionary properties = null, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.QuickTrack(eventName, properties, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { eventName, properties, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Report events data to TE server immediately /// /// project ID (optional) public static void Flush(string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.Flush(appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Scenes load Delegate /// /// the load scene /// the scene loading mode public static void OnSceneLoaded(Scene scene, LoadSceneMode mode) { if (tracking_enabled) { ThinkingAnalyticsWrapper.TrackSceneLoad(scene); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { scene, mode }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Scenes unload Delegate /// /// the unload scene public static void OnSceneUnloaded(Scene scene) { if (tracking_enabled) { ThinkingAnalyticsWrapper.TrackSceneUnload(scene); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { scene }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Super Properties, refer to properties that would be uploaded by each event /// /// super properties for events /// project ID (optional) public static void SetSuperProperties(Dictionary superProperties, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.SetSuperProperties(superProperties, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { superProperties, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Delete Property form current Super Properties /// /// property name /// project ID (optional) public static void UnsetSuperProperty(string property, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UnsetSuperProperty(property, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { property, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Returns current Super Properties /// /// current super properties /// project ID (optional) public static Dictionary GetSuperProperties(string appId = "") { if (tracking_enabled) { return ThinkingAnalyticsWrapper.GetSuperProperties(appId); } return null; } /// /// Clear current Super Properties /// /// project ID (optional) public static void ClearSuperProperties(string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.ClearSuperProperty(appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Returns current Preset Properties /// /// current preset properties /// project ID (optional) public static TDPresetProperties GetPresetProperties(string appId = "") { if (tracking_enabled) { Dictionary properties = ThinkingAnalyticsWrapper.GetPresetProperties(appId); TDPresetProperties presetProperties = new TDPresetProperties(properties); return presetProperties; } return null; } /// /// Sets the Dynamic Super Properties. /// /// dynamic super properties interface /// project ID (optional) public static void SetDynamicSuperProperties(IDynamicSuperProperties dynamicSuperProperties, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.SetDynamicSuperProperties(dynamicSuperProperties, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { dynamicSuperProperties, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Records Event Duration, call TimeEvent to start timing for the Event, call Track to end timing /// /// the event name /// project ID (optional) public static void TimeEvent(string eventName, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.TimeEvent(eventName, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { eventName, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Sets User Properties, this will overwrite the original properties value /// /// user properties /// project ID (optional) public static void UserSet(Dictionary properties, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserSet(properties, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { properties, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Sets User Properties, this will overwrite the original properties value /// /// user properties /// date time /// project ID (optional) public static void UserSet(Dictionary properties, DateTime dateTime, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserSet(properties, dateTime, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { properties , dateTime, appId}; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Unsets one of User Porperties, this would not create properties that have not been created in TE /// /// the user property name /// project ID (optional) public static void UserUnset(string property, string appId = "") { List properties = new List(); properties.Add(property); UserUnset(properties, appId); } /// /// Unsets some of User Porperties, this would not create properties that have not been created in TE /// /// the user properties name /// project ID (optional) public static void UserUnset(List properties, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserUnset(properties, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { properties, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Unsets some of User Porperties, this would not create properties that have not been created in TE /// /// the user properties name /// date time /// project ID (optional) public static void UserUnset(List properties, DateTime dateTime, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserUnset(properties, dateTime, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { properties, dateTime, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Sets User Properties for Once. This message would be neglected, if such property had been set before /// /// user properties /// project ID (optional) public static void UserSetOnce(Dictionary properties, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserSetOnce(properties, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { properties, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Sets User Properties for Once. The property would be neglected, if such property had been set before /// /// user properties /// date time /// project ID (optional) public static void UserSetOnce(Dictionary properties, DateTime dateTime, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserSetOnce(properties, dateTime, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { properties, dateTime,appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Accumulates the property. If the property has not been set, it would be given a value of 0 before computing. /// /// the property name /// value of the property /// project ID (optional) public static void UserAdd(string property, object value, string appId = "") { Dictionary properties = new Dictionary() { { property, value } }; UserAdd(properties, appId); } /// /// Accumulates the property. If the property has not been set, it would be given a value of 0 before computing. /// /// user properties /// project ID (optional) public static void UserAdd(Dictionary properties, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserAdd(properties, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { properties, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Accumulates the property, type of Number. If the property has not been set, it would be given a value of 0 before computing. /// /// user properties /// date time /// project ID (optional) public static void UserAdd(Dictionary properties, DateTime dateTime, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserAdd(properties, dateTime, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { properties, dateTime, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Appends the property, type of List. /// /// user properties /// project ID (optional) public static void UserAppend(Dictionary properties, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserAppend(properties, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { properties, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Appends the property, type of List. /// /// user properties /// date time /// project ID (optional) public static void UserAppend(Dictionary properties, DateTime dateTime, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserAppend(properties, dateTime, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { properties, dateTime, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Appends the property Uniquely, type of List. If the property has been set, it would be neglected /// /// user properties /// project ID (optional) public static void UserUniqAppend(Dictionary properties, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserUniqAppend(properties, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { properties, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Appends the property Uniquely, type of List. If the property has been set, it would be neglected /// /// user prpoerties /// date time /// project ID (optional) public static void UserUniqAppend(Dictionary properties, DateTime dateTime, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserUniqAppend(properties, dateTime, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { properties, dateTime, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Deletes All Properties for a user, the events triggered by the user are still exist /// /// project ID (optional) public static void UserDelete(string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserDelete(appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Deletes All Properties for a user, the events triggered by the user are still exist /// /// date time /// project ID (optional) public static void UserDelete(DateTime dateTime, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.UserDelete(dateTime, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { dateTime, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Sets Network Type for report date to TE /// /// network type, see NetworkType /// project ID (optional) public static void SetNetworkType(NetworkType networkType, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.SetNetworkType(networkType); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { networkType, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Gets the device identifier. /// /// The device identifier. public static string GetDeviceId() { if (tracking_enabled) { return ThinkingAnalyticsWrapper.GetDeviceId(); } return null; } /// /// Sets Data Report Status /// /// data report status, see TA_TRACK_STATUS /// project ID (optional) public static void SetTrackStatus(TA_TRACK_STATUS status, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.SetTrackStatus(status, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { status, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Stops Report Event Data, and Clear Cache Data (include unreported event data, custom distinct ID, account ID, Super Properties) /// /// project ID (optional) [Obsolete("Method is deprecated, please use SetTrackStatus() instead.")] public static void OptOutTracking(string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.OptOutTracking(appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Stops Report Event Data, and Clear Cache Data (include unreported event data, custom distinct ID, account ID, super properties), and Delete User /// /// project ID (optional) [Obsolete("Method is deprecated, please use SetTrackStatus() instead.")] public static void OptOutTrackingAndDeleteUser(string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.OptOutTrackingAndDeleteUser(appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Resumes Report Event Data /// /// project ID (optional) [Obsolete("Method is deprecated, please use SetTrackStatus() instead.")] public static void OptInTracking(string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.OptInTracking(appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Enable Report Event Data /// /// Whether to enable reported data /// project ID (optional) [Obsolete("Method is deprecated, please use SetTrackStatus() instead.")] public static void EnableTracking(bool enabled, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.EnableTracking(enabled, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { enabled, appId }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Creats Light Instance, it has same project ID to main instance, diffent distinct ID, account ID, super properties /// /// project ID (optional) /// light instance token public static string CreateLightInstance(string appId = "") { if (tracking_enabled) { return ThinkingAnalyticsWrapper.CreateLightInstance(); } return null; } /// /// Calibrate Event Time, calibrated times are used for events after that /// /// currnt Unix timestamp, units Ms public static void CalibrateTime(long timestamp) { ThinkingAnalyticsWrapper.CalibrateTime(timestamp); } /// /// Calibrate Event Time, calibrated times are used for events after that /// If NTP server is not returns in 3s, the time will not be re-calibrated /// /// NTP server, e.g 'time.asia.apple.com' public static void CalibrateTimeWithNtp(string ntpServer) { ThinkingAnalyticsWrapper.CalibrateTimeWithNtp(ntpServer); } /// /// Cross Platform /// Share TE account system info to other platforms /// /// type of platforms, see TAThirdPartyShareType /// properties of platforms /// project ID (optional) public static void EnableThirdPartySharing(TAThirdPartyShareType shareType, Dictionary properties = null, string appId = "") { if (tracking_enabled) { ThinkingAnalyticsWrapper.EnableThirdPartySharing(shareType, properties, appId); } else { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); object[] parameters = new object[] { shareType }; eventCaches.Add(new Dictionary() { { "method", method}, { "parameters", parameters} }); } } /// /// Gets the Local Country/Region Code /// the two-letter code defined in ISO 3166 for the country/region /// /// country/region code public static string GetLocalRegion() { return System.Globalization.RegionInfo.CurrentRegion.TwoLetterISORegionName; } /// /// Start Thinking Analytics SDK /// /// project ID /// project URL public static void StartThinkingAnalytics(string appId, string serverUrl) { ThinkingAnalyticsAPI.TAMode mode = ThinkingAnalyticsAPI.TAMode.NORMAL; ThinkingAnalyticsAPI.TATimeZone timeZone = ThinkingAnalyticsAPI.TATimeZone.Local; ThinkingAnalyticsAPI.Token token = new ThinkingAnalyticsAPI.Token(appId, serverUrl, mode, timeZone); ThinkingAnalyticsAPI.StartThinkingAnalytics(token); } /// /// Start Thinking Analytics SDK /// /// project setting, see ThinkingAnalyticsAPI.Token public static void StartThinkingAnalytics(ThinkingAnalyticsAPI.Token token) { ThinkingAnalyticsAPI.Token[] tokens = new ThinkingAnalyticsAPI.Token[1]; tokens[0] = token; ThinkingAnalyticsAPI.StartThinkingAnalytics(tokens); } /// /// Start Thinking Analytics SDK /// /// projects setting, see ThinkingAnalyticsAPI.Token public static void StartThinkingAnalytics(Token[] tokens = null) { #if DISABLE_TA tracking_enabled = false; #else tracking_enabled = true; #endif if (tracking_enabled) { TD_PublicConfig.GetPublicConfig(); TD_Log.EnableLog(sThinkingAnalyticsAPI.enableLog); ThinkingAnalyticsWrapper.EnableLog(sThinkingAnalyticsAPI.enableLog); ThinkingAnalyticsWrapper.SetVersionInfo(TD_PublicConfig.LIB_VERSION); if (tokens == null) { tokens = sThinkingAnalyticsAPI.tokens; } try { for (int i = 0; i < tokens.Length; i++) { Token token = tokens[i]; if (!string.IsNullOrEmpty(token.appid)) { token.appid = token.appid.Replace(" ", ""); if(TD_Log.GetEnable()) TD_Log.d("ThinkingAnalytics start with APPID: " + token.appid + ", SERVERURL: " + token.serverUrl + ", MODE: " + token.mode); ThinkingAnalyticsWrapper.ShareInstance(token, sThinkingAnalyticsAPI); ThinkingAnalyticsWrapper.SetNetworkType(sThinkingAnalyticsAPI.networkType); } } } catch (Exception ex) { if(TD_Log.GetEnable()) TD_Log.d("ThinkingAnalytics start Error: " + ex.Message); } } FlushEventCaches(); } #region internal private static void FlushEventCaches() { List> tmpEventCaches = new List>(eventCaches); eventCaches.Clear(); foreach (Dictionary eventCache in tmpEventCaches) { if (eventCache.ContainsKey("method") && eventCache.ContainsKey("parameters")) { System.Reflection.MethodBase method = (System.Reflection.MethodBase)eventCache["method"]; object[] parameters = (object[])eventCache["parameters"]; method.Invoke(null, parameters); } } } private void Awake() { if (sThinkingAnalyticsAPI == null) { sThinkingAnalyticsAPI = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); return; } if (this.startManually == false) { ThinkingAnalyticsAPI.StartThinkingAnalytics(); } } private void Start() { } private void OnApplicationQuit() { //Scene scene = SceneManager.GetActiveScene(); //if (scene != null) //{ // OnSceneUnloaded(scene); //} } private static ThinkingAnalyticsAPI sThinkingAnalyticsAPI; private static bool tracking_enabled = false; private static List> eventCaches = new List>(); #endregion } }