GameStatistics.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Facebook.Unity;
  5. using Firebase.Analytics;
  6. using Firebase.Extensions;
  7. using UnityEngine;
  8. public static class GameStatistics
  9. {
  10. private const string TalkingDataAppId = "5EF5C3D840224F39A08E85F38214EC0C";
  11. private const string TalkingDataChannelId = "play.google.com";
  12. private const string FirstLoginTimeKey = "GS_FirstLoginTime";
  13. private const string ShowInterAdsTimesKey = "GS_ShowInterAdsTimes";
  14. private const string TotalOnlineTimeKey = "GS_TotalOnlineTime";
  15. private const string HasSend1Day19InterAdsEventKey = "GS_HasSend1Day19InterAdsEvent";
  16. private const string HasSend1Day1500SecondsEventKey = "GS_HasSend1Day1500SecondsEvent";
  17. private static long _firstLoginTime = -1;
  18. private static long FirstLoginTime
  19. {
  20. get
  21. {
  22. if (_firstLoginTime > 0) return _firstLoginTime;
  23. _firstLoginTime = long.Parse(PlayerPrefs.GetString(FirstLoginTimeKey, "0"));
  24. if (_firstLoginTime > 0) return _firstLoginTime;
  25. _firstLoginTime = DateTime.Now.TotalSeconds();
  26. PlayerPrefs.SetString(FirstLoginTimeKey, $"{_firstLoginTime}");
  27. return _firstLoginTime;
  28. }
  29. }
  30. private static bool IsOver1DayFromFirstLoginTime => DateTime.Now.TotalSeconds() - FirstLoginTime > 1 * 24 * 60 * 60;
  31. private static int _showInterAdsTimes = -1;
  32. private static int ShowInterAdsTimes
  33. {
  34. get
  35. {
  36. if (_showInterAdsTimes >= 0) return _showInterAdsTimes;
  37. _showInterAdsTimes = PlayerPrefs.GetInt(ShowInterAdsTimesKey, 0);
  38. return _showInterAdsTimes;
  39. }
  40. set
  41. {
  42. _showInterAdsTimes = value;
  43. PlayerPrefs.SetInt(ShowInterAdsTimesKey, _showInterAdsTimes);
  44. }
  45. }
  46. private static float _currOnlineTime;
  47. private static int _totalOnlineTime = -1;
  48. private static int TotalOnlineTime
  49. {
  50. get
  51. {
  52. if (_totalOnlineTime < 0)
  53. {
  54. _totalOnlineTime = PlayerPrefs.GetInt(TotalOnlineTimeKey, 0);
  55. }
  56. if (_currOnlineTime > 1)
  57. {
  58. _totalOnlineTime += 1;
  59. PlayerPrefs.SetInt(TotalOnlineTimeKey, _totalOnlineTime);
  60. _currOnlineTime -= 1;
  61. }
  62. return _totalOnlineTime + (int) _currOnlineTime;
  63. }
  64. }
  65. private static int _hasSend1Day19InterAdsEvent = -1;
  66. private static bool HasSend1Day19InterAdsEvent
  67. {
  68. get
  69. {
  70. if (_hasSend1Day19InterAdsEvent >= 0) return _hasSend1Day19InterAdsEvent > 0;
  71. _hasSend1Day19InterAdsEvent = PlayerPrefs.GetInt(HasSend1Day19InterAdsEventKey, 0);
  72. return _hasSend1Day19InterAdsEvent > 0;
  73. }
  74. set
  75. {
  76. _hasSend1Day19InterAdsEvent = value ? 1 : 0;
  77. PlayerPrefs.SetInt(HasSend1Day19InterAdsEventKey, _hasSend1Day19InterAdsEvent);
  78. }
  79. }
  80. private static int _hasSend1Day1500SecondsEvent = -1;
  81. private static bool HasSend1Day1500SecondsEvent
  82. {
  83. get
  84. {
  85. if (_hasSend1Day1500SecondsEvent >= 0) return _hasSend1Day1500SecondsEvent > 0;
  86. _hasSend1Day1500SecondsEvent = PlayerPrefs.GetInt(HasSend1Day1500SecondsEventKey, 0);
  87. return _hasSend1Day1500SecondsEvent > 0;
  88. }
  89. set
  90. {
  91. _hasSend1Day1500SecondsEvent = value ? 1 : 0;
  92. PlayerPrefs.SetInt(HasSend1Day1500SecondsEventKey, _hasSend1Day1500SecondsEvent);
  93. }
  94. }
  95. public static void Init()
  96. {
  97. // TalkingData
  98. TalkingDataSDK.SetVerboseLogDisable();
  99. TalkingDataSDK.BackgroundSessionEnabled();
  100. TalkingDataSDK.Init(TalkingDataAppId, TalkingDataChannelId, "");
  101. // Facebook
  102. FB.Init(FBInitCallback, OnHideUnity);
  103. // Firebase
  104. Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
  105. var dependencyStatus = task.Result;
  106. if (dependencyStatus == Firebase.DependencyStatus.Available) {
  107. // Create and hold a reference to your FirebaseApp,
  108. // where app is a Firebase.FirebaseApp property of your application class.
  109. var app = Firebase.FirebaseApp.DefaultInstance;
  110. FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
  111. // Set a flag here to indicate whether Firebase is ready to use by your app.
  112. } else {
  113. UnityEngine.Debug.LogError(System.String.Format(
  114. "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
  115. // Firebase Unity SDK is not safe to use here.
  116. }
  117. });
  118. }
  119. public static void OnUpdate()
  120. {
  121. _currOnlineTime += Time.deltaTime;
  122. Check1Day1500SecondsEvent();
  123. }
  124. public static void OnDestroy()
  125. {
  126. TalkingDataSDK.OnPause();
  127. }
  128. private static void FBInitCallback()
  129. {
  130. if (FB.IsInitialized) {
  131. // Signal an app activation App Event
  132. FB.ActivateApp();
  133. // Continue with Facebook SDK
  134. // ...
  135. } else {
  136. Debug.Log("Failed to Initialize the Facebook SDK");
  137. }
  138. }
  139. private static void OnHideUnity(bool isGameShown)
  140. {
  141. if (!isGameShown) {
  142. // Pause the game - we will need to hide
  143. Time.timeScale = 0;
  144. } else {
  145. // Resume the game - we're getting focus again
  146. Time.timeScale = 1;
  147. }
  148. }
  149. public static void OnLevelSelect(int level)
  150. {
  151. if (level > 200)
  152. {
  153. return;
  154. }
  155. TalkingDataSDK.OnEvent($"LevelSelect_{level}", null);
  156. }
  157. public static void OnLevelStart(int level)
  158. {
  159. if (level > 200)
  160. {
  161. return;
  162. }
  163. TalkingDataSDK.OnEvent($"LevelStart_{level}", null);
  164. }
  165. public static void OnLevelSuccess(int level, int star)
  166. {
  167. if (level > 200)
  168. {
  169. return;
  170. }
  171. TalkingDataSDK.OnEvent($"LevelSuccess_{level}", new Dictionary<string, object> {{"star", $"{star}"}});
  172. }
  173. public static void OnLevelFail(int level)
  174. {
  175. if (level > 200)
  176. {
  177. return;
  178. }
  179. TalkingDataSDK.OnEvent($"LevelFail_{level}", null);
  180. }
  181. public static void OnLevelQuit(int level)
  182. {
  183. // TalkingDataSDK.OnEvent($"LevelFail_{level}", null);
  184. }
  185. public static void OnLevelReplay(int level)
  186. {
  187. // TalkingDataSDK.OnEvent($"LevelFail_{level}", null);
  188. }
  189. public static void OnBannerAdLoaded()
  190. {
  191. TalkingDataSDK.OnEvent($"ShowAd_Banner", null);
  192. }
  193. public static void OnShowInterstitialAd()
  194. {
  195. TalkingDataSDK.OnEvent($"ShowAd_Inter", null);
  196. ShowInterAdsTimes++;
  197. Check1Day19InterAdsEvent();
  198. }
  199. public static void OnShowRewardedAd()
  200. {
  201. TalkingDataSDK.OnEvent($"ShowAd_Reward", null);
  202. }
  203. private static void Check1Day19InterAdsEvent()
  204. {
  205. // Debug.Log($"ShowInterAdsTimes: {ShowInterAdsTimes}, OverFirstLogin: {DateTime.Now.TotalSeconds() - FirstLoginTime}");
  206. if (IsOver1DayFromFirstLoginTime) return;
  207. if (HasSend1Day19InterAdsEvent) return;
  208. if (ShowInterAdsTimes < 19) return;
  209. FirebaseAnalytics.LogEvent("interstitial_ads_19");
  210. HasSend1Day19InterAdsEvent = true;
  211. // Debug.Log($"ShowInterAdsTimes: {ShowInterAdsTimes}, Send Event, HasSend1Day19InterAdsEvent: {HasSend1Day19InterAdsEvent}");
  212. }
  213. private static void Check1Day1500SecondsEvent()
  214. {
  215. // Debug.Log($"TotalOnlineTime: {TotalOnlineTime}");
  216. if (IsOver1DayFromFirstLoginTime) return;
  217. if (HasSend1Day1500SecondsEvent) return;
  218. if (TotalOnlineTime < 1500) return;
  219. FirebaseAnalytics.LogEvent("day1_1500s");
  220. HasSend1Day1500SecondsEvent = true;
  221. }
  222. }