FlurryAnalyticsTest.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. ///----------------------------------------------
  2. /// Flurry Analytics Plugin
  3. /// Copyright © 2016 Aleksei Kuzin
  4. ///----------------------------------------------
  5. using UnityEngine;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System;
  9. namespace KHD {
  10. public class FlurryAnalyticsTest : MonoBehaviour {
  11. public bool startSessionFromCode = true;
  12. public static FlurryAnalyticsTest Instance;
  13. void Awake()
  14. {
  15. if (Instance != null && Instance != this)
  16. {
  17. Debug.LogWarning("FlurryLogger instance already exists on another gameObject. Destroying this gameObject FlurryLogger");
  18. Destroy(gameObject);
  19. return;
  20. }
  21. Instance = this;
  22. DontDestroyOnLoad(gameObject);
  23. }
  24. private void Start() {
  25. if (!startSessionFromCode) {
  26. return;
  27. }
  28. KHD.FlurryAnalytics.Instance.SetDebugLogEnabled(true);
  29. // Enable data replication to UnityAnalytics.
  30. // KHD.FlurryAnalytics.Instance.replicateDataToUnityAnalytics = true;
  31. // Set custom app version.
  32. // KHD.FlurryAnalytics.Instance.SetAppVersion("1.0.0");
  33. // Track unique user info.
  34. // KHD.FlurryAnalytics.Instance.SetUserId("ioufewjkhjwdfsmnfbweoiufj"/* replace with user unique id */);
  35. // KHD.FlurryAnalytics.Instance.SetGender(KHD.FlurryAnalytics.Gender.Male);
  36. // KHD.FlurryAnalytics.Instance.SetAge(32);
  37. // Enables implicit recording of Apple Store transactions.
  38. #if UNITY_IOS
  39. //KHD.FlurryAnalyticsIOS.SetIAPReportingEnabled(true);
  40. #endif
  41. // Just one line of code to get wide range of metrics like:
  42. // Sessions
  43. // Active Users
  44. // New Users
  45. // Session Length
  46. // Frequency of Use
  47. // Custom User Segments
  48. // User Retention
  49. // Version Adoption
  50. // Devices
  51. // Carriers
  52. // Firmware Versions
  53. KHD.FlurryAnalytics.Instance.StartSession(
  54. "782HFPDK6J57DNZJYS9G", // - replace with your API KEY.
  55. "JCFXFP8WQQRB2Y39JQZR", // - replace with your API KEY.
  56. true);
  57. }
  58. public void GameFinishLevel1(int point)
  59. {
  60. LogFlurryEvent("Game Finish Level 1", "point", point.ToString());
  61. }
  62. public void GameFinishLevel2(int point)
  63. {
  64. LogFlurryEvent("Game Finish Level 2", "point", point.ToString());
  65. }
  66. public void GameFinishLevel3(int point)
  67. {
  68. LogFlurryEvent("Game Finish Level 3", "point", point.ToString());
  69. }
  70. // private void OnGUI() {
  71. // var index = 0;
  72. // if (Button("Log Event", index++)) {
  73. // KHD.FlurryAnalytics.Instance.LogEvent("KHD Sample Event");
  74. // }
  75. // if (Button("Log Event Wit Parameters", index++)) {
  76. // KHD.FlurryAnalytics.Instance.LogEventWithParameters(
  77. // "KHD Sample Event with parameters",
  78. // new Dictionary<string, string>() {
  79. // { "Param1", "Value1" },
  80. // { "Param2", "Value2" },
  81. // { "Param3", "Value3" },
  82. // });
  83. // }
  84. // if (Button("Log Timed Event", index++)) {
  85. // KHD.FlurryAnalytics.Instance.LogEvent("KHD Sample Timed Event New", true);
  86. // }
  87. // if (Button("End Timed Event", index++)) {
  88. // KHD.FlurryAnalytics.Instance.EndTimedEvent("KHD Sample Timed Event New");
  89. // }
  90. //#if UNITY_ANDROID
  91. // if (Button("Log Payment", index++)) {
  92. // var random = new System.Random();
  93. // KHD.FlurryAnalyticsAndroid.LogPayment(
  94. // "Test Payment", "com.khd.testpayment", 1,
  95. // 0.99, "USD",
  96. // // You should use appropriate transactionId. Only for test.
  97. // SystemInfo.deviceUniqueIdentifier + random.Next(),
  98. // null
  99. // );
  100. // }
  101. //#endif
  102. // }
  103. void OnDestroy()
  104. {
  105. #if UNITY_IPHONE
  106. #endif
  107. #if UNITY_ANDROID
  108. //FlurryAgent.Instance.onEndSession();
  109. #endif
  110. }
  111. public void GamePlayed()
  112. {
  113. LogFlurryEvent("Games played");
  114. }
  115. public void RescueMoment()
  116. {
  117. LogFlurryEvent("Rescue moment");
  118. }
  119. public void InviteFriends()
  120. {
  121. LogFlurryEvent("Invite friends");
  122. }
  123. public void GameFinish(int dist, int packs, int score)
  124. {
  125. //LogFlurryEvent("GamesFinish", "meters", dist.ToString(), "packages", packs.ToString(), "score", score.ToString());
  126. LogFlurryEvent("Meters run", "meters", dist.ToString());
  127. LogFlurryEvent("Packages collected", "packages", packs.ToString());
  128. LogFlurryEvent("Score achieved", "score", score.ToString());
  129. }
  130. public void CouponWon()
  131. {
  132. LogFlurryEvent("Coupon won");
  133. }
  134. public void CouponConsumed()
  135. {
  136. LogFlurryEvent("Coupon consumed");
  137. }
  138. public void EndGame()
  139. {
  140. LogFlurryEvent("End Game", "id", SystemInfo.deviceUniqueIdentifier);
  141. }
  142. public void ShowVideoAd()
  143. {
  144. LogFlurryEvent("Show video ad", "id", SystemInfo.deviceUniqueIdentifier);
  145. }
  146. public void ShowFullScreenAd()
  147. {
  148. LogFlurryEvent("Show fullscreen ad", "id", SystemInfo.deviceUniqueIdentifier);
  149. }
  150. public void CapCode()
  151. {
  152. LogFlurryEvent("Cap code");
  153. }
  154. public void LogFlurryEvent(string eventName)
  155. {
  156. Debug.Log(string.Format("<color=yellow>Sending a Flurry event '{0}'</color>", eventName));
  157. #if UNITY_EDITOR
  158. // return;
  159. #endif
  160. var parameters = new Dictionary<string, string>();
  161. parameters.Add("event data", "empty");
  162. KHD.FlurryAnalytics.Instance.LogEventWithParameters(eventName, parameters);
  163. }
  164. public void LogFlurryEvent(string eventName, params string[] keyValues)
  165. {
  166. //Debug.Assert (keyValues.Length % 2 == 0, "Make sure to pass key value pairs");
  167. Debug.Log(string.Format("<color=yellow>Sending a Flurry event '{0}' with the following parameters '{1}'</color>", eventName, string.Join(", ", keyValues)));
  168. #if UNITY_EDITOR
  169. // return;
  170. #endif
  171. var parameters = new Dictionary<string, string>();
  172. for (int i = 0; i < keyValues.Length; i += 2)
  173. {
  174. parameters[keyValues[i]] = keyValues[i + 1];
  175. }
  176. KHD.FlurryAnalytics.Instance.LogEventWithParameters(eventName, parameters);
  177. }
  178. public void SendPhone()
  179. {
  180. LogFlurryEvent("Send phone");
  181. }
  182. public void GameStartLevel1()
  183. {
  184. LogFlurryEvent("Game Start Level 1");
  185. }
  186. public void GameStartLevel2()
  187. {
  188. LogFlurryEvent("Game Start Level 2");
  189. }
  190. public void GameStartLevel3()
  191. {
  192. LogFlurryEvent("Game Start Level 3");
  193. }
  194. //private bool Button(string label, int index) {
  195. // var width = Screen.width * 0.8f;
  196. // var height = Screen.height * 0.07f;
  197. // var rect = new Rect((Screen.width - width) * 0.5f,
  198. // index * height + height * 0.5f,
  199. // width, height);
  200. // return GUI.Button(rect, label);
  201. //}
  202. }
  203. }