TalkingDataSDK.cs 31 KB

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