TalkingDataSDK.cs 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. // version: 5.0.4
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. #if UNITY_ANDROID
  5. using System;
  6. #endif
  7. #if UNITY_IPHONE
  8. using System.Runtime.InteropServices;
  9. using System.Collections;
  10. #endif
  11. public static class TalkingDataSDK
  12. {
  13. #if UNITY_ANDROID
  14. private static readonly string TALKINGDATA_CLASS = "com.tendcloud.tenddata.TalkingDataSDK";
  15. private static AndroidJavaClass talkingdataClass;
  16. private static AndroidJavaClass unityPlayerClass;
  17. #endif
  18. #if UNITY_IPHONE
  19. [DllImport("__Internal")]
  20. private static extern void TDInitSDK(string appId, string channelId, string custom);
  21. [DllImport("__Internal")]
  22. private static extern void TDStartA();
  23. [DllImport("__Internal")]
  24. private static extern void TDBackgroundSessionEnabled();
  25. [DllImport("__Internal")]
  26. private static extern string TDGetDeviceId();
  27. [DllImport("__Internal")]
  28. private static extern void TDSetVerboseLogDisable();
  29. [DllImport("__Internal")]
  30. private static extern void TDSetLocation(double latitude, double longitude);
  31. [DllImport("__Internal")]
  32. private static extern void TDOnPageBegin(string pageName);
  33. [DllImport("__Internal")]
  34. private static extern void TDOnPageEnd(string pageName);
  35. [DllImport("__Internal")]
  36. private static extern void TDOnReceiveDeepLink(string url);
  37. [DllImport("__Internal")]
  38. private static extern void TDOnRegister(string profileId, string profileJson, string invitationCode, string eventValueJson);
  39. [DllImport("__Internal")]
  40. private static extern void TDOnLogin(string profileId, string profileJson, string eventValueJson);
  41. [DllImport("__Internal")]
  42. private static extern void TDOnProfileUpdate(string profileJson);
  43. [DllImport("__Internal")]
  44. private static extern void TDOnCreateCard(string profileId, string method, string content);
  45. [DllImport("__Internal")]
  46. private static extern void TDOnFavorite(string category, string content, string eventValueJson);
  47. [DllImport("__Internal")]
  48. private static extern void TDOnShare(string profileId, string content, string eventValueJson);
  49. [DllImport("__Internal")]
  50. private static extern void TDOnPunch(string profileId, string punchId);
  51. [DllImport("__Internal")]
  52. private static extern void TDOnSearch(string searchJson);
  53. #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_ONLINEEDU
  54. [DllImport("__Internal")]
  55. private static extern void TDOnContact(string profileId, string content);
  56. #endif
  57. #if TD_GAME || TD_TOUR || TD_ONLINEEDU || TD_READING || TD_OTHER
  58. [DllImport("__Internal")]
  59. private static extern void TDOnPay(string profileId, string orderId, int amount, string currencyType, string paymentType, string itemId, int itemCount);
  60. #endif
  61. #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_ONLINEEDU
  62. [DllImport("__Internal")]
  63. private static extern void TDOnChargeBack(string profileId, string orderId, string reason, string type);
  64. #endif
  65. #if TD_FINANCE || TD_ONLINEEDU
  66. [DllImport("__Internal")]
  67. private static extern void TDOnReservation(string profileId, string reservationId, string category, int amount, string term);
  68. #endif
  69. #if TD_RETAIL || TD_TOUR
  70. [DllImport("__Internal")]
  71. private static extern void TDOnBooking(string profileId, string bookingId, string category, int amount, string content);
  72. #endif
  73. #if TD_RETAIL
  74. [DllImport("__Internal")]
  75. private static extern void TDOnViewItem(string itemId, string category, string name, int unitPrice, string eventValueJson);
  76. [DllImport("__Internal")]
  77. private static extern void TDOnAddItemToShoppingCart(string item, string category, string name, int unitPrice, int amount, string eventValueJson);
  78. [DllImport("__Internal")]
  79. private static extern void TDOnViewShoppingCart(string shoppingCartJson);
  80. [DllImport("__Internal")]
  81. private static extern void TDOnPlaceOrder(string orderJson, string profileId, string eventValueJson);
  82. [DllImport("__Internal")]
  83. private static extern void TDOnOrderPaySucc(string orderJson, string paymentType, string profileId);
  84. [DllImport("__Internal")]
  85. private static extern void TDOnCancelOrder(string orderJson);
  86. #endif
  87. #if TD_FINANCE
  88. [DllImport("__Internal")]
  89. private static extern void TDOnCredit(string profileId, int amount, string content);
  90. [DllImport("__Internal")]
  91. private static extern void TDOnTransaction(string profileId, string transactionJson);
  92. #endif
  93. #if TD_GAME
  94. [DllImport("__Internal")]
  95. private static extern void TDOnCreateRole(string name);
  96. [DllImport("__Internal")]
  97. private static extern void TDOnLevelPass(string profileId, string levelId);
  98. [DllImport("__Internal")]
  99. private static extern void TDOnGuideFinished(string profileId, string content);
  100. #endif
  101. #if TD_ONLINEEDU
  102. [DllImport("__Internal")]
  103. private static extern void TDOnLearn(string profileId, string course, long begin, int duration);
  104. [DllImport("__Internal")]
  105. private static extern void TDOnPreviewFinished(string profileId, string content);
  106. #endif
  107. #if TD_READING
  108. [DllImport("__Internal")]
  109. private static extern void TDOnRead(string profileId, string book, long begin, int duration);
  110. [DllImport("__Internal")]
  111. private static extern void TDOnFreeFinished(string profileId, string content);
  112. #endif
  113. #if TD_GAME || TD_ONLINEEDU
  114. [DllImport("__Internal")]
  115. private static extern void TDOnAchievementUnlock(string profileId, string achievementId);
  116. #endif
  117. #if TD_FINANCE || TD_TOUR || TD_OTHER
  118. [DllImport("__Internal")]
  119. private static extern void TDOnBrowse(string profileId, string content, long begin, int duration);
  120. #endif
  121. #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_OTHER
  122. [DllImport("__Internal")]
  123. private static extern void TDOnTrialFinished(string profileId, string content);
  124. #endif
  125. [DllImport("__Internal")]
  126. private static extern void TDOnEvent(string eventId, string parameters, string eventValueJson);
  127. [DllImport("__Internal")]
  128. private static extern void TDSetGlobalKV(string key, string strVal, double numVal);
  129. [DllImport("__Internal")]
  130. private static extern void TDRemoveGlobalKV(string key);
  131. #endif
  132. #if UNITY_ANDROID
  133. private static AndroidJavaObject GetCurrentActivity()
  134. {
  135. if (unityPlayerClass == null)
  136. {
  137. unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  138. }
  139. AndroidJavaObject activity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
  140. return activity;
  141. }
  142. private static AndroidJavaObject DictionaryToAndroidMap(Dictionary<string, object> parameters)
  143. {
  144. AndroidJavaObject map = null;
  145. if (parameters != null && parameters.Count > 0)
  146. {
  147. int count = parameters.Count;
  148. map = new AndroidJavaObject("java.util.HashMap", count);
  149. IntPtr method_Put = AndroidJNIHelper.GetMethodID(map.GetRawClass(), "put",
  150. "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
  151. object[] args = new object[2];
  152. foreach (KeyValuePair<string, object> kvp in parameters)
  153. {
  154. args[0] = new AndroidJavaObject("java.lang.String", kvp.Key);
  155. args[1] = typeof(string).IsInstanceOfType(kvp.Value)
  156. ? new AndroidJavaObject("java.lang.String", kvp.Value)
  157. : new AndroidJavaObject("java.lang.Double", "" + kvp.Value);
  158. AndroidJNI.CallObjectMethod(map.GetRawObject(), method_Put, AndroidJNIHelper.CreateJNIArgArray(args));
  159. }
  160. }
  161. return map;
  162. }
  163. #endif
  164. #if UNITY_IPHONE
  165. private static string DictionaryToJSONString(Dictionary<string, object> parameters)
  166. {
  167. string json = null;
  168. if (parameters != null && parameters.Count > 0)
  169. {
  170. json = "{";
  171. foreach (KeyValuePair<string, object> kvp in parameters)
  172. {
  173. if (kvp.Value is string)
  174. {
  175. json += "\"" + kvp.Key + "\":\"" + kvp.Value + "\",";
  176. }
  177. else
  178. {
  179. try
  180. {
  181. double tmp = System.Convert.ToDouble(kvp.Value);
  182. json += "\"" + kvp.Key + "\":" + tmp + ",";
  183. }
  184. catch (System.Exception)
  185. {
  186. }
  187. }
  188. }
  189. json = json.TrimEnd(',');
  190. json += "}";
  191. }
  192. return json;
  193. }
  194. #endif
  195. public static void SetConfig(TalkingDataSDKConfig config)
  196. {
  197. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  198. {
  199. #if UNITY_ANDROID
  200. if (talkingdataClass == null)
  201. {
  202. talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
  203. }
  204. talkingdataClass.CallStatic("setConfig", config.javaObj);
  205. #endif
  206. }
  207. }
  208. public static void InitSDK(string appId, string channelId, string custom)
  209. {
  210. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  211. {
  212. Debug.Log("TalkingData Unity SDK.");
  213. #if UNITY_ANDROID
  214. using (AndroidJavaClass dz = new AndroidJavaClass("com.tendcloud.tenddata.dz"))
  215. {
  216. dz.SetStatic("a", 2);
  217. }
  218. if (talkingdataClass == null)
  219. {
  220. talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
  221. }
  222. AndroidJavaObject activity = GetCurrentActivity();
  223. talkingdataClass.CallStatic("initSDK", activity, appId, channelId, custom);
  224. #endif
  225. #if UNITY_IPHONE
  226. TDInitSDK(appId, channelId, custom);
  227. #endif
  228. }
  229. }
  230. public static void StartA()
  231. {
  232. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  233. {
  234. #if UNITY_ANDROID
  235. if (talkingdataClass != null)
  236. {
  237. AndroidJavaObject activity = GetCurrentActivity();
  238. talkingdataClass.CallStatic("startA", activity);
  239. talkingdataClass.CallStatic("onResume", activity);
  240. }
  241. #endif
  242. #if UNITY_IPHONE
  243. TDStartA();
  244. #endif
  245. }
  246. }
  247. public static void OnPause()
  248. {
  249. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  250. {
  251. #if UNITY_ANDROID
  252. if (talkingdataClass != null)
  253. {
  254. talkingdataClass.CallStatic("onPause", GetCurrentActivity());
  255. }
  256. #endif
  257. }
  258. }
  259. public static void BackgroundSessionEnabled()
  260. {
  261. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  262. {
  263. #if UNITY_IPHONE
  264. TDBackgroundSessionEnabled();
  265. #endif
  266. }
  267. }
  268. private static string deviceId = null;
  269. public static string GetDeviceId()
  270. {
  271. if (deviceId == null && Application.platform != RuntimePlatform.OSXEditor &&
  272. Application.platform != RuntimePlatform.WindowsEditor)
  273. {
  274. #if UNITY_ANDROID
  275. if (talkingdataClass == null)
  276. {
  277. talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
  278. }
  279. deviceId = talkingdataClass.CallStatic<string>("getDeviceId", GetCurrentActivity());
  280. #endif
  281. #if UNITY_IPHONE
  282. deviceId = TDGetDeviceId();
  283. #endif
  284. }
  285. return deviceId;
  286. }
  287. private static string oaid = null;
  288. public static string GetOAID()
  289. {
  290. if (oaid == null && Application.platform != RuntimePlatform.OSXEditor &&
  291. Application.platform != RuntimePlatform.WindowsEditor)
  292. {
  293. #if UNITY_ANDROID
  294. if (talkingdataClass == null)
  295. {
  296. talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
  297. }
  298. oaid = talkingdataClass.CallStatic<string>("getOAID", GetCurrentActivity());
  299. #endif
  300. }
  301. return oaid;
  302. }
  303. public static void SetVerboseLogDisable()
  304. {
  305. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  306. {
  307. #if UNITY_ANDROID
  308. if (talkingdataClass == null)
  309. {
  310. talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
  311. }
  312. talkingdataClass.CallStatic("setVerboseLogDisable");
  313. #endif
  314. #if UNITY_IPHONE
  315. TDSetVerboseLogDisable();
  316. #endif
  317. }
  318. }
  319. public static void SetLocation(double latitude, double longitude)
  320. {
  321. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  322. {
  323. #if UNITY_IPHONE
  324. TDSetLocation(latitude, longitude);
  325. #endif
  326. }
  327. }
  328. public static void OnPageBegin(string pageName)
  329. {
  330. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  331. {
  332. #if UNITY_ANDROID
  333. if (talkingdataClass != null)
  334. {
  335. talkingdataClass.CallStatic("onPageBegin", GetCurrentActivity(), pageName);
  336. }
  337. #endif
  338. #if UNITY_IPHONE
  339. TDOnPageBegin(pageName);
  340. #endif
  341. }
  342. }
  343. public static void OnPageEnd(string pageName)
  344. {
  345. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  346. {
  347. #if UNITY_ANDROID
  348. if (talkingdataClass != null)
  349. {
  350. talkingdataClass.CallStatic("onPageEnd", GetCurrentActivity(), pageName);
  351. }
  352. #endif
  353. #if UNITY_IPHONE
  354. TDOnPageEnd(pageName);
  355. #endif
  356. }
  357. }
  358. public static void OnReceiveDeepLink(string url)
  359. {
  360. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  361. {
  362. #if UNITY_ANDROID
  363. if (talkingdataClass != null)
  364. {
  365. talkingdataClass.CallStatic("onReceiveDeepLink", url);
  366. }
  367. #endif
  368. #if UNITY_IPHONE
  369. TDOnReceiveDeepLink(url);
  370. #endif
  371. }
  372. }
  373. public static void OnRegister(string profileId, TalkingDataProfile profile, string invitationCode,
  374. Dictionary<string, object> eventValue)
  375. {
  376. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  377. {
  378. #if UNITY_ANDROID
  379. if (talkingdataClass != null)
  380. {
  381. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  382. talkingdataClass.CallStatic("onRegister", profileId, profile.javaObj, invitationCode, eventValueMap);
  383. if (eventValueMap != null)
  384. {
  385. eventValueMap.Dispose();
  386. }
  387. }
  388. #endif
  389. #if UNITY_IPHONE
  390. string eventValueJson = DictionaryToJSONString(eventValue);
  391. TDOnRegister(profileId, profile.ToString(), invitationCode, eventValueJson);
  392. #endif
  393. }
  394. }
  395. public static void OnLogin(string profileId, TalkingDataProfile profile, Dictionary<string, object> eventValue)
  396. {
  397. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  398. {
  399. #if UNITY_ANDROID
  400. if (talkingdataClass != null)
  401. {
  402. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  403. talkingdataClass.CallStatic("onLogin", profileId, profile.javaObj, eventValueMap);
  404. if (eventValueMap != null)
  405. {
  406. eventValueMap.Dispose();
  407. }
  408. }
  409. #endif
  410. #if UNITY_IPHONE
  411. string eventValueJson = DictionaryToJSONString(eventValue);
  412. TDOnLogin(profileId, profile.ToString(), eventValueJson);
  413. #endif
  414. }
  415. }
  416. public static void OnProfileUpdate(TalkingDataProfile profile)
  417. {
  418. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  419. {
  420. #if UNITY_ANDROID
  421. if (talkingdataClass != null)
  422. {
  423. talkingdataClass.CallStatic("onProfileUpdate", profile.javaObj);
  424. }
  425. #endif
  426. #if UNITY_IPHONE
  427. TDOnProfileUpdate(profile.ToString());
  428. #endif
  429. }
  430. }
  431. public static void OnCreateCard(string profileId, string method, string content)
  432. {
  433. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  434. {
  435. #if UNITY_ANDROID
  436. if (talkingdataClass != null)
  437. {
  438. talkingdataClass.CallStatic("onCreateCard", profileId, method, content);
  439. }
  440. #endif
  441. #if UNITY_IPHONE
  442. TDOnCreateCard(profileId, method, content);
  443. #endif
  444. }
  445. }
  446. public static void OnFavorite(string category, string content, Dictionary<string, object> eventValue)
  447. {
  448. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  449. {
  450. #if UNITY_ANDROID
  451. if (talkingdataClass != null)
  452. {
  453. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  454. talkingdataClass.CallStatic("onFavorite", category, content, eventValueMap);
  455. if (eventValueMap != null)
  456. {
  457. eventValueMap.Dispose();
  458. }
  459. }
  460. #endif
  461. #if UNITY_IPHONE
  462. string eventValueJson = DictionaryToJSONString(eventValue);
  463. TDOnFavorite(category, content, eventValueJson);
  464. #endif
  465. }
  466. }
  467. public static void OnShare(string profileId, string content, Dictionary<string, object> eventValue)
  468. {
  469. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  470. {
  471. #if UNITY_ANDROID
  472. if (talkingdataClass != null)
  473. {
  474. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  475. talkingdataClass.CallStatic("onShare", profileId, content, eventValueMap);
  476. if (eventValueMap != null)
  477. {
  478. eventValueMap.Dispose();
  479. }
  480. }
  481. #endif
  482. #if UNITY_IPHONE
  483. string eventValueJson = DictionaryToJSONString(eventValue);
  484. TDOnShare(profileId, content, eventValueJson);
  485. #endif
  486. }
  487. }
  488. public static void OnPunch(string profileId, string punchId)
  489. {
  490. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  491. {
  492. #if UNITY_ANDROID
  493. if (talkingdataClass != null)
  494. {
  495. talkingdataClass.CallStatic("onPunch", profileId, punchId);
  496. }
  497. #endif
  498. #if UNITY_IPHONE
  499. TDOnPunch(profileId, punchId);
  500. #endif
  501. }
  502. }
  503. public static void OnSearch(TalkingDataSearch search)
  504. {
  505. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  506. {
  507. #if UNITY_ANDROID
  508. if (talkingdataClass != null)
  509. {
  510. talkingdataClass.CallStatic("onSearch", search.javaObj);
  511. }
  512. #endif
  513. #if UNITY_IPHONE
  514. TDOnSearch(search.ToString());
  515. #endif
  516. }
  517. }
  518. #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_ONLINEEDU
  519. public static void OnContact(string profileId, string content)
  520. {
  521. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  522. {
  523. #if UNITY_ANDROID
  524. if (talkingdataClass != null)
  525. {
  526. talkingdataClass.CallStatic("onContact", profileId, content);
  527. }
  528. #endif
  529. #if UNITY_IPHONE
  530. TDOnContact(profileId, content);
  531. #endif
  532. }
  533. }
  534. #endif
  535. #if TD_GAME || TD_TOUR || TD_ONLINEEDU || TD_READING || TD_OTHER
  536. public static void OnPay(string profileId, string orderId, int amount, string currencyType, string paymentType, string itemId, int itemCount)
  537. {
  538. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  539. {
  540. #if UNITY_ANDROID
  541. if (talkingdataClass != null)
  542. {
  543. talkingdataClass.CallStatic("onPay", profileId, orderId, amount, currencyType, paymentType, itemId, itemCount);
  544. }
  545. #endif
  546. #if UNITY_IPHONE
  547. TDOnPay(profileId, orderId, amount, currencyType, paymentType, itemId, itemCount);
  548. #endif
  549. }
  550. }
  551. #endif
  552. #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_ONLINEEDU
  553. public static void OnChargeBack(string profileId, string orderId, string reason, string type)
  554. {
  555. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  556. {
  557. #if UNITY_ANDROID
  558. if (talkingdataClass != null)
  559. {
  560. talkingdataClass.CallStatic("onChargeBack", profileId, orderId, reason, type);
  561. }
  562. #endif
  563. #if UNITY_IPHONE
  564. TDOnChargeBack(profileId, orderId, reason, type);
  565. #endif
  566. }
  567. }
  568. #endif
  569. #if TD_FINANCE || TD_ONLINEEDU
  570. public static void OnReservation(string profileId, string reservationId, string category, int amount, string term)
  571. {
  572. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  573. {
  574. #if UNITY_ANDROID
  575. if (talkingdataClass != null)
  576. {
  577. talkingdataClass.CallStatic("onReservation", profileId, reservationId, category, amount, term);
  578. }
  579. #endif
  580. #if UNITY_IPHONE
  581. TDOnReservation(profileId, reservationId, category, amount, term);
  582. #endif
  583. }
  584. }
  585. #endif
  586. #if TD_RETAIL || TD_TOUR
  587. public static void OnBooking(string profileId, string bookingId, string category, int amount, string content)
  588. {
  589. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  590. {
  591. #if UNITY_ANDROID
  592. if (talkingdataClass != null)
  593. {
  594. talkingdataClass.CallStatic("onBooking", profileId, bookingId, category, amount, content);
  595. }
  596. #endif
  597. #if UNITY_IPHONE
  598. TDOnBooking(profileId, bookingId, category, amount, content);
  599. #endif
  600. }
  601. }
  602. #endif
  603. #if TD_RETAIL
  604. public static void OnViewItem(string itemId, string category, string name, int unitPrice, Dictionary<string, object> eventValue)
  605. {
  606. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  607. {
  608. #if UNITY_ANDROID
  609. if (talkingdataClass != null)
  610. {
  611. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  612. talkingdataClass.CallStatic("onViewItem", itemId, category, name, unitPrice, eventValueMap);
  613. if (eventValueMap != null)
  614. {
  615. eventValueMap.Dispose();
  616. }
  617. }
  618. #endif
  619. #if UNITY_IPHONE
  620. string eventValueJson = DictionaryToJSONString(eventValue);
  621. TDOnViewItem(itemId, category, name, unitPrice, eventValueJson);
  622. #endif
  623. }
  624. }
  625. public static void OnAddItemToShoppingCart(string itemId, string category, string name, int unitPrice, int amount, Dictionary<string, object> eventValue)
  626. {
  627. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  628. {
  629. #if UNITY_ANDROID
  630. if (talkingdataClass != null)
  631. {
  632. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  633. talkingdataClass.CallStatic("onAddItemToShoppingCart", itemId, category, name, unitPrice, amount, eventValueMap);
  634. if (eventValueMap != null)
  635. {
  636. eventValueMap.Dispose();
  637. }
  638. }
  639. #endif
  640. #if UNITY_IPHONE
  641. string eventValueJson = DictionaryToJSONString(eventValue);
  642. TDOnAddItemToShoppingCart(itemId, category, name, unitPrice, amount, eventValueJson);
  643. #endif
  644. }
  645. }
  646. public static void OnViewShoppingCart(TalkingDataShoppingCart shoppingCart)
  647. {
  648. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  649. {
  650. #if UNITY_ANDROID
  651. if (talkingdataClass != null)
  652. {
  653. talkingdataClass.CallStatic("onViewShoppingCart", shoppingCart.javaObj);
  654. }
  655. #endif
  656. #if UNITY_IPHONE
  657. TDOnViewShoppingCart(shoppingCart.ToString());
  658. #endif
  659. }
  660. }
  661. public static void OnPlaceOrder(TalkingDataOrder order, string profileId, Dictionary<string, object> eventValue)
  662. {
  663. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  664. {
  665. #if UNITY_ANDROID
  666. if (talkingdataClass != null)
  667. {
  668. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  669. talkingdataClass.CallStatic("onPlaceOrder", order.javaObj, profileId, eventValueMap);
  670. if (eventValueMap != null)
  671. {
  672. eventValueMap.Dispose();
  673. }
  674. }
  675. #endif
  676. #if UNITY_IPHONE
  677. string eventValueJson = DictionaryToJSONString(eventValue);
  678. TDOnPlaceOrder(order.ToString(), profileId, eventValueJson);
  679. #endif
  680. }
  681. }
  682. public static void OnOrderPaySucc(TalkingDataOrder order, string paymentType, string profileId)
  683. {
  684. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  685. {
  686. #if UNITY_ANDROID
  687. if (talkingdataClass != null)
  688. {
  689. talkingdataClass.CallStatic("onOrderPaySucc", order.javaObj, paymentType, profileId);
  690. }
  691. #endif
  692. #if UNITY_IPHONE
  693. TDOnOrderPaySucc(order.ToString(), paymentType, profileId);
  694. #endif
  695. }
  696. }
  697. public static void OnCancelOrder(TalkingDataOrder order)
  698. {
  699. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  700. {
  701. #if UNITY_ANDROID
  702. if (talkingdataClass != null)
  703. {
  704. talkingdataClass.CallStatic("onCancelOrder", order.javaObj);
  705. }
  706. #endif
  707. #if UNITY_IPHONE
  708. TDOnCancelOrder(order.ToString());
  709. #endif
  710. }
  711. }
  712. #endif
  713. #if TD_FINANCE
  714. public static void OnCredit(string profileId, int amount, string content)
  715. {
  716. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  717. {
  718. #if UNITY_ANDROID
  719. if (talkingdataClass != null)
  720. {
  721. talkingdataClass.CallStatic("onCredit", profileId, amount, content);
  722. }
  723. #endif
  724. #if UNITY_IPHONE
  725. TDOnCredit(profileId, amount, content);
  726. #endif
  727. }
  728. }
  729. public static void OnTransaction(string profileId, TalkingDataTransaction transaction)
  730. {
  731. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  732. {
  733. #if UNITY_ANDROID
  734. if (talkingdataClass != null)
  735. {
  736. talkingdataClass.CallStatic("onTransaction", profileId, transaction.javaObj);
  737. }
  738. #endif
  739. #if UNITY_IPHONE
  740. TDOnTransaction(profileId, transaction.ToString());
  741. #endif
  742. }
  743. }
  744. #endif
  745. #if TD_GAME
  746. public static void OnCreateRole(string name)
  747. {
  748. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  749. {
  750. #if UNITY_ANDROID
  751. if (talkingdataClass != null)
  752. {
  753. talkingdataClass.CallStatic("onCreateRole", name);
  754. }
  755. #endif
  756. #if UNITY_IPHONE
  757. TDOnCreateRole(name);
  758. #endif
  759. }
  760. }
  761. public static void OnLevelPass(string profileId, string levelId)
  762. {
  763. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  764. {
  765. #if UNITY_ANDROID
  766. if (talkingdataClass != null)
  767. {
  768. talkingdataClass.CallStatic("onLevelPass", profileId, levelId);
  769. }
  770. #endif
  771. #if UNITY_IPHONE
  772. TDOnLevelPass(profileId, levelId);
  773. #endif
  774. }
  775. }
  776. public static void OnGuideFinished(string profileId, string content)
  777. {
  778. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  779. {
  780. #if UNITY_ANDROID
  781. if (talkingdataClass != null)
  782. {
  783. talkingdataClass.CallStatic("onGuideFinished", profileId, content);
  784. }
  785. #endif
  786. #if UNITY_IPHONE
  787. TDOnGuideFinished(profileId, content);
  788. #endif
  789. }
  790. }
  791. #endif
  792. #if TD_ONLINEEDU
  793. public static void OnLearn(string profileId, string course, long begin, int duration)
  794. {
  795. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  796. {
  797. #if UNITY_ANDROID
  798. if (talkingdataClass != null)
  799. {
  800. talkingdataClass.CallStatic("onLearn", profileId, course, begin, duration);
  801. }
  802. #endif
  803. #if UNITY_IPHONE
  804. TDOnLearn(profileId, course, begin, duration);
  805. #endif
  806. }
  807. }
  808. public static void OnPreviewFinished(string profileId, string content)
  809. {
  810. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  811. {
  812. #if UNITY_ANDROID
  813. if (talkingdataClass != null)
  814. {
  815. talkingdataClass.CallStatic("onPreviewFinished", profileId, content);
  816. }
  817. #endif
  818. #if UNITY_IPHONE
  819. TDOnPreviewFinished(profileId, content);
  820. #endif
  821. }
  822. }
  823. #endif
  824. #if TD_READING
  825. public static void OnRead(string profileId, string book, long begin, int duration)
  826. {
  827. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  828. {
  829. #if UNITY_ANDROID
  830. if (talkingdataClass != null)
  831. {
  832. talkingdataClass.CallStatic("onRead", profileId, book, begin, duration);
  833. }
  834. #endif
  835. #if UNITY_IPHONE
  836. TDOnRead(profileId, book, begin, duration);
  837. #endif
  838. }
  839. }
  840. public static void OnFreeFinished(string profileId, string content)
  841. {
  842. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  843. {
  844. #if UNITY_ANDROID
  845. if (talkingdataClass != null)
  846. {
  847. talkingdataClass.CallStatic("onFreeFinished", profileId, content);
  848. }
  849. #endif
  850. #if UNITY_IPHONE
  851. TDOnFreeFinished(profileId, content);
  852. #endif
  853. }
  854. }
  855. #endif
  856. #if TD_GAME || TD_ONLINEEDU
  857. public static void OnAchievementUnlock(string profileId, string achievementId)
  858. {
  859. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  860. {
  861. #if UNITY_ANDROID
  862. if (talkingdataClass != null)
  863. {
  864. talkingdataClass.CallStatic("onAchievementUnlock", profileId, achievementId);
  865. }
  866. #endif
  867. #if UNITY_IPHONE
  868. TDOnAchievementUnlock(profileId, achievementId);
  869. #endif
  870. }
  871. }
  872. #endif
  873. #if TD_FINANCE || TD_TOUR || TD_OTHER
  874. public static void OnBrowse(string profileId, string content, long begin, int duration)
  875. {
  876. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  877. {
  878. #if UNITY_ANDROID
  879. if (talkingdataClass != null)
  880. {
  881. talkingdataClass.CallStatic("onBrowse", profileId, content, begin, duration);
  882. }
  883. #endif
  884. #if UNITY_IPHONE
  885. TDOnBrowse(profileId, content, begin, duration);
  886. #endif
  887. }
  888. }
  889. #endif
  890. #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_OTHER
  891. public static void OnTrialFinished(string profileId, string content)
  892. {
  893. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  894. {
  895. #if UNITY_ANDROID
  896. if (talkingdataClass != null)
  897. {
  898. talkingdataClass.CallStatic("onTrialFinished", profileId, content);
  899. }
  900. #endif
  901. #if UNITY_IPHONE
  902. TDOnTrialFinished(profileId, content);
  903. #endif
  904. }
  905. }
  906. #endif
  907. public static void OnEvent(string eventId, Dictionary<string, object> parameters,
  908. Dictionary<string, object> eventValue)
  909. {
  910. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  911. {
  912. #if UNITY_ANDROID
  913. if (talkingdataClass != null)
  914. {
  915. AndroidJavaObject parametersMap = DictionaryToAndroidMap(parameters);
  916. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  917. talkingdataClass.CallStatic("onEvent", GetCurrentActivity(), eventId, parametersMap, eventValueMap);
  918. if (parametersMap != null)
  919. {
  920. parametersMap.Dispose();
  921. }
  922. if (eventValueMap != null)
  923. {
  924. eventValueMap.Dispose();
  925. }
  926. }
  927. #endif
  928. #if UNITY_IPHONE
  929. string parametersJson = DictionaryToJSONString(parameters);
  930. string eventValueJson = DictionaryToJSONString(eventValue);
  931. TDOnEvent(eventId, parametersJson, eventValueJson);
  932. #endif
  933. }
  934. }
  935. /// <summary>
  936. /// 手动报告错误
  937. /// </summary>
  938. public static void OnError(string message, string stackTrace, LogType logType)
  939. {
  940. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  941. {
  942. #if UNITY_ANDROID
  943. if (talkingdataClass != null)
  944. {
  945. string exceptionMessage = $"{logType}: {message}\n{stackTrace}";
  946. using (AndroidJavaObject javaException = new AndroidJavaObject("java.lang.Exception", exceptionMessage))
  947. {
  948. talkingdataClass.CallStatic("onError", unityPlayerClass, javaException);
  949. }
  950. }
  951. #endif
  952. }
  953. }
  954. /// <summary>
  955. /// 手动报告异常
  956. /// </summary>
  957. public static void ReportException(System.Exception exception)
  958. {
  959. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  960. {
  961. #if UNITY_ANDROID
  962. try
  963. {
  964. using (AndroidJavaObject javaException = new AndroidJavaObject("java.lang.Exception",
  965. $"{exception.Message}\n{exception.StackTrace}"))
  966. {
  967. talkingdataClass.CallStatic("onError", unityPlayerClass, javaException);
  968. }
  969. }
  970. catch (System.Exception e)
  971. {
  972. Debug.LogError("报告异常到TalkingData失败: " + e.Message);
  973. }
  974. #endif
  975. }
  976. }
  977. /// <summary>
  978. /// 设置是否自动捕获未处理异常
  979. /// </summary>
  980. /// <param name="enabled">true:开启, false:关闭</param>
  981. public static void SetReportUncaughtExceptions(bool enabled)
  982. {
  983. try
  984. {
  985. talkingdataClass.CallStatic("setReportUncaughtExceptions", enabled);
  986. Debug.Log($"TalkingData自动异常捕获: {(enabled ? "开启" : "关闭")}");
  987. }
  988. catch (System.Exception e)
  989. {
  990. Debug.LogError("设置TalkingData自动异常捕获失败: " + e.Message);
  991. }
  992. }
  993. public static void SetGlobalKV(string key, object val)
  994. {
  995. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  996. {
  997. #if UNITY_ANDROID
  998. if (talkingdataClass != null)
  999. {
  1000. AndroidJavaObject valObject = typeof(string).IsInstanceOfType(val)
  1001. ? new AndroidJavaObject("java.lang.String", val)
  1002. : new AndroidJavaObject("java.lang.Double", "" + val);
  1003. talkingdataClass.CallStatic("setGlobalKV", key, valObject);
  1004. }
  1005. #endif
  1006. #if UNITY_IPHONE
  1007. if (val is string)
  1008. {
  1009. string tmp = System.Convert.ToString(val);
  1010. TDSetGlobalKV(key, tmp, 0);
  1011. }
  1012. else
  1013. {
  1014. double tmp = System.Convert.ToDouble(val);
  1015. TDSetGlobalKV(key, null, tmp);
  1016. }
  1017. #endif
  1018. }
  1019. }
  1020. public static void RemoveGlobalKV(string key)
  1021. {
  1022. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  1023. {
  1024. #if UNITY_ANDROID
  1025. if (talkingdataClass != null)
  1026. {
  1027. talkingdataClass.CallStatic("removeGlobalKV", key);
  1028. }
  1029. #endif
  1030. #if UNITY_IPHONE
  1031. TDRemoveGlobalKV(key);
  1032. #endif
  1033. }
  1034. }
  1035. }