using System;
using TapSDK.Core.Internal;
using TapSDK.Core.Standalone.Internal;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using TapSDK.Core.Internal.Utils;
using TapSDK.Core.Internal.Log;
namespace TapSDK.Core.Standalone
{
///
/// Represents the standalone implementation of the Tap event.
///
public class TapEventStandalone : ITapEventPlatform
{
internal static Tracker Tracker;
private readonly User User = TapCoreStandalone.User;
private TapTapEventOptions eventOptions;
public void Init(TapTapEventOptions eventOptions)
{
this.eventOptions = eventOptions;
if (eventOptions == null || !eventOptions.enableTapTapEvent)
{
return;
}
Tracker = new Tracker();
Tracker.Init();
}
///
/// Sets the user ID for tracking events.
///
/// The user ID to set.
public void SetUserID(string userID)
{
if (!CheckInitAndEnableState())
{
return;
}
SetUserID(userID, null);
}
///
/// Sets the user ID and additional properties for tracking events.
///
/// The user ID to set.
/// Additional properties to associate with the user.
public void SetUserID(string userID, string properties)
{
if (!CheckInitAndEnableState())
{
return;
}
if (!IsValidUserID(userID))
{
TapLog.Error("Invalid user ID, length should be 1-160 and only contains a-zA-Z0-9_+/=.,:");
return;
}
Dictionary prop = Json.Deserialize(properties) as Dictionary;
User.Login(userID, filterProperties(prop));
}
///
/// Clears the current user.
///
public void ClearUser()
{
if (!CheckInitAndEnableState())
{
return;
}
User.Logout();
}
///
/// Gets the device ID.
///
/// The device ID.
public string GetDeviceId()
{
if (!CheckInitAndEnableState())
{
return "";
}
return Identity.DeviceId;
}
///
/// Logs an event with the specified name and properties.
///
/// The name of the event.
/// Additional properties to associate with the event.
public void LogEvent(string name, string properties)
{
if (!CheckInitAndEnableState())
{
return;
}
// name 长度256非空,不符合的丢事件,打log
if (!checkLength(name))
{
TapLog.Error(name + " Event name length should be less than or equal to 256 characters.");
return;
}
Dictionary prop = Json.Deserialize(properties) as Dictionary;
Tracker.TrackEvent(name, filterProperties(prop));
}
///
/// Tracks device initialization with the specified properties.
///
/// Additional properties to associate with the device initialization.
public void DeviceInitialize(string properties)
{
if (!CheckInitAndEnableState())
{
return;
}
Dictionary prop = Json.Deserialize(properties) as Dictionary;
Tracker.TrackDeviceProperties(Constants.PROPERTY_INITIALIZE_TYPE, filterProperties(prop));
}
///
/// Tracks device update with the specified properties.
///
/// Additional properties to associate with the device update.
public void DeviceUpdate(string properties)
{
if (!CheckInitAndEnableState())
{
return;
}
Dictionary prop = Json.Deserialize(properties) as Dictionary;
Tracker.TrackDeviceProperties(Constants.PROPERTY_UPDATE_TYPE, filterProperties(prop));
}
///
/// Tracks device addition with the specified properties.
///
/// Additional properties to associate with the device addition.
public void DeviceAdd(string properties)
{
if (!CheckInitAndEnableState())
{
return;
}
Dictionary prop = Json.Deserialize(properties) as Dictionary;
Tracker.TrackDeviceProperties(Constants.PROPERTY_ADD_TYPE, filterProperties(prop));
}
///
/// Tracks user initialization with the specified properties.
///
/// Additional properties to associate with the user initialization.
public void UserInitialize(string properties)
{
if (!CheckInitAndEnableState())
{
return;
}
Dictionary prop = Json.Deserialize(properties) as Dictionary;
Tracker.TrackUserProperties(Constants.PROPERTY_INITIALIZE_TYPE, filterProperties(prop));
}
///
/// Tracks user update with the specified properties.
///
/// Additional properties to associate with the user update.
public void UserUpdate(string properties)
{
if (!CheckInitAndEnableState())
{
return;
}
Dictionary prop = Json.Deserialize(properties) as Dictionary;
Tracker.TrackUserProperties(Constants.PROPERTY_UPDATE_TYPE, filterProperties(prop));
}
///
/// Tracks user addition with the specified properties.
///
/// Additional properties to associate with the user addition.
public void UserAdd(string properties)
{
if (!CheckInitAndEnableState())
{
return;
}
Dictionary prop = Json.Deserialize(properties) as Dictionary;
Tracker.TrackUserProperties(Constants.PROPERTY_ADD_TYPE, filterProperties(prop));
}
///
/// Adds a common property with the specified key and value.
///
/// The key of the common property.
/// The value of the common property.
public void AddCommonProperty(string key, string value)
{
if (!CheckInitAndEnableState())
{
return;
}
if (!checkLength(key))
{
TapLog.Error(key + " Property key length should be less than or equal to 256 characters.");
return;
}
if (!checkLength(value))
{
TapLog.Error(value + " Property value length should be less than or equal to 256 characters.");
return;
}
Tracker.AddCommonProperty(key, value);
}
///
/// Adds common properties with the specified JSON string.
///
/// The JSON string containing the common properties.
public void AddCommon(string properties)
{
if (!CheckInitAndEnableState())
{
return;
}
Dictionary prop = Json.Deserialize(properties) as Dictionary;
Tracker.AddCommon(filterProperties(prop));
}
///
/// Clears the common property with the specified key.
///
/// The key of the common property to clear.
public void ClearCommonProperty(string key)
{
if (!CheckInitAndEnableState())
{
return;
}
Tracker.ClearCommonProperty(key);
}
///
/// Clears the common properties with the specified keys.
///
/// The keys of the common properties to clear.
public void ClearCommonProperties(string[] keys)
{
if (!CheckInitAndEnableState())
{
return;
}
Tracker.ClearCommonProperties(keys);
}
///
/// Clears all common properties.
///
public void ClearAllCommonProperties()
{
if (!CheckInitAndEnableState())
{
return;
}
Tracker.ClearAllCommonProperties();
}
///
/// Logs a charge event with the specified details and properties.
///
/// The ID of the order.
/// The name of the product.
/// The amount of the charge.
/// The currency type of the charge.
/// The payment method used for the charge.
/// Additional properties to associate with the charge event.
public void LogChargeEvent(string orderID, string productName, long amount, string currencyType, string paymentMethod, string properties)
{
if (!CheckInitAndEnableState())
{
return;
}
if (amount <= 0 || amount > 100000000000)
{
TapLog.Error(amount + " is invalid, amount should be in range (0, 100000000000]");
return;
}
Tracker.LogPurchasedEvent(orderID, productName, amount, currencyType, paymentMethod, properties);
}
///
/// Registers a callback function for retrieving dynamic properties.
///
/// The callback function that returns a JSON string containing the dynamic properties.
public void RegisterDynamicProperties(Func callback)
{
if (!CheckInitAndEnableState())
{
return;
}
DynamicProperties dynamicProperties = new DynamicProperties(callback);
Tracker.RegisterDynamicPropsDelegate(dynamicProperties);
}
///
/// set custom oaid value
///
/// oaid
public void SetOAID(string value)
{
TapLog.Log("SetOAID called in PC platform (empty implementation)");
}
///
/// Logs a device login event.
///
public void LogDeviceLoginEvent()
{
TapLog.Log("LogDeviceLoginEvent called in PC platform (empty implementation)");
}
///
/// Represents the implementation of dynamic properties for the Tap event platform.
///
public class DynamicProperties : Tracker.IDynamicProperties
{
readonly Func callback;
///
/// Initializes a new instance of the class with the specified callback function.
///
/// The callback function that returns a JSON string containing the dynamic properties.
public DynamicProperties(Func callback)
{
this.callback = callback;
}
///
/// Gets the dynamic properties.
///
/// A dictionary containing the dynamic properties.
public Dictionary GetDynamicProperties()
{
var jsonString = callback();
return Json.Deserialize(jsonString) as Dictionary;
}
}
private bool checkLength(string value)
{
var maxLength = 256;
if (value.Length <= 0 || value.Length > maxLength)
{
return false;
}
return true;
}
private bool IsValidUserID(string userID)
{
string pattern = @"^[a-zA-Z0-9_+/=.,:]{1,160}$";
Regex regex = new Regex(pattern);
return regex.IsMatch(userID);
}
///
/// 检查是否 Core模块初始化及 TapEvent 启用
///
///
private bool CheckInitAndEnableState()
{
if (!TapCoreStandalone.CheckInitState())
{
return false;
}
else
{
if (eventOptions == null || !eventOptions.enableTapTapEvent)
{
string tip = "当前应用已关闭 TapTapEvent 开关,请开启后再调用相关接口";
TapLog.Error(tip + " 开启方式:enableTapTapEvent = true");
return false;
}
else
{
return true;
}
}
}
private Dictionary filterProperties(Dictionary properties)
{
Dictionary filteredProperties = new Dictionary();
if (properties != null)
{
foreach (var property in properties)
{
if (property.Key.Length <= 0 || property.Key.Length > 256)
{
TapLog.Log(property.Key + " Property key length should be more then 0 and less than or equal to 256 characters.");
continue;
}
if (property.Value.ToString().Length > 256)
{
TapLog.Log(property.Value + " Property value length should be less than or equal to 256 characters.");
continue;
}
filteredProperties.Add(property.Key, property.Value);
}
}
return filteredProperties;
}
}
}