using System.Collections.Generic; using UnityEngine; using TapTap.Common; namespace TapTap.TapDB.Standalone.Internal { public class Tracker { private string clientId; private string channel; private string gameVersion; private Dictionary customProps; private Dictionary basicProps; private Dictionary staticProps = new Dictionary(); private IDynamicProperties dynamicPropsDelegate; private HttpClient httpClient; public void Init(string clientId, string channel, string gameVersion, TapDBRegion region, Dictionary properties) { this.clientId = clientId; this.channel = channel; this.gameVersion = gameVersion; this.customProps = properties; if(region == TapDBRegion.CN) { this.httpClient = new HttpClient(Constants.SERVER_URL_BJ, Constants.API_VERSION); } else if (region == TapDBRegion.IO) { this.httpClient = new HttpClient(Constants.SERVER_URL_SG, Constants.API_VERSION); } else { this.httpClient = new HttpClient(Constants.SERVER_URL_BJ, Constants.API_VERSION); } InitBasicProps(); Dictionary props = new Dictionary(basicProps); TapDBStandalone.Tracker.TrackEvent(Constants.EVENT, "device_login", props); } public void RegisterStaticProps(Dictionary properties) { foreach (KeyValuePair kv in properties) { staticProps[kv.Key] = kv.Value; } } public void UnregisterStaticProp(string propertyKey) { if (string.IsNullOrWhiteSpace(propertyKey)) { TapDBStandalone.Logger.Error("Property key is NULL"); } staticProps.Remove(propertyKey); } public void ClearStaticProps() { staticProps.Clear(); } public void RegisterDynamicPropsDelegate(IDynamicProperties dynamicPropsDelegate) { this.dynamicPropsDelegate = dynamicPropsDelegate; } /// /// 上报事件 /// /// /// /// public void TrackEvent(string path, string name, Dictionary properties = null) { Dictionary props = new Dictionary(basicProps); if (staticProps != null) { foreach (KeyValuePair kv in staticProps) { props[kv.Key] = kv.Value; } } Dictionary dynamicProps = dynamicPropsDelegate?.GetDynamicProperties(); if (dynamicProps != null) { foreach (KeyValuePair kv in dynamicProps) { props[kv.Key] = kv.Value; } } if (customProps != null) { foreach (KeyValuePair kv in customProps) { props[kv.Key] = kv.Value; } } if (properties != null) { foreach (KeyValuePair kv in properties) { props[kv.Key] = kv.Value; } } Dictionary data = new Dictionary { { "client_id", clientId }, { "type", "track" }, { "name", name }, { "device_id", TapDBStandalone.Identity.DeviceId }, { "properties", props } }; if (!string.IsNullOrWhiteSpace(TapDBStandalone.User.Id)) { data["user_id"] = TapDBStandalone.User.Id; } _ = httpClient.Post(path, data: data); } /// /// 上报设备属性变化 /// /// /// public void TrackDeviceProperties(string type, Dictionary properties) { if (string.IsNullOrWhiteSpace(TapDBStandalone.Identity.DeviceId)) { TapDBStandalone.Logger.Error("DeviceId is NULL."); return; } Dictionary baseProps = new Dictionary { { "device_id", TapDBStandalone.Identity.DeviceId } }; TrackProperties(type, baseProps, properties); } /// /// 上报玩家属性变化 /// public void TrackUserProperties(string type, Dictionary properties) { string userId = TapDBStandalone.User.Id; if (string.IsNullOrWhiteSpace(userId)) { TapDBStandalone.Logger.Error("UserId is NULL."); return; } Dictionary baseProps = new Dictionary { { "user_id", userId } }; TrackProperties(type, baseProps, properties); } private void TrackProperties(string type, Dictionary basicProps, Dictionary properties) { if (!IsInitialized) { return; } if (properties == null) { properties = new Dictionary(); } properties["sdk_version"] = TapCommon.SDKVersion; Dictionary data = new Dictionary(basicProps) { { "client_id", clientId }, { "type", type }, { "properties", properties } }; _ = httpClient.Post(Constants.EVENT, data: data); } private void InitBasicProps() { DeviceInfo.GetMacAddress(out string macList, out string firstMac); basicProps = new Dictionary { { "os", OS }, { "device_model", SystemInfo.deviceModel }, { "os_version", SystemInfo.operatingSystem }, { "install_uuid", TapDBStandalone.Identity.InstallationId }, { "persist_uuid", TapDBStandalone.Identity.PersistentId }, { "width", Screen.currentResolution.width }, { "height", Screen.currentResolution.height }, { "provider", "unknown" }, { "app_version", gameVersion }, { "sdk_version", TapCommon.SDKVersion }, { "network", Network }, { "channel", channel }, { "lang_system", DeviceInfo.GetLanguage() }, { "mac_list", macList }, { "first_mac", firstMac }, { "device_id5", DeviceInfo.GetLaunchUniqueID() } }; } private string OS { get { switch (SystemInfo.operatingSystemFamily) { case OperatingSystemFamily.Windows: return "Windows"; case OperatingSystemFamily.MacOSX: return "Mac"; case OperatingSystemFamily.Linux: return "Linux"; default: return "Unknown"; } } } private string Network { get { switch (Application.internetReachability) { case NetworkReachability.ReachableViaCarrierDataNetwork: return "3"; case NetworkReachability.ReachableViaLocalAreaNetwork: return "2"; default: return "Unknown"; } } } internal bool IsInitialized { get { if (string.IsNullOrWhiteSpace(clientId)) { TapDBStandalone.Logger.Error("MUST be initialized."); return false; } return true; } } } }