123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using Prime31;
- using UnityEngine;
- public class FlurryAgent : IDisposable
- {
- private static FlurryAgent _instance;
- public static FlurryAgent Instance
- {
- get
- {
- if (_instance == null) _instance = new FlurryAgent();
- return _instance;
- }
- }
- #if UNITY_IPHONE
- [DllImport("__Internal")]
- private static extern void _flurryStartSession( string apiKey );
- // Starts up your Flurry analytics session. Call on application startup.
- public void onStartSession( string apiKey )
- {
- if( Application.platform == RuntimePlatform.IPhonePlayer )
- _flurryStartSession( apiKey );
- }
- [DllImport("__Internal")]
- private static extern void _flurryLogEvent( string eventName, bool isTimed );
- // 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
- public void logEvent( string eventName, bool isTimed )
- {
- if( Application.platform == RuntimePlatform.IPhonePlayer )
- _flurryLogEvent( eventName, isTimed );
- }
- [DllImport("__Internal")]
- private static extern void _flurryLogEventWithParameters( string eventName, string parameters, bool isTimed );
- // Logs an event with optional key/value pairs
- public void logEvent( string eventName, Dictionary<string,string> parameters, bool isTimed = false)
- {
- if( parameters == null )
- parameters = new Dictionary<string, string>();
- if( Application.platform == RuntimePlatform.IPhonePlayer )
- _flurryLogEventWithParameters( eventName, parameters.toJson(), isTimed );
- }
- public void Dispose()
- {
- //Dispose();
- }
- #elif UNITY_ANDROID
- private AndroidJavaClass cls_FlurryAgent = new AndroidJavaClass("com.flurry.android.FlurryAgent");
- public void onStartSession(string apiKey)
- {
- Debug.Log("****** onStartSession " + apiKey);
- using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- {
- using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
- {
- cls_FlurryAgent.CallStatic("init", obj_Activity, apiKey);
- cls_FlurryAgent.CallStatic("onStartSession", obj_Activity, apiKey);
- }
- }
- }
- public void onEndSession()
- {
- Debug.Log("****** onEndSession ");
- using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- {
- using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
- {
- cls_FlurryAgent.CallStatic("onEndSession", obj_Activity);
- }
- }
- }
- public void logEvent(string eventId)
- {
- Debug.Log("****** log Event " + eventId);
- cls_FlurryAgent.CallStatic<AndroidJavaObject>("logEvent", eventId);
- }
- public void setContinueSessionMillis(long milliseconds)
- {
- cls_FlurryAgent.CallStatic("setContinueSessionMillis", milliseconds);
- }
- public void onError(string errorId, string message, string errorClass)
- {
- cls_FlurryAgent.CallStatic("onError", errorId, message, errorClass);
- }
- public void onPageView()
- {
- cls_FlurryAgent.CallStatic("onPageView");
- }
- public void setLogEnabled(bool enabled)
- {
- cls_FlurryAgent.CallStatic("setLogEnabled", enabled);
- }
- public void setUserID(string userId)
- {
- cls_FlurryAgent.CallStatic("setUserID", userId);
- }
- public void setAge(int age)
- {
- cls_FlurryAgent.CallStatic("setAge", age);
- }
- /*
- // Not working, and I don't need it, so I'm not going to worry about it
- private static AndroidJavaClass cls_FlurryAgentConstants = new AndroidJavaClass("com.flurry.android.FlurryAgent.Constants");
- public enum Gender
- {
- Male,
- Female
- }
-
- public void setGender(Gender gender)
- {
- byte javaGender = (gender == Gender.Male ? cls_FlurryAgentConstants.Get<byte>("MALE") : cls_FlurryAgentConstants.Get<byte>("FEMALE"));
- cls_FlurryAgent.CallStatic("setGender", javaGender);
- }
- */
- public void setReportLocation(bool reportLocation)
- {
- cls_FlurryAgent.CallStatic("setReportLocation", reportLocation);
- }
- public void logEvent(string eventId, Dictionary<string, string> parameters)
- {
- cls_FlurryAgent.CallStatic<AndroidJavaObject>("logEvent", eventId, ConvertHashMap(parameters), false);
- }
- private AndroidJavaObject ConvertHashMap(Dictionary<string, string> dict)
- {
- AndroidJavaObject obj_HashMap = new AndroidJavaObject("java.util.HashMap");
- IntPtr method_Put = AndroidJNIHelper.GetMethodID(obj_HashMap.GetRawClass(), "put",
- "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
- object[] args = new object[2];
- foreach (KeyValuePair<string, string> kvp in dict)
- {
- using (AndroidJavaObject k = new AndroidJavaObject("java.lang.String", kvp.Key))
- {
- using (AndroidJavaObject v = new AndroidJavaObject("java.lang.String", kvp.Value))
- {
- args[0] = k;
- args[1] = v;
- AndroidJNI.CallObjectMethod(obj_HashMap.GetRawObject(), method_Put, AndroidJNIHelper.CreateJNIArgArray(args));
- }
- }
- }
- return obj_HashMap;
- }
- public void Dispose()
- {
- cls_FlurryAgent.Dispose();
- }
- #else
-
- public void onStartSession(string apiKey){}
-
- public void onEndSession(){}
-
- public void logEvent(string eventId){}
-
- public void setContinueSessionMillis(long milliseconds){}
-
- public void onError(string errorId, string message, string errorClass){}
-
- public void onPageView(){}
-
- public void setLogEnabled(bool enabled){}
-
- public void setUserID(string userId){}
-
- public void setAge(int age){}
-
- public void setReportLocation(bool reportLocation){}
-
- public void logEvent(string eventId, Dictionary<string, string> parameters){}
-
- public void Dispose(){}
-
- #endif
- };
|