Xamarin.Forms Analytics Wrapper

Thats a easy to use Xamarin.Forms Google Analytics wrapper library, which warps Google Analytics tracking for iOS and Android to Xamarin.Forms. I developed the lib in my freetime, so I’m eager about your feedback. Feel free to improve my code via a contribution on GitHub.
Nuget package is available at: nuget.org

Setup and Usage

  1. Install the package via nuget into your PCL and platform specific projects.
  2. Get your Analytics-Id (iOS AppDelegate.cs, Android: MainActivity.cs)
  3. Add the registration call to your platform specific main class (see sample).

Example Android implementation of the Init call:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    Forms.Init(this, bundle);
    var gaService = AnalyticsService.GetGASInstance();
    gaService.Init("UA-12345678-9", this, 3);
    gaService.OptOut = false;
}

Example iOS implementation of the Init call:

public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
{
    Forms.Init();
    var gaService = AnalyticsService.GetGASInstance();
    gaService.Init("UA-12345678-9", 3);
    gaService.OptOut = false;
}

 

Usage

Get the service in Xamarin.Forms

var gaService = DependencyService.Get<IAnalyticsService>();

Track Screen

gaService.TrackScreen ("CampainPage",
"http://examplepetstore.com/index.html?" +
"utm_source=direct&utm_medium=" +
"marketing_android" +
"&utm_campaign=capaingn_" +
"&utm_content=email_variation_1",
null,
ProductActions.none,
null,
null,
null,
null
); 

Track Event

gaService.TrackEvent (
new EventData {
EventAction = "Action_name",
EventCategory = "Event Category",
EventLabel = "Event Label",
Id = 1
},
null,
null
); 

Track Exception

gaService.TrackException (
"Exception_name",
false
); 

Track Social

gaService.TrackSocial (
"MySocialNetwork",
"Action_name,
"Target"
); 

Track Timing

gaService.TrackTiming (
"timingCategory",
"timingLabel",
1,
"timingVariable"
); 

More examples on GitHub with the Sample App.
https://github.com/galonga/xamarin-forms-analytics-wrapper