FlurryAnalytics.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. ///----------------------------------------------
  2. /// Flurry Analytics Plugin
  3. /// Copyright © 2016 Aleksei Kuzin
  4. ///----------------------------------------------
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. namespace KHD {
  8. [AddComponentMenu("")]
  9. public class FlurryAnalytics : SingletonCrossSceneAutoCreate<FlurryAnalytics> {
  10. public enum Gender {
  11. Female = 0,
  12. Male
  13. }
  14. /// <summary>
  15. /// Enable/disable replication events to UnityAnalytics.
  16. /// </summary>
  17. public bool replicateDataToUnityAnalytics { get; set; }
  18. /// <summary>
  19. /// On Destroy.
  20. /// </summary>
  21. protected override void OnDestroy() {
  22. base.OnDestroy();
  23. #if UNITY_ANDROID
  24. FlurryAnalyticsAndroid.Dispose();
  25. #endif
  26. }
  27. /// <summary>
  28. /// Start Flurry session.
  29. /// </summary>
  30. /// <param name="iOSApiKey">iOS API key.</param>
  31. /// <param name="androidApiKey">Android API key.</param>
  32. public void StartSession(string iOSApiKey, string androidApiKey, bool sendErrorsToFlurry) {
  33. #if UNITY_IOS
  34. FlurryAnalyticsIOS.StartSession(iOSApiKey, sendErrorsToFlurry);
  35. #elif UNITY_ANDROID
  36. FlurryAnalyticsAndroid.Init(androidApiKey, sendErrorsToFlurry);
  37. FlurryAnalyticsAndroid.OnStartSession();
  38. #endif
  39. }
  40. /// <summary>
  41. /// Enable/Disable Flurry SDK debug logs.
  42. ///
  43. /// By default logs are disabled.
  44. /// Should be called before StartSession.
  45. /// </summary>
  46. public void SetDebugLogEnabled(bool enabled) {
  47. #if UNITY_IOS
  48. FlurryAnalyticsIOS.SetDebugLogEnabled(enabled);
  49. #elif UNITY_ANDROID
  50. FlurryAnalyticsAndroid.SetLogEnabled(enabled);
  51. #endif
  52. }
  53. /// <summary>
  54. /// Explicitly set app version..
  55. /// Should be called before StartSession.
  56. /// </summary>
  57. /// <param name="appVersion">App version.</param>
  58. public void SetAppVersion(string appVersion) {
  59. #if UNITY_IOS
  60. FlurryAnalyticsIOS.SetAppVersion(appVersion);
  61. #elif UNITY_ANDROID
  62. FlurryAnalyticsAndroid.SetVersionName(appVersion);
  63. #endif
  64. }
  65. /// <summary>
  66. /// Sets the time the app may be in the background before starting a new session upon resume.
  67. /// Default value 10 seconds.
  68. ///
  69. /// Should be called before StartSession.
  70. /// </summary>
  71. public void SetSessionContinueSeconds(int seconds) {
  72. #if UNITY_IOS
  73. FlurryAnalyticsIOS.SetSessionContinueSeconds(seconds);
  74. #elif UNITY_ANDROID
  75. FlurryAnalyticsAndroid.SetContinueSessionMillis(seconds * 1000);
  76. #endif
  77. }
  78. /// <summary>
  79. /// Enables Flurry Pulse.
  80. /// Please see https://developer.yahoo.com/flurry-pulse/ for more details.
  81. ///
  82. /// Should be called before StartSession.
  83. /// </summary>
  84. public void SetPulseEnabled(bool enabled) {
  85. #if UNITY_IOS
  86. FlurryAnalyticsIOS.SetPulseEnabled(enabled);
  87. #elif UNITY_ANDROID
  88. FlurryAnalyticsAndroid.SetPulseEnabled(enabled);
  89. #endif
  90. }
  91. /// <summary>
  92. /// Assign a unique id for a user in your app.
  93. /// </summary>
  94. public void SetUserId(string userId) {
  95. #if UNITY_IOS
  96. FlurryAnalyticsIOS.SetUserId(userId);
  97. #elif UNITY_ANDROID
  98. FlurryAnalyticsAndroid.SetUserId(userId);
  99. #endif
  100. ReplicateUserIdToUnityAnalytics(userId);
  101. }
  102. /// <summary>
  103. /// Use this method to capture the age of your user.
  104. /// </summary>
  105. public void SetAge(int age) {
  106. #if UNITY_IOS
  107. FlurryAnalyticsIOS.SetAge(age);
  108. #elif UNITY_ANDROID
  109. FlurryAnalyticsAndroid.SetAge(age);
  110. #endif
  111. }
  112. /// <summary>
  113. /// Set user's gender.
  114. /// </summary>
  115. public void SetGender(FlurryAnalytics.Gender gender) {
  116. #if UNITY_IOS
  117. FlurryAnalyticsIOS.SetGender(gender);
  118. #elif UNITY_ANDROID
  119. FlurryAnalyticsAndroid.SetGender(gender);
  120. #endif
  121. ReplicateUserGenderToUnityAnalytics(gender);
  122. }
  123. /// <summary>
  124. /// Records a custom event specified by eventName.
  125. /// </summary>
  126. /// <param name="eventName">
  127. /// Name of the event. For maximum effectiveness, we recommend using a naming scheme
  128. /// that can be easily understood by non-technical people in your business domain.
  129. /// </param>
  130. public void LogEvent(string eventName) {
  131. #if UNITY_IOS
  132. FlurryAnalyticsIOS.LogEvent(eventName, false);
  133. #elif UNITY_ANDROID
  134. FlurryAnalyticsAndroid.LogEvent(eventName, false);
  135. #endif
  136. ReplicateEventToUnityAnalytics(eventName);
  137. }
  138. /// <summary>
  139. /// Records a timed event specified by eventName.
  140. /// </summary>
  141. /// <param name="eventName">
  142. /// Name of the event. For maximum effectiveness, we recommend using a naming scheme
  143. /// that can be easily understood by non-technical people in your business domain.
  144. /// </param>
  145. /// <param name="isTimed">If set to <c>true</c> event will be timed.
  146. /// Call EndTimedEvent to stop timed event.</param>
  147. public void LogEvent(string eventName, bool isTimed) {
  148. #if UNITY_IOS
  149. FlurryAnalyticsIOS.LogEvent(eventName, isTimed);
  150. #elif UNITY_ANDROID
  151. FlurryAnalyticsAndroid.LogEvent(eventName, isTimed);
  152. #endif
  153. ReplicateEventToUnityAnalytics(eventName);
  154. }
  155. /// <summary>
  156. /// Records a custom parameterized event specified by eventName with parameters.
  157. /// A maximum of 10 parameter names may be associated with any event.
  158. /// </summary>
  159. /// <param name="eventName">
  160. /// Name of the event. For maximum effectiveness, we recommend using a naming scheme
  161. /// that can be easily understood by non-technical people in your business domain.
  162. /// </param>
  163. /// <param name="parameters">An immutable copy of map containing Name-Value pairs of parameters.</param>
  164. public void LogEventWithParameters(string eventName, Dictionary<string, string> parameters) {
  165. #if UNITY_IOS
  166. FlurryAnalyticsIOS.LogEventWithParameters(eventName, parameters, false);
  167. #elif UNITY_ANDROID
  168. FlurryAnalyticsAndroid.LogEventWithParameters(eventName, parameters, false);
  169. #endif
  170. ReplicateEventToUnityAnalytics(eventName, parameters);
  171. }
  172. /// <summary>
  173. /// Records a custom parameterized timed event specified by eventName with parameters.
  174. /// A maximum of 10 parameter names may be associated with any event.
  175. /// </summary>
  176. /// <param name="eventName">
  177. /// Name of the event. For maximum effectiveness, we recommend using a naming scheme
  178. /// that can be easily understood by non-technical people in your business domain.
  179. /// </param>
  180. /// <param name="parameters">An immutable copy of map containing Name-Value pairs of parameters.</param>
  181. /// <param name="isTimed">If set to <c>true</c> event will be timed.
  182. /// Call EndTimedEvent to stop timed event.</param>
  183. public void LogEventWithParameters(string eventName, Dictionary<string, string> parameters, bool isTimed) {
  184. #if UNITY_IOS
  185. FlurryAnalyticsIOS.LogEventWithParameters(eventName, parameters, isTimed);
  186. #elif UNITY_ANDROID
  187. FlurryAnalyticsAndroid.LogEventWithParameters(eventName, parameters, isTimed);
  188. #endif
  189. ReplicateEventToUnityAnalytics(eventName, parameters);
  190. }
  191. /// <summary>
  192. /// Ends a timed event specified by eventName and optionally updates parameters with parameters.
  193. /// </summary>
  194. /// <param name="eventName">
  195. /// Name of the event. For maximum effectiveness, we recommend using a naming scheme
  196. /// that can be easily understood by non-technical people in your business domain.
  197. /// </param>
  198. /// <param name="parameters">An immutable copy of map containing Name-Value pairs of parameters.</param>
  199. public void EndTimedEvent(string eventName, Dictionary<string, string> parameters = null) {
  200. #if UNITY_IOS
  201. FlurryAnalyticsIOS.EndTimedEvent(eventName, parameters);
  202. #elif UNITY_ANDROID
  203. FlurryAnalyticsAndroid.EndTimedEvent(eventName, parameters);
  204. #endif
  205. }
  206. /// <summary>
  207. /// Replicate user id to Unity Analytics.
  208. /// </summary>
  209. private void ReplicateUserIdToUnityAnalytics(string userId) {
  210. if (!replicateDataToUnityAnalytics) {
  211. return;
  212. }
  213. #if UNITY_ANALYTICS
  214. UnityEngine.Analytics.Analytics.SetUserId(userId);
  215. #else
  216. DataReplicationToUnityAnalyticsWarning();
  217. #endif
  218. }
  219. /// <summary>
  220. /// Replicate user gender to Unity Analytics.
  221. /// </summary>
  222. private void ReplicateUserGenderToUnityAnalytics(Gender gender) {
  223. if (!replicateDataToUnityAnalytics) {
  224. return;
  225. }
  226. #if UNITY_ANALYTICS
  227. UnityEngine.Analytics.Analytics.SetUserGender(gender == Gender.Male ?
  228. UnityEngine.Analytics.Gender.Male :
  229. UnityEngine.Analytics.Gender.Female);
  230. #else
  231. DataReplicationToUnityAnalyticsWarning();
  232. #endif
  233. }
  234. /// <summary>
  235. /// Replicate event to Unity Analytics.
  236. /// </summary>
  237. private void ReplicateEventToUnityAnalytics(string eventName, Dictionary<string, string> parameters = null) {
  238. if (!replicateDataToUnityAnalytics) {
  239. return;
  240. }
  241. #if UNITY_ANALYTICS
  242. Dictionary<string, object> eventData = null;
  243. if (parameters != null && parameters.Count > 0) {
  244. eventData = new Dictionary<string, object>();
  245. foreach (var pair in parameters) {
  246. eventData.Add(pair.Key, pair.Value);
  247. }
  248. }
  249. UnityEngine.Analytics.Analytics.CustomEvent(eventName, eventData);
  250. #else
  251. DataReplicationToUnityAnalyticsWarning();
  252. #endif
  253. }
  254. /// <summary>
  255. /// Data replication warning message.
  256. /// </summary>
  257. private void DataReplicationToUnityAnalyticsWarning() {
  258. #if (UNITY_5_2 || UNITY_5_3_OR_NEWER)
  259. Debug.LogWarning("Unity Analytics is disabled, please turn off data replication." +
  260. "To enable Unity Analytics please use this guide " +
  261. "http://docs.unity3d.com/Manual/UnityAnalyticsOverview.html");
  262. #else
  263. Debug.LogWarning("Data replication to Unity Analytics is only supported for Unity 5.2 or newer");
  264. #endif
  265. }
  266. }
  267. }