132 lines
4.4 KiB
C#
132 lines
4.4 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using AnyThinkAds.ThirdParty.LitJson;
|
|
using GoogleMobileAds.Api;
|
|
using ThinkingData.Analytics;
|
|
|
|
namespace WZ
|
|
{
|
|
public class ShuShuEvent : D_MonoSingleton<ShuShuEvent>
|
|
{
|
|
public static string FormatEventName(string eventName)
|
|
{
|
|
if (string.IsNullOrEmpty(eventName))
|
|
return eventName;
|
|
|
|
// 替换所有非字母、数字、中文字符为下划线
|
|
string formatted = Regex.Replace(eventName, @"[^a-zA-Z0-9\u4e00-\u9fa5]", "_");
|
|
|
|
// 将连续多个下划线合并为一个
|
|
formatted = Regex.Replace(formatted, @"_+", "_");
|
|
|
|
// 去除首尾的下划线
|
|
formatted = formatted.Trim('_');
|
|
|
|
// 只保留49个字符
|
|
if (formatted.Length > 49)
|
|
{
|
|
// 确保截断后最后一个字符不是下划线
|
|
formatted = formatted.Substring(0, 49);
|
|
}
|
|
|
|
return formatted;
|
|
}
|
|
/// <summary>
|
|
/// 设置用户属性
|
|
/// </summary>
|
|
public void UserSet(Dictionary<string, object> dic,bool setOnce = false)
|
|
{
|
|
LoggerUtils.Debug("[shushu] userset:"+JsonMapper.ToJson(dic));
|
|
if (setOnce)
|
|
{
|
|
TDAnalytics.UserSetOnce(dic);
|
|
}
|
|
else
|
|
{
|
|
TDAnalytics.UserSet(dic);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置公共事件属性
|
|
/// </summary>
|
|
/// <param name="superProperties"></param>
|
|
public void SetSuperProperties(Dictionary<string, object> superProperties)
|
|
{
|
|
LoggerUtils.Debug("[shushu] public:"+JsonMapper.ToJson(superProperties));
|
|
TDAnalytics.SetSuperProperties(superProperties);//设置公共事件属性
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送事件
|
|
/// </summary>
|
|
public void Track(string eventName)
|
|
{
|
|
TDAnalytics.Track(eventName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送事件
|
|
/// </summary>
|
|
public void Track(string eventName, string key1, object value1)
|
|
{
|
|
var extraInfo = new Dictionary<string, object>();
|
|
if (key1 != null && value1 != null)
|
|
{
|
|
extraInfo[key1] = value1;
|
|
}
|
|
|
|
Track(FormatEventName(eventName), extraInfo);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送事件
|
|
/// </summary>
|
|
public void Track(string eventName, Dictionary<string, object> properties)
|
|
{
|
|
TDAnalytics.Track(FormatEventName(eventName), properties);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送事件
|
|
/// </summary>
|
|
public void Track(string eventName, Dictionary<string, string> properties)
|
|
{
|
|
var newProperties = properties.ToDictionary(
|
|
pair => pair.Key,
|
|
pair => (object)pair.Value
|
|
);
|
|
TDAnalytics.Track(FormatEventName(eventName), newProperties);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数数收益上报
|
|
/// </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,int positionNum, NativeType nativeType)
|
|
{
|
|
TDAnalytics.Track("ad_impression", new Dictionary<string, object>
|
|
{
|
|
{ "ad_platform", ad_platform },
|
|
{ "ad_source", ad_source },
|
|
{ "ad_unit_name", ad_unit_name },
|
|
{ "ad_format", ad_format.ToString() },
|
|
{ "value", revenue },
|
|
{ "currency", "USD" },
|
|
{ "position", position },
|
|
{ "format_number", number },
|
|
{ "position_number", positionNum},
|
|
{ "native_type", nativeType}
|
|
});
|
|
}
|
|
}
|
|
} |