using System; using System.Collections.Generic; using Firebase.Analytics; using Firebase.RemoteConfig; using Newtonsoft.Json; using WZ; namespace WZ { /// /// Firebase analytics事件上报 /// public class FireBaseAnalyticsManager : D_MonoSingleton { private const string KEY_SUPER_PROPERTIES = "KEY_SUPER_PROPERTIES"; private Dictionary superProperties = new Dictionary(); public void InitSuperProperties() { string json = PlayerPrefsUtils.GetPlayerPrefsString(KEY_SUPER_PROPERTIES, ""); if (!string.IsNullOrEmpty(json)) { superProperties = JsonConvert.DeserializeObject>(json); } } public void LogEvent(string eventName) { if (!FireBaseRemoteConfigManager.Instance.IsInitialized) return; var superProperties = GetSuperProperties(); if (superProperties.Count > 0) { FirebaseAnalytics.LogEvent(eventName, superProperties.ToArray()); } else { FirebaseAnalytics.LogEvent(eventName); } } public void LogEvent(string eventName, string key1, object value1) { if (!FireBaseRemoteConfigManager.Instance.IsInitialized) return; List parameters = new List(); parameters.Add(GetParameter(key1, value1)); foreach (var superProperty in GetSuperProperties()) { parameters.Add(superProperty); } FirebaseAnalytics.LogEvent(eventName, parameters.ToArray()); } public void LogEvent(string eventName, Dictionary dict) { if (!FireBaseRemoteConfigManager.Instance.IsInitialized) return; List parameters = new List(); foreach (var item in dict) { Parameter parameter = GetParameter(item.Key, item.Value); parameters.Add(parameter); } foreach (var superProperty in GetSuperProperties()) { parameters.Add(superProperty); } FirebaseAnalytics.LogEvent(eventName, parameters.ToArray()); } public void LogEvent(string eventName, Dictionary dict) { if (!FireBaseRemoteConfigManager.Instance.IsInitialized) return; List parameters = new List(); foreach (var item in dict) { Parameter parameter = new Parameter(item.Key, item.Value); parameters.Add(parameter); } foreach (var superProperty in GetSuperProperties()) { parameters.Add(superProperty); } FirebaseAnalytics.LogEvent(eventName, parameters.ToArray()); } /// /// 设置用户属性 /// /// /// public void SetUserProperty(string name, string property) { FirebaseAnalytics.SetUserProperty(name, property); } /// /// 设置用户属性 /// /// /// public void SetUserProperty(Dictionary propertys) { foreach (var property in propertys) { SetUserProperty(property.Key, property.Value.ToString()); } } /// /// 设置公共事件属性 /// /// public void SetSuperProperties(Dictionary propertys) { if (superProperties == null) { superProperties = new Dictionary(); } foreach (var property in propertys) { superProperties[property.Key] = property.Value; } string json = JsonConvert.SerializeObject(superProperties); PlayerPrefsUtils.SavePlayerPrefsString(KEY_SUPER_PROPERTIES, json); } /// /// 获取公共事件属性,每次上报都带上 /// /// private List GetSuperProperties() { List parameterList = new List(); parameterList.Add(new Parameter("rush_version", RushSDKManager.GetSDKVersion())); if (superProperties != null) { foreach (var superProperty in superProperties) { Parameter parameter = GetParameter(superProperty.Key, superProperty.Value); parameterList.Add(parameter); } } return parameterList; } /// /// FireBase收益上报 /// /// 聚合平台名称 /// 广告平台名称 /// 广告位ID /// 广告格式 /// 单次展示收益 /// 广告展示点位 /// 第几次展示该格式的广告 public void OnAdRevenueEvent(string ad_platform, string ad_source, string ad_unit_name, AdsType ad_format, double revenue, string position, int number) { if (!FireBaseRemoteConfigManager.Instance.IsInitialized) return; var impressionParameters = new[] { new Firebase.Analytics.Parameter("ad_platform", ad_platform), new Firebase.Analytics.Parameter("ad_source", ad_source), new Firebase.Analytics.Parameter("ad_unit_name",ad_unit_name), new Firebase.Analytics.Parameter("ad_format", ad_format.ToString()), new Firebase.Analytics.Parameter("value", revenue), new Firebase.Analytics.Parameter("currency", "USD"), // All AppLovin revenue is sent in USD new Firebase.Analytics.Parameter("position", position), new Firebase.Analytics.Parameter("number", number), }; string name = GetName(); Firebase.Analytics.FirebaseAnalytics.LogEvent(name, impressionParameters); AdsKeyEvents.Instance.LogAdCountEvents(ad_format, revenue); AdsKeyEvents.Instance.LogAdTotalRevenueEvents(ad_format, revenue); } /// /// 收益分离 /// /// private string GetName() { string name = "ad_impression"; //获取在线参数 string json = FireBaseRemoteConfigManager.Instance.GetRemoteConfigString("revenue_fir"); if (string.IsNullOrEmpty(json)) { return name; } var revenueAdjs = JsonConvert.DeserializeObject(json); if (revenueAdjs == null && revenueAdjs.Length == 0) { return name; } int totalRate = 0; //获取全部概率 foreach (var item in revenueAdjs) { totalRate += item.rate; } //开始随机 int randomValue = UnityEngine.Random.Range(0, totalRate); int accumulatedRate = 0; //根据随机值定位 foreach (var item in revenueAdjs) { accumulatedRate += item.rate; if (randomValue < accumulatedRate) { return item.name; } } return name; } private Parameter GetParameter(string key, object value) { Type type = value.GetType(); // 根据类型进行转换 if (type == typeof(string)) { return new Parameter(key, value as string); } if (type == typeof(int)) { return new Parameter(key, (int)value); } if (type == typeof(float)) { return new Parameter(key, (float)value); } if (type == typeof(long)) { return new Parameter(key, (long)value); } if (type == typeof(double)) { return new Parameter(key, (double)value); } return new Parameter(key, value as string); } } }