Skip to content

Instantly share code, notes, and snippets.

@Redth
Created May 9, 2013 19:04
Show Gist options
  • Save Redth/5549716 to your computer and use it in GitHub Desktop.
Save Redth/5549716 to your computer and use it in GitHub Desktop.
Flurry analytics for Xamarin.iOS, Xamarin.Android, Windows Phone
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#if MONODROID
using Android.Telephony;
#endif
#if MONOTOUCH
using MonoTouch.Foundation;
#endif
#if MONODROID
using Android.App;
using Android.Content.PM;
using Android.Views;
using Android.OS;
using Android.Hardware;
using Android.Graphics;
using Android.Content;
using Android.Runtime;
using Android.Widget;
#endif
namespace Shield.Common
{
public class Log
{
#if MONODROID
public const string TAG = "AEGIS";
public static void Debug(string msg)
{
#if DEBUG
Android.Util.Log.Debug(TAG, msg);
#endif
}
public static void Error(string msg)
{
Android.Util.Log.Error(TAG, msg);
}
public static void Info(string msg)
{
Android.Util.Log.Info(TAG, msg);
}
#else
public static void Debug(string msg)
{
#if DEBUG
Console.WriteLine(msg);
#endif
}
public static void Error(string msg)
{
Console.Error.WriteLine(msg);
}
public static void Info(string msg)
{
Console.WriteLine(msg);
}
#endif
#if MONOTOUCH
public static void StartAnalytics()
{
//try { FlurryAnalytics.FlurryAnalytics.StartSession("NCRXYFFBYF6V8TP6TCXJ"); }
//catch { }
}
public static void LogAnalyticEvent(string eventName)
{
//try { FlurryAnalytics.FlurryAnalytics.LogEvent(eventName); }
//catch { }
}
public static void LogAnalyticEvent (string eventName, params KeyValuePair<string, string>[] eventParams)
{
/*try
{
var dict = new MonoTouch.Foundation.NSDictionary ();
foreach (var kvp in eventParams) {
dict [new NSString(kvp.Key)] = new NSString(kvp.Value);
}
FlurryAnalytics.FlurryAnalytics.LogEvent (eventName, dict);
}
catch
{
}
*/
//FlurryAnalytics.FlurryAnalytics.LogEvent()
}
public static void LogAnalyticLocation(double lat, double lon, double acc)
{
//try { FlurryAnalytics.FlurryAnalytics.SetLocation(lat, lon, (float)acc, (float)acc); }
//catch {}
}
public static void LogAnalyticUser()
{
/*if (string.IsNullOrEmpty(Settings.Instance.UserId))
return;
try { FlurryAnalytics.FlurryAnalytics.SetUserID(Settings.Instance.UserId); }
catch { }
* */
}
#elif WINDOWS_PHONE
public static void StartAnalytics()
{
FlurryWP7SDK.Api.StartSession("ZWPCT5Z6J4YXYJX8JQTP");
}
public static void LogAnalyticEvent(string eventName, params KeyValuePair<string, string>[] eventParams)
{
var fp = new List<FlurryWP7SDK.Models.Parameter>();
foreach (var kvp in eventParams)
{
fp.Add(new FlurryWP7SDK.Models.Parameter(kvp.Key, kvp.Value));
}
FlurryWP7SDK.Api.LogEvent(eventName, fp);
}
public static void LogAnalyticLocation(double lat, double lon, double acc)
{
//FlurryWP7SDK.Api.SetLocation(lat, lon, (float)acc);
}
public static void LogAnalyticUser()
{
//if (string.IsNullOrEmpty(Settings.Instance.UserId))
// return;
//FlurryWP7SDK.Api.SetUserId(Settings.Instance.UserId);
}
#elif MONODROID
public static void StartAnalytics()
{
//Not used in android
}
public static void LogAnalyticEvent(string eventName)
{
ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurryLogEvent, new JValue(new Java.Lang.String(eventName))));
}
public static void LogAnalyticLocation(double lat, double lon, double acc)
{
//Location auto set in android
}
public static void LogAnalyticUser(string userId)
{
//ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurrySetUserId, new JValue(new Java.Lang.String(userId))));
}
static IntPtr _flurryClass;
static IntPtr _flurryOnStartSession;
static IntPtr _flurryOnEndSession;
static IntPtr _flurryLogEvent;
static IntPtr _flurrySetUserId;
static Log()
{
_flurryClass = JNIEnv.FindClass("com/flurry/android/FlurryAgent");
_flurryOnStartSession = JNIEnv.GetStaticMethodID(_flurryClass, "onStartSession",
"(Landroid/content/Context;Ljava/lang/String;)V");
_flurryOnEndSession = JNIEnv.GetStaticMethodID(_flurryClass, "onEndSession", "(Landroid/content/Context;)V");
_flurryLogEvent = JNIEnv.GetStaticMethodID(_flurryClass, "logEvent", "(Ljava/lang/String;)V");
_flurrySetUserId = JNIEnv.GetStaticMethodID(_flurryClass, "setUserId", "(Ljava/lang/String;)V");
}
public static void OnStartActivity(Activity activity)
{
ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurryOnStartSession, new JValue(activity), new JValue(new Java.Lang.String("FMZCXXTDGX2HHNYCS5QZ"))));
}
public static void OnStopActivity(Activity activity)
{
ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurryOnEndSession, new JValue(activity)));
}
private static void ExceptionSafe(Action action)
{
try
{
action();
}
catch (Exception exception)
{
Log.Error("Flurry JNI: " + exception.ToString());
//UITrace.Trace("Exception seen in calling Flurry through JNI " + exception.ToLongString());
}
}
/*public static string DeviceId(Context context)
{
//var deviceId = Android.Provider.Settings.Secure.GetString(context.ApplicationContext.ContentResolver, Android.Provider.Settings.Secure.AndroidId);
//return deviceId;
}*/
#endif
}
}
@carlinyuen
Copy link

Does this work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment