Google Analytics for Xamarin

Analytics has long been associated with Google in the web realm, however it isn’t as common in mobile development. While most people might choose Application Insights, RayGun or HockeyApp / Mobile Center, adding Google Analytics can be a good option, if your company is heavily invested in Google Analytics with other assets.

Google Analytics is also different than their Firebase Analytics service. Google released Firebase Analytics, specifically for mobile. This service has less functionality, though is rapidly growing, rather than Google Analytics, but it is designed more for mobile applications. Either Google Analytics or Firebase Analytics is suitable. This post however, will focus solely on Google Analytics.

Create Google Analytics Property

First, we need to go to Google Analytics, and sign up, if you haven’t already. From there, you create a new account, then a property.

When you are creating a property, it is best to select Mobile App, then Google Analytics Services SDK.

You will now be given a tracking ID. Copy this down you will need it shortly.

Xamarin Forms Project

There are numerous components you can choose to have Google Analytics in your Xamarin Forms project. You have most likely come across the components Google Play Services – Analytics and Google Analytics for iOS. These are the Google SDK’s wrapped for use by Xamarin. However, they are tedious to setup and use, especially cross platform, hence I recommend using GoogleAnalytics Plugin for Xamarin.

Download GoogleAnalytics Plugin for Xamarin (ksemenenko.GoogleAnalytics) into all your Xamarin projects, where you will use it.

To intialize the tracker, run this code, in the native platform code, before LoadApplication is called.

GoogleAnalytics.Current.Config.TrackingId = "UA-XXXXXXXX-2";
GoogleAnalytics.Current.Config.AppId = "GASample";
GoogleAnalytics.Current.Config.AppName = "Google Analytics Sample";
GoogleAnalytics.Current.Config.AppVersion = "1.0.0.0";
GoogleAnalytics.Current.InitTracker();

Tracking Pages

Tracking pages is very simple. Just place the following code, in the OnAppearing on each page, or anything that signifies a page has just loaded.

protected override void OnAppearing()
{
    base.OnAppearing();

    GoogleAnalytics.Current.Tracker.SendView("MainPage");
}

If you start the app, then look at your RealTime, dashboard, you will see the page view.

More Tracking

You can track far more than just page views with this plugin.

  1. TrackEvent
  2. TrackException
  3. TrackSocialInteraction
  4. TrackUserTiming
  5. TrackTransaction
  6. TrackTransactionItem

These scenario’s will most likely cover all of your cases, including purchases, specific user times, custom events and exception logging.

Learn More

Have a look at the Project Page, for more details. This plugin does not reference the SDK and keeps your project much lighter. If you want to have the latest the SDK can offer and perform far more advanced functions, you may need to look at the SDK bindings, provided by Xamarin, previously mentioned above.

Microsoft MVP | Xamarin MVP | Xamarin Certified Developer |
Exrin MVVM Framework | Xamarin Forms Developer | Melbourne, Australia

Leave A Comment?