///---------------------------------------------- /// Flurry Analytics Plugin /// Copyright © 2016 Aleksei Kuzin ///---------------------------------------------- using UnityEngine; using System.Collections.Generic; namespace KHD { [AddComponentMenu("")] public class FlurryAnalytics : SingletonCrossSceneAutoCreate { public enum Gender { Female = 0, Male } /// /// Enable/disable replication events to UnityAnalytics. /// public bool replicateDataToUnityAnalytics { get; set; } /// /// On Destroy. /// protected override void OnDestroy() { base.OnDestroy(); #if UNITY_ANDROID FlurryAnalyticsAndroid.Dispose(); #endif } /// /// Start Flurry session. /// /// iOS API key. /// Android API key. public void StartSession(string iOSApiKey, string androidApiKey, bool sendErrorsToFlurry) { #if UNITY_IOS FlurryAnalyticsIOS.StartSession(iOSApiKey, sendErrorsToFlurry); #elif UNITY_ANDROID FlurryAnalyticsAndroid.Init(androidApiKey, sendErrorsToFlurry); FlurryAnalyticsAndroid.OnStartSession(); #endif } /// /// Enable/Disable Flurry SDK debug logs. /// /// By default logs are disabled. /// Should be called before StartSession. /// public void SetDebugLogEnabled(bool enabled) { #if UNITY_IOS FlurryAnalyticsIOS.SetDebugLogEnabled(enabled); #elif UNITY_ANDROID FlurryAnalyticsAndroid.SetLogEnabled(enabled); #endif } /// /// Explicitly set app version.. /// Should be called before StartSession. /// /// App version. public void SetAppVersion(string appVersion) { #if UNITY_IOS FlurryAnalyticsIOS.SetAppVersion(appVersion); #elif UNITY_ANDROID FlurryAnalyticsAndroid.SetVersionName(appVersion); #endif } /// /// Sets the time the app may be in the background before starting a new session upon resume. /// Default value 10 seconds. /// /// Should be called before StartSession. /// public void SetSessionContinueSeconds(int seconds) { #if UNITY_IOS FlurryAnalyticsIOS.SetSessionContinueSeconds(seconds); #elif UNITY_ANDROID FlurryAnalyticsAndroid.SetContinueSessionMillis(seconds * 1000); #endif } /// /// Enables Flurry Pulse. /// Please see https://developer.yahoo.com/flurry-pulse/ for more details. /// /// Should be called before StartSession. /// public void SetPulseEnabled(bool enabled) { #if UNITY_IOS FlurryAnalyticsIOS.SetPulseEnabled(enabled); #elif UNITY_ANDROID FlurryAnalyticsAndroid.SetPulseEnabled(enabled); #endif } /// /// Assign a unique id for a user in your app. /// public void SetUserId(string userId) { #if UNITY_IOS FlurryAnalyticsIOS.SetUserId(userId); #elif UNITY_ANDROID FlurryAnalyticsAndroid.SetUserId(userId); #endif ReplicateUserIdToUnityAnalytics(userId); } /// /// Use this method to capture the age of your user. /// public void SetAge(int age) { #if UNITY_IOS FlurryAnalyticsIOS.SetAge(age); #elif UNITY_ANDROID FlurryAnalyticsAndroid.SetAge(age); #endif } /// /// Set user's gender. /// public void SetGender(FlurryAnalytics.Gender gender) { #if UNITY_IOS FlurryAnalyticsIOS.SetGender(gender); #elif UNITY_ANDROID FlurryAnalyticsAndroid.SetGender(gender); #endif ReplicateUserGenderToUnityAnalytics(gender); } /// /// Records a custom event specified by eventName. /// /// /// Name of the event. For maximum effectiveness, we recommend using a naming scheme /// that can be easily understood by non-technical people in your business domain. /// public void LogEvent(string eventName) { #if UNITY_IOS FlurryAnalyticsIOS.LogEvent(eventName, false); #elif UNITY_ANDROID FlurryAnalyticsAndroid.LogEvent(eventName, false); #endif ReplicateEventToUnityAnalytics(eventName); } /// /// Records a timed event specified by eventName. /// /// /// Name of the event. For maximum effectiveness, we recommend using a naming scheme /// that can be easily understood by non-technical people in your business domain. /// /// If set to true event will be timed. /// Call EndTimedEvent to stop timed event. public void LogEvent(string eventName, bool isTimed) { #if UNITY_IOS FlurryAnalyticsIOS.LogEvent(eventName, isTimed); #elif UNITY_ANDROID FlurryAnalyticsAndroid.LogEvent(eventName, isTimed); #endif ReplicateEventToUnityAnalytics(eventName); } /// /// Records a custom parameterized event specified by eventName with parameters. /// A maximum of 10 parameter names may be associated with any event. /// /// /// Name of the event. For maximum effectiveness, we recommend using a naming scheme /// that can be easily understood by non-technical people in your business domain. /// /// An immutable copy of map containing Name-Value pairs of parameters. public void LogEventWithParameters(string eventName, Dictionary parameters) { #if UNITY_IOS FlurryAnalyticsIOS.LogEventWithParameters(eventName, parameters, false); #elif UNITY_ANDROID FlurryAnalyticsAndroid.LogEventWithParameters(eventName, parameters, false); #endif ReplicateEventToUnityAnalytics(eventName, parameters); } /// /// Records a custom parameterized timed event specified by eventName with parameters. /// A maximum of 10 parameter names may be associated with any event. /// /// /// Name of the event. For maximum effectiveness, we recommend using a naming scheme /// that can be easily understood by non-technical people in your business domain. /// /// An immutable copy of map containing Name-Value pairs of parameters. /// If set to true event will be timed. /// Call EndTimedEvent to stop timed event. public void LogEventWithParameters(string eventName, Dictionary parameters, bool isTimed) { #if UNITY_IOS FlurryAnalyticsIOS.LogEventWithParameters(eventName, parameters, isTimed); #elif UNITY_ANDROID FlurryAnalyticsAndroid.LogEventWithParameters(eventName, parameters, isTimed); #endif ReplicateEventToUnityAnalytics(eventName, parameters); } /// /// Ends a timed event specified by eventName and optionally updates parameters with parameters. /// /// /// Name of the event. For maximum effectiveness, we recommend using a naming scheme /// that can be easily understood by non-technical people in your business domain. /// /// An immutable copy of map containing Name-Value pairs of parameters. public void EndTimedEvent(string eventName, Dictionary parameters = null) { #if UNITY_IOS FlurryAnalyticsIOS.EndTimedEvent(eventName, parameters); #elif UNITY_ANDROID FlurryAnalyticsAndroid.EndTimedEvent(eventName, parameters); #endif } /// /// Replicate user id to Unity Analytics. /// private void ReplicateUserIdToUnityAnalytics(string userId) { if (!replicateDataToUnityAnalytics) { return; } #if UNITY_ANALYTICS UnityEngine.Analytics.Analytics.SetUserId(userId); #else DataReplicationToUnityAnalyticsWarning(); #endif } /// /// Replicate user gender to Unity Analytics. /// private void ReplicateUserGenderToUnityAnalytics(Gender gender) { if (!replicateDataToUnityAnalytics) { return; } #if UNITY_ANALYTICS UnityEngine.Analytics.Analytics.SetUserGender(gender == Gender.Male ? UnityEngine.Analytics.Gender.Male : UnityEngine.Analytics.Gender.Female); #else DataReplicationToUnityAnalyticsWarning(); #endif } /// /// Replicate event to Unity Analytics. /// private void ReplicateEventToUnityAnalytics(string eventName, Dictionary parameters = null) { if (!replicateDataToUnityAnalytics) { return; } #if UNITY_ANALYTICS Dictionary eventData = null; if (parameters != null && parameters.Count > 0) { eventData = new Dictionary(); foreach (var pair in parameters) { eventData.Add(pair.Key, pair.Value); } } UnityEngine.Analytics.Analytics.CustomEvent(eventName, eventData); #else DataReplicationToUnityAnalyticsWarning(); #endif } /// /// Data replication warning message. /// private void DataReplicationToUnityAnalyticsWarning() { #if (UNITY_5_2 || UNITY_5_3_OR_NEWER) Debug.LogWarning("Unity Analytics is disabled, please turn off data replication." + "To enable Unity Analytics please use this guide " + "http://docs.unity3d.com/Manual/UnityAnalyticsOverview.html"); #else Debug.LogWarning("Data replication to Unity Analytics is only supported for Unity 5.2 or newer"); #endif } } }