261 lines
		
	
	
		
			8.9 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			261 lines
		
	
	
		
			8.9 KiB
		
	
	
	
		
			C#
		
	
	
	
| 
 | |
| using System;
 | |
| using System.Collections.Generic;
 | |
| using Firebase.Analytics;
 | |
| using Firebase.RemoteConfig;
 | |
| using Newtonsoft.Json;
 | |
| using WZ;
 | |
| 
 | |
| namespace WZ
 | |
| { 
 | |
|     /// <summary>
 | |
|     /// Firebase analytics事件上报
 | |
|     /// </summary>
 | |
|     public class FireBaseAnalyticsManager : D_MonoSingleton<FireBaseAnalyticsManager>
 | |
|     {
 | |
|         
 | |
|         private const string KEY_SUPER_PROPERTIES = "KEY_SUPER_PROPERTIES";
 | |
|         
 | |
|         private Dictionary<string, object> superProperties = new Dictionary<string, object>();
 | |
| 
 | |
|         public void InitSuperProperties()
 | |
|         {
 | |
|             string json = PlayerPrefsUtils.GetPlayerPrefsString(KEY_SUPER_PROPERTIES, "");
 | |
|             if (!string.IsNullOrEmpty(json))
 | |
|             {
 | |
|                 superProperties = JsonConvert.DeserializeObject<Dictionary<string, object>>(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<Parameter> parameters = new List<Parameter>();
 | |
|             parameters.Add(GetParameter(key1, value1));
 | |
|             
 | |
|             foreach (var superProperty in GetSuperProperties())
 | |
|             {
 | |
|                 parameters.Add(superProperty);
 | |
|             }
 | |
|             
 | |
|             FirebaseAnalytics.LogEvent(eventName, parameters.ToArray());
 | |
|         }
 | |
| 
 | |
|         public void LogEvent(string eventName, Dictionary<string, object> dict)
 | |
|         {
 | |
|             if (!FireBaseRemoteConfigManager.Instance.IsInitialized) return;
 | |
|             List<Parameter> parameters = new List<Parameter>();
 | |
|             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<string, string> dict)
 | |
|         {
 | |
|             if (!FireBaseRemoteConfigManager.Instance.IsInitialized) return;
 | |
|             List<Parameter> parameters = new List<Parameter>();
 | |
|             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());
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 设置用户属性
 | |
|         /// </summary>
 | |
|         /// <param name="name"></param>
 | |
|         /// <param name="property"></param>
 | |
|         public void SetUserProperty(string name, string property)
 | |
|         {
 | |
|             FirebaseAnalytics.SetUserProperty(name, property);
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 设置用户属性
 | |
|         /// </summary>
 | |
|         /// <param name="name"></param>
 | |
|         /// <param name="property"></param>
 | |
|         public void SetUserProperty(Dictionary<string, object> propertys)
 | |
|         {
 | |
|             foreach (var property in propertys)
 | |
|             {
 | |
|                 SetUserProperty(property.Key, property.Value.ToString());
 | |
|             }
 | |
|            
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 设置公共事件属性
 | |
|         /// </summary>
 | |
|         /// <param name="propertys"></param>
 | |
|         public void SetSuperProperties(Dictionary<string, object> propertys)
 | |
|         {
 | |
|             if (superProperties == null)
 | |
|             {
 | |
|                 superProperties = new Dictionary<string, object>();
 | |
|             }
 | |
| 
 | |
|             foreach (var property in propertys)
 | |
|             {
 | |
|                 superProperties[property.Key] = property.Value;
 | |
|             }
 | |
|             
 | |
|             string json = JsonConvert.SerializeObject(superProperties);
 | |
|             PlayerPrefsUtils.SavePlayerPrefsString(KEY_SUPER_PROPERTIES, json);
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 获取公共事件属性,每次上报都带上
 | |
|         /// </summary>
 | |
|         /// <returns></returns>
 | |
|         private List<Parameter> GetSuperProperties()
 | |
|         {
 | |
|             List<Parameter> parameterList = new List<Parameter>();
 | |
|             if (superProperties != null)
 | |
|             {
 | |
|                 foreach (var superProperty in superProperties)
 | |
|                 {
 | |
|                     Parameter parameter = GetParameter(superProperty.Key, superProperty.Value);
 | |
|                     parameterList.Add(parameter);
 | |
|                 }
 | |
|             }
 | |
| 
 | |
|             return parameterList;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// FireBase收益上报
 | |
|         /// </summary>
 | |
|         /// <param name="ad_platform">聚合平台名称</param>
 | |
|         /// <param name="ad_source">广告平台名称</param>
 | |
|         /// <param name="ad_unit_name">广告位ID</param>
 | |
|         /// <param name="ad_format">广告格式</param>
 | |
|         /// <param name="revenue">单次展示收益</param>
 | |
|         /// <param name="position">广告展示点位</param>
 | |
|         /// <param name="number">第几次展示该格式的广告</param>
 | |
|         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);
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 收益分离
 | |
|         /// </summary>
 | |
|         /// <returns></returns>
 | |
|         private string GetName()
 | |
|         {
 | |
|             string name = "ad_impression";
 | |
|             //获取在线参数
 | |
|             string json =  FireBaseRemoteConfigManager.Instance.GetRemoteConfigString("revenue_fir");
 | |
|             if (string.IsNullOrEmpty(json))
 | |
|             {
 | |
|                 return name;
 | |
|             }
 | |
|             var revenueAdjs = JsonConvert.DeserializeObject<RevenueData[]>(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);
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 |