FlurryAgent.cs 6.0 KB

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