FlurryAgent.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using Prime31;
  5. using UnityEngine;
  6. public class FlurryAgent : IDisposable
  7. {
  8. private static FlurryAgent _instance;
  9. public static FlurryAgent Instance
  10. {
  11. get
  12. {
  13. #if UNITY_IPHONE
  14. if (_instance == null) _instance = new FlurryAgent();
  15. #endif
  16. #if UNITY_ANDROID || UNITY_IPHONE && !UNITY_EDITOR
  17. if (_instance == null) _instance = new FlurryAgent();
  18. #endif
  19. return _instance;
  20. }
  21. }
  22. #if UNITY_IPHONE
  23. [DllImport("__Internal")]
  24. private static extern void _flurryStartSession( string apiKey );
  25. // Starts up your Flurry analytics session. Call on application startup.
  26. public void onStartSession( string apiKey )
  27. {
  28. if( Application.platform == RuntimePlatform.IPhonePlayer )
  29. _flurryStartSession( apiKey );
  30. }
  31. [DllImport("__Internal")]
  32. private static extern void _flurryLogEvent( string eventName, bool isTimed );
  33. // Logs an event to Flurry. If isTimed is true, this will be a timed event and it should be paired with a call to endTimedEvent
  34. public void logEvent( string eventName, bool isTimed )
  35. {
  36. if( Application.platform == RuntimePlatform.IPhonePlayer )
  37. _flurryLogEvent( eventName, isTimed );
  38. }
  39. [DllImport("__Internal")]
  40. private static extern void _flurryLogEventWithParameters( string eventName, string parameters, bool isTimed );
  41. // Logs an event with optional key/value pairs
  42. public void logEvent( string eventName, Dictionary<string,string> parameters, bool isTimed = false)
  43. {
  44. if( parameters == null )
  45. parameters = new Dictionary<string, string>();
  46. if( Application.platform == RuntimePlatform.IPhonePlayer )
  47. _flurryLogEventWithParameters( eventName, parameters.toJson(), isTimed );
  48. }
  49. public void Dispose()
  50. {
  51. //Dispose();
  52. }
  53. #elif UNITY_ANDROID
  54. private AndroidJavaClass cls_FlurryAgent = new AndroidJavaClass("com.flurry.android.FlurryAgent");
  55. public void onStartSession(string apiKey)
  56. {
  57. Debug.Log("****** onStartSession " + apiKey);
  58. using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  59. {
  60. using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
  61. {
  62. cls_FlurryAgent.CallStatic("init", obj_Activity, apiKey);
  63. cls_FlurryAgent.CallStatic("onStartSession", obj_Activity, apiKey);
  64. }
  65. }
  66. }
  67. public void onEndSession()
  68. {
  69. Debug.Log("****** onEndSession ");
  70. using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  71. {
  72. using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
  73. {
  74. cls_FlurryAgent.CallStatic("onEndSession", obj_Activity);
  75. }
  76. }
  77. }
  78. public void logEvent(string eventId)
  79. {
  80. Debug.Log("****** log Event " + eventId);
  81. cls_FlurryAgent.CallStatic<AndroidJavaObject>("logEvent", eventId);
  82. }
  83. public void setContinueSessionMillis(long milliseconds)
  84. {
  85. cls_FlurryAgent.CallStatic("setContinueSessionMillis", milliseconds);
  86. }
  87. public void onError(string errorId, string message, string errorClass)
  88. {
  89. cls_FlurryAgent.CallStatic("onError", errorId, message, errorClass);
  90. }
  91. public void onPageView()
  92. {
  93. cls_FlurryAgent.CallStatic("onPageView");
  94. }
  95. public void setLogEnabled(bool enabled)
  96. {
  97. cls_FlurryAgent.CallStatic("setLogEnabled", enabled);
  98. }
  99. public void setUserID(string userId)
  100. {
  101. cls_FlurryAgent.CallStatic("setUserID", userId);
  102. }
  103. public void setAge(int age)
  104. {
  105. cls_FlurryAgent.CallStatic("setAge", age);
  106. }
  107. /*
  108. // Not working, and I don't need it, so I'm not going to worry about it
  109. private static AndroidJavaClass cls_FlurryAgentConstants = new AndroidJavaClass("com.flurry.android.FlurryAgent.Constants");
  110. public enum Gender
  111. {
  112. Male,
  113. Female
  114. }
  115. public void setGender(Gender gender)
  116. {
  117. byte javaGender = (gender == Gender.Male ? cls_FlurryAgentConstants.Get<byte>("MALE") : cls_FlurryAgentConstants.Get<byte>("FEMALE"));
  118. cls_FlurryAgent.CallStatic("setGender", javaGender);
  119. }
  120. */
  121. public void setReportLocation(bool reportLocation)
  122. {
  123. cls_FlurryAgent.CallStatic("setReportLocation", reportLocation);
  124. }
  125. public void logEvent(string eventId, Dictionary<string, string> parameters)
  126. {
  127. cls_FlurryAgent.CallStatic<AndroidJavaObject>("logEvent", eventId, ConvertHashMap(parameters), false);
  128. }
  129. private AndroidJavaObject ConvertHashMap(Dictionary<string, string> dict)
  130. {
  131. AndroidJavaObject obj_HashMap = new AndroidJavaObject("java.util.HashMap");
  132. IntPtr method_Put = AndroidJNIHelper.GetMethodID(obj_HashMap.GetRawClass(), "put",
  133. "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
  134. object[] args = new object[2];
  135. foreach (KeyValuePair<string, string> kvp in dict)
  136. {
  137. using (AndroidJavaObject k = new AndroidJavaObject("java.lang.String", kvp.Key))
  138. {
  139. using (AndroidJavaObject v = new AndroidJavaObject("java.lang.String", kvp.Value))
  140. {
  141. args[0] = k;
  142. args[1] = v;
  143. AndroidJNI.CallObjectMethod(obj_HashMap.GetRawObject(), method_Put, AndroidJNIHelper.CreateJNIArgArray(args));
  144. }
  145. }
  146. }
  147. return obj_HashMap;
  148. }
  149. public void Dispose()
  150. {
  151. cls_FlurryAgent.Dispose();
  152. }
  153. #else
  154. public void onStartSession(string apiKey){}
  155. public void onEndSession(){}
  156. public void logEvent(string eventId){}
  157. public void setContinueSessionMillis(long milliseconds){}
  158. public void onError(string errorId, string message, string errorClass){}
  159. public void onPageView(){}
  160. public void setLogEnabled(bool enabled){}
  161. public void setUserID(string userId){}
  162. public void setAge(int age){}
  163. public void setReportLocation(bool reportLocation){}
  164. public void logEvent(string eventId, Dictionary<string, string> parameters){}
  165. public void Dispose(){}
  166. #endif
  167. };