TapEventStandalone.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. using System;
  2. using TapSDK.Core.Internal;
  3. using TapSDK.Core.Standalone.Internal;
  4. using System.Collections.Generic;
  5. using System.Text.RegularExpressions;
  6. using UnityEngine;
  7. using TapSDK.Core.Internal.Utils;
  8. using TapSDK.Core.Internal.Log;
  9. namespace TapSDK.Core.Standalone
  10. {
  11. /// <summary>
  12. /// Represents the standalone implementation of the Tap event.
  13. /// </summary>
  14. public class TapEventStandalone : ITapEventPlatform
  15. {
  16. internal static Tracker Tracker;
  17. private readonly User User = TapCoreStandalone.User;
  18. private TapTapEventOptions eventOptions;
  19. public void Init(TapTapEventOptions eventOptions)
  20. {
  21. this.eventOptions = eventOptions;
  22. if (eventOptions == null || !eventOptions.enableTapTapEvent)
  23. {
  24. return;
  25. }
  26. Tracker = new Tracker();
  27. Tracker.Init();
  28. }
  29. /// <summary>
  30. /// Sets the user ID for tracking events.
  31. /// </summary>
  32. /// <param name="userID">The user ID to set.</param>
  33. public void SetUserID(string userID)
  34. {
  35. if (!CheckInitAndEnableState())
  36. {
  37. return;
  38. }
  39. SetUserID(userID, null);
  40. }
  41. /// <summary>
  42. /// Sets the user ID and additional properties for tracking events.
  43. /// </summary>
  44. /// <param name="userID">The user ID to set.</param>
  45. /// <param name="properties">Additional properties to associate with the user.</param>
  46. public void SetUserID(string userID, string properties)
  47. {
  48. if (!CheckInitAndEnableState())
  49. {
  50. return;
  51. }
  52. if (!IsValidUserID(userID))
  53. {
  54. TapLog.Error("Invalid user ID, length should be 1-160 and only contains a-zA-Z0-9_+/=.,:");
  55. return;
  56. }
  57. Dictionary<string, object> prop = Json.Deserialize(properties) as Dictionary<string, object>;
  58. User.Login(userID, filterProperties(prop));
  59. }
  60. /// <summary>
  61. /// Clears the current user.
  62. /// </summary>
  63. public void ClearUser()
  64. {
  65. if (!CheckInitAndEnableState())
  66. {
  67. return;
  68. }
  69. User.Logout();
  70. }
  71. /// <summary>
  72. /// Gets the device ID.
  73. /// </summary>
  74. /// <returns>The device ID.</returns>
  75. public string GetDeviceId()
  76. {
  77. if (!CheckInitAndEnableState())
  78. {
  79. return "";
  80. }
  81. return Identity.DeviceId;
  82. }
  83. /// <summary>
  84. /// Logs an event with the specified name and properties.
  85. /// </summary>
  86. /// <param name="name">The name of the event.</param>
  87. /// <param name="properties">Additional properties to associate with the event.</param>
  88. public void LogEvent(string name, string properties)
  89. {
  90. if (!CheckInitAndEnableState())
  91. {
  92. return;
  93. }
  94. // name 长度256非空,不符合的丢事件,打log
  95. if (!checkLength(name))
  96. {
  97. TapLog.Error(name + " Event name length should be less than or equal to 256 characters.");
  98. return;
  99. }
  100. Dictionary<string, object> prop = Json.Deserialize(properties) as Dictionary<string, object>;
  101. Tracker.TrackEvent(name, filterProperties(prop));
  102. }
  103. /// <summary>
  104. /// Tracks device initialization with the specified properties.
  105. /// </summary>
  106. /// <param name="properties">Additional properties to associate with the device initialization.</param>
  107. public void DeviceInitialize(string properties)
  108. {
  109. if (!CheckInitAndEnableState())
  110. {
  111. return;
  112. }
  113. Dictionary<string, object> prop = Json.Deserialize(properties) as Dictionary<string, object>;
  114. Tracker.TrackDeviceProperties(Constants.PROPERTY_INITIALIZE_TYPE, filterProperties(prop));
  115. }
  116. /// <summary>
  117. /// Tracks device update with the specified properties.
  118. /// </summary>
  119. /// <param name="properties">Additional properties to associate with the device update.</param>
  120. public void DeviceUpdate(string properties)
  121. {
  122. if (!CheckInitAndEnableState())
  123. {
  124. return;
  125. }
  126. Dictionary<string, object> prop = Json.Deserialize(properties) as Dictionary<string, object>;
  127. Tracker.TrackDeviceProperties(Constants.PROPERTY_UPDATE_TYPE, filterProperties(prop));
  128. }
  129. /// <summary>
  130. /// Tracks device addition with the specified properties.
  131. /// </summary>
  132. /// <param name="properties">Additional properties to associate with the device addition.</param>
  133. public void DeviceAdd(string properties)
  134. {
  135. if (!CheckInitAndEnableState())
  136. {
  137. return;
  138. }
  139. Dictionary<string, object> prop = Json.Deserialize(properties) as Dictionary<string, object>;
  140. Tracker.TrackDeviceProperties(Constants.PROPERTY_ADD_TYPE, filterProperties(prop));
  141. }
  142. /// <summary>
  143. /// Tracks user initialization with the specified properties.
  144. /// </summary>
  145. /// <param name="properties">Additional properties to associate with the user initialization.</param>
  146. public void UserInitialize(string properties)
  147. {
  148. if (!CheckInitAndEnableState())
  149. {
  150. return;
  151. }
  152. Dictionary<string, object> prop = Json.Deserialize(properties) as Dictionary<string, object>;
  153. Tracker.TrackUserProperties(Constants.PROPERTY_INITIALIZE_TYPE, filterProperties(prop));
  154. }
  155. /// <summary>
  156. /// Tracks user update with the specified properties.
  157. /// </summary>
  158. /// <param name="properties">Additional properties to associate with the user update.</param>
  159. public void UserUpdate(string properties)
  160. {
  161. if (!CheckInitAndEnableState())
  162. {
  163. return;
  164. }
  165. Dictionary<string, object> prop = Json.Deserialize(properties) as Dictionary<string, object>;
  166. Tracker.TrackUserProperties(Constants.PROPERTY_UPDATE_TYPE, filterProperties(prop));
  167. }
  168. /// <summary>
  169. /// Tracks user addition with the specified properties.
  170. /// </summary>
  171. /// <param name="properties">Additional properties to associate with the user addition.</param>
  172. public void UserAdd(string properties)
  173. {
  174. if (!CheckInitAndEnableState())
  175. {
  176. return;
  177. }
  178. Dictionary<string, object> prop = Json.Deserialize(properties) as Dictionary<string, object>;
  179. Tracker.TrackUserProperties(Constants.PROPERTY_ADD_TYPE, filterProperties(prop));
  180. }
  181. /// <summary>
  182. /// Adds a common property with the specified key and value.
  183. /// </summary>
  184. /// <param name="key">The key of the common property.</param>
  185. /// <param name="value">The value of the common property.</param>
  186. public void AddCommonProperty(string key, string value)
  187. {
  188. if (!CheckInitAndEnableState())
  189. {
  190. return;
  191. }
  192. if (!checkLength(key))
  193. {
  194. TapLog.Error(key + " Property key length should be less than or equal to 256 characters.");
  195. return;
  196. }
  197. if (!checkLength(value))
  198. {
  199. TapLog.Error(value + " Property value length should be less than or equal to 256 characters.");
  200. return;
  201. }
  202. Tracker.AddCommonProperty(key, value);
  203. }
  204. /// <summary>
  205. /// Adds common properties with the specified JSON string.
  206. /// </summary>
  207. /// <param name="properties">The JSON string containing the common properties.</param>
  208. public void AddCommon(string properties)
  209. {
  210. if (!CheckInitAndEnableState())
  211. {
  212. return;
  213. }
  214. Dictionary<string, object> prop = Json.Deserialize(properties) as Dictionary<string, object>;
  215. Tracker.AddCommon(filterProperties(prop));
  216. }
  217. /// <summary>
  218. /// Clears the common property with the specified key.
  219. /// </summary>
  220. /// <param name="key">The key of the common property to clear.</param>
  221. public void ClearCommonProperty(string key)
  222. {
  223. if (!CheckInitAndEnableState())
  224. {
  225. return;
  226. }
  227. Tracker.ClearCommonProperty(key);
  228. }
  229. /// <summary>
  230. /// Clears the common properties with the specified keys.
  231. /// </summary>
  232. /// <param name="keys">The keys of the common properties to clear.</param>
  233. public void ClearCommonProperties(string[] keys)
  234. {
  235. if (!CheckInitAndEnableState())
  236. {
  237. return;
  238. }
  239. Tracker.ClearCommonProperties(keys);
  240. }
  241. /// <summary>
  242. /// Clears all common properties.
  243. /// </summary>
  244. public void ClearAllCommonProperties()
  245. {
  246. if (!CheckInitAndEnableState())
  247. {
  248. return;
  249. }
  250. Tracker.ClearAllCommonProperties();
  251. }
  252. /// <summary>
  253. /// Logs a charge event with the specified details and properties.
  254. /// </summary>
  255. /// <param name="orderID">The ID of the order.</param>
  256. /// <param name="productName">The name of the product.</param>
  257. /// <param name="amount">The amount of the charge.</param>
  258. /// <param name="currencyType">The currency type of the charge.</param>
  259. /// <param name="paymentMethod">The payment method used for the charge.</param>
  260. /// <param name="properties">Additional properties to associate with the charge event.</param>
  261. public void LogChargeEvent(string orderID, string productName, long amount, string currencyType, string paymentMethod, string properties)
  262. {
  263. if (!CheckInitAndEnableState())
  264. {
  265. return;
  266. }
  267. if (amount <= 0 || amount > 100000000000)
  268. {
  269. TapLog.Error(amount + " is invalid, amount should be in range (0, 100000000000]");
  270. return;
  271. }
  272. Tracker.LogPurchasedEvent(orderID, productName, amount, currencyType, paymentMethod, properties);
  273. }
  274. /// <summary>
  275. /// Registers a callback function for retrieving dynamic properties.
  276. /// </summary>
  277. /// <param name="callback">The callback function that returns a JSON string containing the dynamic properties.</param>
  278. public void RegisterDynamicProperties(Func<string> callback)
  279. {
  280. if (!CheckInitAndEnableState())
  281. {
  282. return;
  283. }
  284. DynamicProperties dynamicProperties = new DynamicProperties(callback);
  285. Tracker.RegisterDynamicPropsDelegate(dynamicProperties);
  286. }
  287. /// <summary>
  288. /// set custom oaid value
  289. /// </summary>
  290. /// <param name="value">oaid</param>
  291. public void SetOAID(string value)
  292. {
  293. TapLog.Log("SetOAID called in PC platform (empty implementation)");
  294. }
  295. /// <summary>
  296. /// Logs a device login event.
  297. /// </summary>
  298. public void LogDeviceLoginEvent()
  299. {
  300. TapLog.Log("LogDeviceLoginEvent called in PC platform (empty implementation)");
  301. }
  302. /// <summary>
  303. /// Represents the implementation of dynamic properties for the Tap event platform.
  304. /// </summary>
  305. public class DynamicProperties : Tracker.IDynamicProperties
  306. {
  307. readonly Func<string> callback;
  308. /// <summary>
  309. /// Initializes a new instance of the <see cref="DynamicProperties"/> class with the specified callback function.
  310. /// </summary>
  311. /// <param name="callback">The callback function that returns a JSON string containing the dynamic properties.</param>
  312. public DynamicProperties(Func<string> callback)
  313. {
  314. this.callback = callback;
  315. }
  316. /// <summary>
  317. /// Gets the dynamic properties.
  318. /// </summary>
  319. /// <returns>A dictionary containing the dynamic properties.</returns>
  320. public Dictionary<string, object> GetDynamicProperties()
  321. {
  322. var jsonString = callback();
  323. return Json.Deserialize(jsonString) as Dictionary<string, object>;
  324. }
  325. }
  326. private bool checkLength(string value)
  327. {
  328. var maxLength = 256;
  329. if (value.Length <= 0 || value.Length > maxLength)
  330. {
  331. return false;
  332. }
  333. return true;
  334. }
  335. private bool IsValidUserID(string userID)
  336. {
  337. string pattern = @"^[a-zA-Z0-9_+/=.,:]{1,160}$";
  338. Regex regex = new Regex(pattern);
  339. return regex.IsMatch(userID);
  340. }
  341. /// <summary>
  342. /// 检查是否 Core模块初始化及 TapEvent 启用
  343. /// </summary>
  344. /// <returns></returns>
  345. private bool CheckInitAndEnableState()
  346. {
  347. if (!TapCoreStandalone.CheckInitState())
  348. {
  349. return false;
  350. }
  351. else
  352. {
  353. if (eventOptions == null || !eventOptions.enableTapTapEvent)
  354. {
  355. string tip = "当前应用已关闭 TapTapEvent 开关,请开启后再调用相关接口";
  356. TapLog.Error(tip + " 开启方式:enableTapTapEvent = true");
  357. return false;
  358. }
  359. else
  360. {
  361. return true;
  362. }
  363. }
  364. }
  365. private Dictionary<string, object> filterProperties(Dictionary<string, object> properties)
  366. {
  367. Dictionary<string, object> filteredProperties = new Dictionary<string, object>();
  368. if (properties != null)
  369. {
  370. foreach (var property in properties)
  371. {
  372. if (property.Key.Length <= 0 || property.Key.Length > 256)
  373. {
  374. TapLog.Log(property.Key + " Property key length should be more then 0 and less than or equal to 256 characters.");
  375. continue;
  376. }
  377. if (property.Value.ToString().Length > 256)
  378. {
  379. TapLog.Log(property.Value + " Property value length should be less than or equal to 256 characters.");
  380. continue;
  381. }
  382. filteredProperties.Add(property.Key, property.Value);
  383. }
  384. }
  385. return filteredProperties;
  386. }
  387. }
  388. }