TalkingDataSDK.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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 (talkingdataClass == null)
  911. return;
  912. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  913. {
  914. #if UNITY_ANDROID
  915. if (talkingdataClass != null)
  916. {
  917. AndroidJavaObject parametersMap = DictionaryToAndroidMap(parameters);
  918. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  919. talkingdataClass.CallStatic("onEvent", GetCurrentActivity(), eventId, parametersMap, eventValueMap);
  920. if (parametersMap != null)
  921. {
  922. parametersMap.Dispose();
  923. }
  924. if (eventValueMap != null)
  925. {
  926. eventValueMap.Dispose();
  927. }
  928. }
  929. #endif
  930. #if UNITY_IPHONE
  931. string parametersJson = DictionaryToJSONString(parameters);
  932. string eventValueJson = DictionaryToJSONString(eventValue);
  933. TDOnEvent(eventId, parametersJson, eventValueJson);
  934. #endif
  935. }
  936. }
  937. /// <summary>
  938. /// 手动报告错误
  939. /// </summary>
  940. public static void OnError(string message, string stackTrace, LogType logType)
  941. {
  942. if (talkingdataClass == null)
  943. return;
  944. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  945. {
  946. #if UNITY_ANDROID
  947. if (talkingdataClass != null)
  948. {
  949. string exceptionMessage = $"{logType}: {message}\n{stackTrace}";
  950. using (AndroidJavaObject javaException = new AndroidJavaObject("java.lang.Exception", exceptionMessage))
  951. {
  952. talkingdataClass.CallStatic("onError", unityPlayerClass, javaException);
  953. }
  954. }
  955. #endif
  956. }
  957. }
  958. /// <summary>
  959. /// 手动报告异常
  960. /// </summary>
  961. public static void ReportException(System.Exception exception)
  962. {
  963. if (talkingdataClass == null)
  964. return;
  965. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  966. {
  967. #if UNITY_ANDROID
  968. try
  969. {
  970. using (AndroidJavaObject javaException = new AndroidJavaObject("java.lang.Exception",
  971. $"{exception.Message}\n{exception.StackTrace}"))
  972. {
  973. talkingdataClass.CallStatic("onError", unityPlayerClass, javaException);
  974. }
  975. }
  976. catch (System.Exception e)
  977. {
  978. Debug.LogError("报告异常到TalkingData失败: " + e.Message);
  979. }
  980. #endif
  981. }
  982. }
  983. /// <summary>
  984. /// 设置是否自动捕获未处理异常
  985. /// </summary>
  986. /// <param name="enabled">true:开启, false:关闭</param>
  987. public static void SetReportUncaughtExceptions(bool enabled)
  988. {
  989. if (talkingdataClass == null)
  990. return;
  991. try
  992. {
  993. talkingdataClass.CallStatic("setReportUncaughtExceptions", enabled);
  994. Debug.Log($"TalkingData自动异常捕获: {(enabled ? "开启" : "关闭")}");
  995. }
  996. catch (System.Exception e)
  997. {
  998. Debug.LogError("设置TalkingData自动异常捕获失败: " + e.Message);
  999. }
  1000. }
  1001. public static void SetGlobalKV(string key, object val)
  1002. {
  1003. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  1004. {
  1005. #if UNITY_ANDROID
  1006. if (talkingdataClass != null)
  1007. {
  1008. AndroidJavaObject valObject = typeof(string).IsInstanceOfType(val)
  1009. ? new AndroidJavaObject("java.lang.String", val)
  1010. : new AndroidJavaObject("java.lang.Double", "" + val);
  1011. talkingdataClass.CallStatic("setGlobalKV", key, valObject);
  1012. }
  1013. #endif
  1014. #if UNITY_IPHONE
  1015. if (val is string)
  1016. {
  1017. string tmp = System.Convert.ToString(val);
  1018. TDSetGlobalKV(key, tmp, 0);
  1019. }
  1020. else
  1021. {
  1022. double tmp = System.Convert.ToDouble(val);
  1023. TDSetGlobalKV(key, null, tmp);
  1024. }
  1025. #endif
  1026. }
  1027. }
  1028. public static void RemoveGlobalKV(string key)
  1029. {
  1030. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  1031. {
  1032. #if UNITY_ANDROID
  1033. if (talkingdataClass != null)
  1034. {
  1035. talkingdataClass.CallStatic("removeGlobalKV", key);
  1036. }
  1037. #endif
  1038. #if UNITY_IPHONE
  1039. TDRemoveGlobalKV(key);
  1040. #endif
  1041. }
  1042. }
  1043. }