TalkingDataSDK.cs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
  150. object[] args = new object[2];
  151. foreach (KeyValuePair<string, object> kvp in parameters)
  152. {
  153. args[0] = new AndroidJavaObject("java.lang.String", kvp.Key);
  154. args[1] = typeof(string).IsInstanceOfType(kvp.Value)
  155. ? new AndroidJavaObject("java.lang.String", kvp.Value)
  156. : new AndroidJavaObject("java.lang.Double", "" + kvp.Value);
  157. AndroidJNI.CallObjectMethod(map.GetRawObject(), method_Put, AndroidJNIHelper.CreateJNIArgArray(args));
  158. }
  159. }
  160. return map;
  161. }
  162. #endif
  163. #if UNITY_IPHONE
  164. private static string DictionaryToJSONString(Dictionary<string, object> parameters)
  165. {
  166. string json = null;
  167. if (parameters != null && parameters.Count > 0)
  168. {
  169. json = "{";
  170. foreach (KeyValuePair<string, object> kvp in parameters)
  171. {
  172. if (kvp.Value is string)
  173. {
  174. json += "\"" + kvp.Key + "\":\"" + kvp.Value + "\",";
  175. }
  176. else
  177. {
  178. try
  179. {
  180. double tmp = System.Convert.ToDouble(kvp.Value);
  181. json += "\"" + kvp.Key + "\":" + tmp + ",";
  182. }
  183. catch (System.Exception)
  184. {
  185. }
  186. }
  187. }
  188. json = json.TrimEnd(',');
  189. json += "}";
  190. }
  191. return json;
  192. }
  193. #endif
  194. public static void SetConfig(TalkingDataSDKConfig config)
  195. {
  196. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  197. {
  198. #if UNITY_ANDROID
  199. if (talkingdataClass == null)
  200. {
  201. talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
  202. }
  203. talkingdataClass.CallStatic("setConfig", config.javaObj);
  204. #endif
  205. }
  206. }
  207. public static void InitSDK(string appId, string channelId, string custom)
  208. {
  209. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  210. {
  211. Debug.Log("TalkingData Unity SDK.");
  212. #if UNITY_ANDROID
  213. using (AndroidJavaClass dz = new AndroidJavaClass("com.tendcloud.tenddata.dz"))
  214. {
  215. dz.SetStatic("a", 2);
  216. }
  217. if (talkingdataClass == null)
  218. {
  219. talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
  220. }
  221. AndroidJavaObject activity = GetCurrentActivity();
  222. talkingdataClass.CallStatic("initSDK", activity, appId, channelId, custom);
  223. #endif
  224. #if UNITY_IPHONE
  225. TDInitSDK(appId, channelId, custom);
  226. #endif
  227. }
  228. }
  229. public static void StartA()
  230. {
  231. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  232. {
  233. #if UNITY_ANDROID
  234. if (talkingdataClass != null)
  235. {
  236. AndroidJavaObject activity = GetCurrentActivity();
  237. talkingdataClass.CallStatic("startA", activity);
  238. talkingdataClass.CallStatic("onResume", activity);
  239. }
  240. #endif
  241. #if UNITY_IPHONE
  242. TDStartA();
  243. #endif
  244. }
  245. }
  246. public static void OnPause()
  247. {
  248. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  249. {
  250. #if UNITY_ANDROID
  251. if (talkingdataClass != null)
  252. {
  253. talkingdataClass.CallStatic("onPause", GetCurrentActivity());
  254. }
  255. #endif
  256. }
  257. }
  258. public static void BackgroundSessionEnabled()
  259. {
  260. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  261. {
  262. #if UNITY_IPHONE
  263. TDBackgroundSessionEnabled();
  264. #endif
  265. }
  266. }
  267. private static string deviceId = null;
  268. public static string GetDeviceId()
  269. {
  270. if (deviceId == null && Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  271. {
  272. #if UNITY_ANDROID
  273. if (talkingdataClass == null)
  274. {
  275. talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
  276. }
  277. deviceId = talkingdataClass.CallStatic<string>("getDeviceId", GetCurrentActivity());
  278. #endif
  279. #if UNITY_IPHONE
  280. deviceId = TDGetDeviceId();
  281. #endif
  282. }
  283. return deviceId;
  284. }
  285. private static string oaid = null;
  286. public static string GetOAID()
  287. {
  288. if (oaid == null && Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  289. {
  290. #if UNITY_ANDROID
  291. if (talkingdataClass == null)
  292. {
  293. talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
  294. }
  295. oaid = talkingdataClass.CallStatic<string>("getOAID", GetCurrentActivity());
  296. #endif
  297. }
  298. return oaid;
  299. }
  300. public static void SetVerboseLogDisable()
  301. {
  302. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  303. {
  304. #if UNITY_ANDROID
  305. if (talkingdataClass == null)
  306. {
  307. talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
  308. }
  309. talkingdataClass.CallStatic("setVerboseLogDisable");
  310. #endif
  311. #if UNITY_IPHONE
  312. TDSetVerboseLogDisable();
  313. #endif
  314. }
  315. }
  316. public static void SetLocation(double latitude, double longitude)
  317. {
  318. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  319. {
  320. #if UNITY_IPHONE
  321. TDSetLocation(latitude, longitude);
  322. #endif
  323. }
  324. }
  325. public static void OnPageBegin(string pageName)
  326. {
  327. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  328. {
  329. #if UNITY_ANDROID
  330. if (talkingdataClass != null)
  331. {
  332. talkingdataClass.CallStatic("onPageBegin", GetCurrentActivity(), pageName);
  333. }
  334. #endif
  335. #if UNITY_IPHONE
  336. TDOnPageBegin(pageName);
  337. #endif
  338. }
  339. }
  340. public static void OnPageEnd(string pageName)
  341. {
  342. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  343. {
  344. #if UNITY_ANDROID
  345. if (talkingdataClass != null)
  346. {
  347. talkingdataClass.CallStatic("onPageEnd", GetCurrentActivity(), pageName);
  348. }
  349. #endif
  350. #if UNITY_IPHONE
  351. TDOnPageEnd(pageName);
  352. #endif
  353. }
  354. }
  355. public static void OnReceiveDeepLink(string url)
  356. {
  357. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  358. {
  359. #if UNITY_ANDROID
  360. if (talkingdataClass != null)
  361. {
  362. talkingdataClass.CallStatic("onReceiveDeepLink", url);
  363. }
  364. #endif
  365. #if UNITY_IPHONE
  366. TDOnReceiveDeepLink(url);
  367. #endif
  368. }
  369. }
  370. public static void OnRegister(string profileId, TalkingDataProfile profile, string invitationCode, Dictionary<string, object> eventValue)
  371. {
  372. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  373. {
  374. #if UNITY_ANDROID
  375. if (talkingdataClass != null)
  376. {
  377. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  378. talkingdataClass.CallStatic("onRegister", profileId, profile.javaObj, invitationCode, eventValueMap);
  379. if (eventValueMap != null)
  380. {
  381. eventValueMap.Dispose();
  382. }
  383. }
  384. #endif
  385. #if UNITY_IPHONE
  386. string eventValueJson = DictionaryToJSONString(eventValue);
  387. TDOnRegister(profileId, profile.ToString(), invitationCode, eventValueJson);
  388. #endif
  389. }
  390. }
  391. public static void OnLogin(string profileId, TalkingDataProfile profile, Dictionary<string, object> eventValue)
  392. {
  393. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  394. {
  395. #if UNITY_ANDROID
  396. if (talkingdataClass != null)
  397. {
  398. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  399. talkingdataClass.CallStatic("onLogin", profileId, profile.javaObj, eventValueMap);
  400. if (eventValueMap != null)
  401. {
  402. eventValueMap.Dispose();
  403. }
  404. }
  405. #endif
  406. #if UNITY_IPHONE
  407. string eventValueJson = DictionaryToJSONString(eventValue);
  408. TDOnLogin(profileId, profile.ToString(), eventValueJson);
  409. #endif
  410. }
  411. }
  412. public static void OnProfileUpdate(TalkingDataProfile profile)
  413. {
  414. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  415. {
  416. #if UNITY_ANDROID
  417. if (talkingdataClass != null)
  418. {
  419. talkingdataClass.CallStatic("onProfileUpdate", profile.javaObj);
  420. }
  421. #endif
  422. #if UNITY_IPHONE
  423. TDOnProfileUpdate(profile.ToString());
  424. #endif
  425. }
  426. }
  427. public static void OnCreateCard(string profileId, string method, string content)
  428. {
  429. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  430. {
  431. #if UNITY_ANDROID
  432. if (talkingdataClass != null)
  433. {
  434. talkingdataClass.CallStatic("onCreateCard", profileId, method, content);
  435. }
  436. #endif
  437. #if UNITY_IPHONE
  438. TDOnCreateCard(profileId, method, content);
  439. #endif
  440. }
  441. }
  442. public static void OnFavorite(string category, string content, Dictionary<string, object> eventValue)
  443. {
  444. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  445. {
  446. #if UNITY_ANDROID
  447. if (talkingdataClass != null)
  448. {
  449. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  450. talkingdataClass.CallStatic("onFavorite", category, content, eventValueMap);
  451. if (eventValueMap != null)
  452. {
  453. eventValueMap.Dispose();
  454. }
  455. }
  456. #endif
  457. #if UNITY_IPHONE
  458. string eventValueJson = DictionaryToJSONString(eventValue);
  459. TDOnFavorite(category, content, eventValueJson);
  460. #endif
  461. }
  462. }
  463. public static void OnShare(string profileId, string content, Dictionary<string, object> eventValue)
  464. {
  465. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  466. {
  467. #if UNITY_ANDROID
  468. if (talkingdataClass != null)
  469. {
  470. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  471. talkingdataClass.CallStatic("onShare", profileId, content, eventValueMap);
  472. if (eventValueMap != null)
  473. {
  474. eventValueMap.Dispose();
  475. }
  476. }
  477. #endif
  478. #if UNITY_IPHONE
  479. string eventValueJson = DictionaryToJSONString(eventValue);
  480. TDOnShare(profileId, content, eventValueJson);
  481. #endif
  482. }
  483. }
  484. public static void OnPunch(string profileId, string punchId)
  485. {
  486. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  487. {
  488. #if UNITY_ANDROID
  489. if (talkingdataClass != null)
  490. {
  491. talkingdataClass.CallStatic("onPunch", profileId, punchId);
  492. }
  493. #endif
  494. #if UNITY_IPHONE
  495. TDOnPunch(profileId, punchId);
  496. #endif
  497. }
  498. }
  499. public static void OnSearch(TalkingDataSearch search)
  500. {
  501. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  502. {
  503. #if UNITY_ANDROID
  504. if (talkingdataClass != null)
  505. {
  506. talkingdataClass.CallStatic("onSearch", search.javaObj);
  507. }
  508. #endif
  509. #if UNITY_IPHONE
  510. TDOnSearch(search.ToString());
  511. #endif
  512. }
  513. }
  514. #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_ONLINEEDU
  515. public static void OnContact(string profileId, string content)
  516. {
  517. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  518. {
  519. #if UNITY_ANDROID
  520. if (talkingdataClass != null)
  521. {
  522. talkingdataClass.CallStatic("onContact", profileId, content);
  523. }
  524. #endif
  525. #if UNITY_IPHONE
  526. TDOnContact(profileId, content);
  527. #endif
  528. }
  529. }
  530. #endif
  531. #if TD_GAME || TD_TOUR || TD_ONLINEEDU || TD_READING || TD_OTHER
  532. public static void OnPay(string profileId, string orderId, int amount, string currencyType, string paymentType, string itemId, int itemCount)
  533. {
  534. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  535. {
  536. #if UNITY_ANDROID
  537. if (talkingdataClass != null)
  538. {
  539. talkingdataClass.CallStatic("onPay", profileId, orderId, amount, currencyType, paymentType, itemId, itemCount);
  540. }
  541. #endif
  542. #if UNITY_IPHONE
  543. TDOnPay(profileId, orderId, amount, currencyType, paymentType, itemId, itemCount);
  544. #endif
  545. }
  546. }
  547. #endif
  548. #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_ONLINEEDU
  549. public static void OnChargeBack(string profileId, string orderId, string reason, string type)
  550. {
  551. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  552. {
  553. #if UNITY_ANDROID
  554. if (talkingdataClass != null)
  555. {
  556. talkingdataClass.CallStatic("onChargeBack", profileId, orderId, reason, type);
  557. }
  558. #endif
  559. #if UNITY_IPHONE
  560. TDOnChargeBack(profileId, orderId, reason, type);
  561. #endif
  562. }
  563. }
  564. #endif
  565. #if TD_FINANCE || TD_ONLINEEDU
  566. public static void OnReservation(string profileId, string reservationId, string category, int amount, string term)
  567. {
  568. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  569. {
  570. #if UNITY_ANDROID
  571. if (talkingdataClass != null)
  572. {
  573. talkingdataClass.CallStatic("onReservation", profileId, reservationId, category, amount, term);
  574. }
  575. #endif
  576. #if UNITY_IPHONE
  577. TDOnReservation(profileId, reservationId, category, amount, term);
  578. #endif
  579. }
  580. }
  581. #endif
  582. #if TD_RETAIL || TD_TOUR
  583. public static void OnBooking(string profileId, string bookingId, string category, int amount, string content)
  584. {
  585. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  586. {
  587. #if UNITY_ANDROID
  588. if (talkingdataClass != null)
  589. {
  590. talkingdataClass.CallStatic("onBooking", profileId, bookingId, category, amount, content);
  591. }
  592. #endif
  593. #if UNITY_IPHONE
  594. TDOnBooking(profileId, bookingId, category, amount, content);
  595. #endif
  596. }
  597. }
  598. #endif
  599. #if TD_RETAIL
  600. public static void OnViewItem(string itemId, string category, string name, int unitPrice, Dictionary<string, object> eventValue)
  601. {
  602. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  603. {
  604. #if UNITY_ANDROID
  605. if (talkingdataClass != null)
  606. {
  607. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  608. talkingdataClass.CallStatic("onViewItem", itemId, category, name, unitPrice, eventValueMap);
  609. if (eventValueMap != null)
  610. {
  611. eventValueMap.Dispose();
  612. }
  613. }
  614. #endif
  615. #if UNITY_IPHONE
  616. string eventValueJson = DictionaryToJSONString(eventValue);
  617. TDOnViewItem(itemId, category, name, unitPrice, eventValueJson);
  618. #endif
  619. }
  620. }
  621. public static void OnAddItemToShoppingCart(string itemId, string category, string name, int unitPrice, int amount, Dictionary<string, object> eventValue)
  622. {
  623. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  624. {
  625. #if UNITY_ANDROID
  626. if (talkingdataClass != null)
  627. {
  628. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  629. talkingdataClass.CallStatic("onAddItemToShoppingCart", itemId, category, name, unitPrice, amount, eventValueMap);
  630. if (eventValueMap != null)
  631. {
  632. eventValueMap.Dispose();
  633. }
  634. }
  635. #endif
  636. #if UNITY_IPHONE
  637. string eventValueJson = DictionaryToJSONString(eventValue);
  638. TDOnAddItemToShoppingCart(itemId, category, name, unitPrice, amount, eventValueJson);
  639. #endif
  640. }
  641. }
  642. public static void OnViewShoppingCart(TalkingDataShoppingCart shoppingCart)
  643. {
  644. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  645. {
  646. #if UNITY_ANDROID
  647. if (talkingdataClass != null)
  648. {
  649. talkingdataClass.CallStatic("onViewShoppingCart", shoppingCart.javaObj);
  650. }
  651. #endif
  652. #if UNITY_IPHONE
  653. TDOnViewShoppingCart(shoppingCart.ToString());
  654. #endif
  655. }
  656. }
  657. public static void OnPlaceOrder(TalkingDataOrder order, string profileId, Dictionary<string, object> eventValue)
  658. {
  659. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  660. {
  661. #if UNITY_ANDROID
  662. if (talkingdataClass != null)
  663. {
  664. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  665. talkingdataClass.CallStatic("onPlaceOrder", order.javaObj, profileId, eventValueMap);
  666. if (eventValueMap != null)
  667. {
  668. eventValueMap.Dispose();
  669. }
  670. }
  671. #endif
  672. #if UNITY_IPHONE
  673. string eventValueJson = DictionaryToJSONString(eventValue);
  674. TDOnPlaceOrder(order.ToString(), profileId, eventValueJson);
  675. #endif
  676. }
  677. }
  678. public static void OnOrderPaySucc(TalkingDataOrder order, string paymentType, string profileId)
  679. {
  680. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  681. {
  682. #if UNITY_ANDROID
  683. if (talkingdataClass != null)
  684. {
  685. talkingdataClass.CallStatic("onOrderPaySucc", order.javaObj, paymentType, profileId);
  686. }
  687. #endif
  688. #if UNITY_IPHONE
  689. TDOnOrderPaySucc(order.ToString(), paymentType, profileId);
  690. #endif
  691. }
  692. }
  693. public static void OnCancelOrder(TalkingDataOrder order)
  694. {
  695. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  696. {
  697. #if UNITY_ANDROID
  698. if (talkingdataClass != null)
  699. {
  700. talkingdataClass.CallStatic("onCancelOrder", order.javaObj);
  701. }
  702. #endif
  703. #if UNITY_IPHONE
  704. TDOnCancelOrder(order.ToString());
  705. #endif
  706. }
  707. }
  708. #endif
  709. #if TD_FINANCE
  710. public static void OnCredit(string profileId, int amount, string content)
  711. {
  712. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  713. {
  714. #if UNITY_ANDROID
  715. if (talkingdataClass != null)
  716. {
  717. talkingdataClass.CallStatic("onCredit", profileId, amount, content);
  718. }
  719. #endif
  720. #if UNITY_IPHONE
  721. TDOnCredit(profileId, amount, content);
  722. #endif
  723. }
  724. }
  725. public static void OnTransaction(string profileId, TalkingDataTransaction transaction)
  726. {
  727. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  728. {
  729. #if UNITY_ANDROID
  730. if (talkingdataClass != null)
  731. {
  732. talkingdataClass.CallStatic("onTransaction", profileId, transaction.javaObj);
  733. }
  734. #endif
  735. #if UNITY_IPHONE
  736. TDOnTransaction(profileId, transaction.ToString());
  737. #endif
  738. }
  739. }
  740. #endif
  741. #if TD_GAME
  742. public static void OnCreateRole(string name)
  743. {
  744. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  745. {
  746. #if UNITY_ANDROID
  747. if (talkingdataClass != null)
  748. {
  749. talkingdataClass.CallStatic("onCreateRole", name);
  750. }
  751. #endif
  752. #if UNITY_IPHONE
  753. TDOnCreateRole(name);
  754. #endif
  755. }
  756. }
  757. public static void OnLevelPass(string profileId, string levelId)
  758. {
  759. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  760. {
  761. #if UNITY_ANDROID
  762. if (talkingdataClass != null)
  763. {
  764. talkingdataClass.CallStatic("onLevelPass", profileId, levelId);
  765. }
  766. #endif
  767. #if UNITY_IPHONE
  768. TDOnLevelPass(profileId, levelId);
  769. #endif
  770. }
  771. }
  772. public static void OnGuideFinished(string profileId, string content)
  773. {
  774. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  775. {
  776. #if UNITY_ANDROID
  777. if (talkingdataClass != null)
  778. {
  779. talkingdataClass.CallStatic("onGuideFinished", profileId, content);
  780. }
  781. #endif
  782. #if UNITY_IPHONE
  783. TDOnGuideFinished(profileId, content);
  784. #endif
  785. }
  786. }
  787. #endif
  788. #if TD_ONLINEEDU
  789. public static void OnLearn(string profileId, string course, long begin, int duration)
  790. {
  791. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  792. {
  793. #if UNITY_ANDROID
  794. if (talkingdataClass != null)
  795. {
  796. talkingdataClass.CallStatic("onLearn", profileId, course, begin, duration);
  797. }
  798. #endif
  799. #if UNITY_IPHONE
  800. TDOnLearn(profileId, course, begin, duration);
  801. #endif
  802. }
  803. }
  804. public static void OnPreviewFinished(string profileId, string content)
  805. {
  806. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  807. {
  808. #if UNITY_ANDROID
  809. if (talkingdataClass != null)
  810. {
  811. talkingdataClass.CallStatic("onPreviewFinished", profileId, content);
  812. }
  813. #endif
  814. #if UNITY_IPHONE
  815. TDOnPreviewFinished(profileId, content);
  816. #endif
  817. }
  818. }
  819. #endif
  820. #if TD_READING
  821. public static void OnRead(string profileId, string book, long begin, int duration)
  822. {
  823. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  824. {
  825. #if UNITY_ANDROID
  826. if (talkingdataClass != null)
  827. {
  828. talkingdataClass.CallStatic("onRead", profileId, book, begin, duration);
  829. }
  830. #endif
  831. #if UNITY_IPHONE
  832. TDOnRead(profileId, book, begin, duration);
  833. #endif
  834. }
  835. }
  836. public static void OnFreeFinished(string profileId, string content)
  837. {
  838. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  839. {
  840. #if UNITY_ANDROID
  841. if (talkingdataClass != null)
  842. {
  843. talkingdataClass.CallStatic("onFreeFinished", profileId, content);
  844. }
  845. #endif
  846. #if UNITY_IPHONE
  847. TDOnFreeFinished(profileId, content);
  848. #endif
  849. }
  850. }
  851. #endif
  852. #if TD_GAME || TD_ONLINEEDU
  853. public static void OnAchievementUnlock(string profileId, string achievementId)
  854. {
  855. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  856. {
  857. #if UNITY_ANDROID
  858. if (talkingdataClass != null)
  859. {
  860. talkingdataClass.CallStatic("onAchievementUnlock", profileId, achievementId);
  861. }
  862. #endif
  863. #if UNITY_IPHONE
  864. TDOnAchievementUnlock(profileId, achievementId);
  865. #endif
  866. }
  867. }
  868. #endif
  869. #if TD_FINANCE || TD_TOUR || TD_OTHER
  870. public static void OnBrowse(string profileId, string content, long begin, int duration)
  871. {
  872. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  873. {
  874. #if UNITY_ANDROID
  875. if (talkingdataClass != null)
  876. {
  877. talkingdataClass.CallStatic("onBrowse", profileId, content, begin, duration);
  878. }
  879. #endif
  880. #if UNITY_IPHONE
  881. TDOnBrowse(profileId, content, begin, duration);
  882. #endif
  883. }
  884. }
  885. #endif
  886. #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_OTHER
  887. public static void OnTrialFinished(string profileId, string content)
  888. {
  889. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  890. {
  891. #if UNITY_ANDROID
  892. if (talkingdataClass != null)
  893. {
  894. talkingdataClass.CallStatic("onTrialFinished", profileId, content);
  895. }
  896. #endif
  897. #if UNITY_IPHONE
  898. TDOnTrialFinished(profileId, content);
  899. #endif
  900. }
  901. }
  902. #endif
  903. public static void OnEvent(string eventId, Dictionary<string, object> parameters, Dictionary<string, object> eventValue)
  904. {
  905. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  906. {
  907. #if UNITY_ANDROID
  908. if (talkingdataClass != null)
  909. {
  910. AndroidJavaObject parametersMap = DictionaryToAndroidMap(parameters);
  911. AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
  912. talkingdataClass.CallStatic("onEvent", GetCurrentActivity(), eventId, parametersMap, eventValueMap);
  913. if (parametersMap != null)
  914. {
  915. parametersMap.Dispose();
  916. }
  917. if (eventValueMap != null)
  918. {
  919. eventValueMap.Dispose();
  920. }
  921. }
  922. #endif
  923. #if UNITY_IPHONE
  924. string parametersJson = DictionaryToJSONString(parameters);
  925. string eventValueJson = DictionaryToJSONString(eventValue);
  926. TDOnEvent(eventId, parametersJson, eventValueJson);
  927. #endif
  928. }
  929. }
  930. public static void SetGlobalKV(string key, object val)
  931. {
  932. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  933. {
  934. #if UNITY_ANDROID
  935. if (talkingdataClass != null)
  936. {
  937. AndroidJavaObject valObject = typeof(string).IsInstanceOfType(val)
  938. ? new AndroidJavaObject("java.lang.String", val)
  939. : new AndroidJavaObject("java.lang.Double", "" + val);
  940. talkingdataClass.CallStatic("setGlobalKV", key, valObject);
  941. }
  942. #endif
  943. #if UNITY_IPHONE
  944. if (val is string)
  945. {
  946. string tmp = System.Convert.ToString(val);
  947. TDSetGlobalKV(key, tmp, 0);
  948. }
  949. else
  950. {
  951. double tmp = System.Convert.ToDouble(val);
  952. TDSetGlobalKV(key, null, tmp);
  953. }
  954. #endif
  955. }
  956. }
  957. public static void RemoveGlobalKV(string key)
  958. {
  959. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  960. {
  961. #if UNITY_ANDROID
  962. if (talkingdataClass != null)
  963. {
  964. talkingdataClass.CallStatic("removeGlobalKV", key);
  965. }
  966. #endif
  967. #if UNITY_IPHONE
  968. TDRemoveGlobalKV(key);
  969. #endif
  970. }
  971. }
  972. }