Tag Archives: Windows Phone Google Analytics

Google Analytics tracking implementation in Xamarin …

It all began a while back when my boss tasked me to implement Google Analytics Tracking in one of the enterprise mobile applications we were developing using Xamarin platform, and to be specific the whole app was based on Xamarin Forms platform, harnessing the true cross platform abilities of Xamarin…

Since the first day of the development, it’s been a bit hard but a massive learning curve, due to the lack of articles regarding Google Analytics implementation in Xamarin, specifically Xamarin Forms. And yes, I did go through some back and forth mess, as for the lack of resources… But somehow I managed to successfully implement Google Analytics Tracking in our Xamarin based enterprise application.

Therefore I decided to share the experience and the knowledge I gained, with our fellow Xamarin Developer Community and ultimately save the precious time of a another developer, with the hopes of filling the gap of the lack of articles in Google Analytics implementation in Xamarin. 🙂 So here is the series of articles I wrote in that pursue…

Google Analytics implementation in Xamarin Android –

Let’s add Google Analytics to your Xamarin Android App ^_^

Untitled-1

https://theconfuzedsourcecode.wordpress.com/2015/07/06/lets-add-google-analytics-to-your-xamarin-android-app-_/

Google Analytics implementation in Xamarin iOS –

Let’s add Google Analytics to your Xamarin iOS App ^_^

Untitled-2

https://theconfuzedsourcecode.wordpress.com/2015/07/09/lets-add-google-analytics-to-your-xamarin-ios-app-_/

Google Analytics implementation in Windows Phone –

Let’s add Google Analytics to your Windows Phone App ^_^

Untitled-3

https://theconfuzedsourcecode.wordpress.com/2015/07/17/lets-add-google-analytics-to-your-windows-phone-app-_/

Google Analytics implementation in Xamarin Forms –

Okay Finally, Let’s add Google Analytics to Xamarin Forms …

Untitled-1

https://theconfuzedsourcecode.wordpress.com/2015/07/18/okay-finally-lets-add-google-analytics-to-xamarin-forms/

Well there it goes… Google Analytics tracking implementation in Xamarin ! 🙂

Well there it goes folks, hope this post was helpful for you and prolly saved you a lot of time… 🙂 Please share this among other developers as there aren’t much tutorials on Google Analytics tracking for Xamarin, and you may save another developer’s precious time as well… 😉

Cheers ! Stay Awesome ^_^ !

Advertisements

Let’s add Google Analytics to your Windows Phone App ^_^

After my two previous posts on, Adding Google Analytics for Xamarin Android and Adding Google Analytics for Xamarin iOS the time has come for Windows Phone implementation of Google Analytics… 😉
Well I managed to blog about the major two platforms implementation Android and iOS, so why not blog about the 3rd rival’s implementation, awesome Windows Phone.. ^_^

Untitled-3

I’m not gonna lie to you, when it comes to Google Analytics implementation its pretty much easy thanks to the extended 3rd party library support. Couple of years back I recall looking for a library for this same scenario and I tried out several 3rd party libraries, but none of them served the purpose pretty well. But later I found out this library for Google Analytics implementation on Windows Phone…

https://googleanalyticssdk.codeplex.com/

Untitled5

I have been using this library for all my Windows Phone and Windows Store apps since long time back and it worked perfectly and still serves the purpose very well. So in this article I will be showing you how to implement this library in your project for Google Analytics tracking.

This library has got some good documentation about how to implement it on Windows Phone, with easy to use code samples. But I’m gonna add some decorations to it and explain you how to implement it from the scratch.

Setting up the Library…

In order to set up the library we need to get it from NuGet Package Manager in your Visual Studio… Go ahead Right click on your References and go to Manage NuGet Packages which opens up the NuGet Package Manager..

Untitled2

Select the Online section and search for “GoogleAnalyticsSDK“, hit Enter ! Once the results comes up hit Install…

Untitled3

Let it install… ^_^

Untitled4

Wait for itttttt….. 😛

Untitled5

And you are DONE ! but not quite yet… 😉

We need to configure our Google Analytics Tracker info into the library, for that open up the “analytics.xml” file which was automatically being created by the library we just installed. This xml file is very important because the library will set up itself automatically through the configurations we insert through this file.

Untitled1

Go ahead and add the necessary information, and specially no matter what add your Google Analytics Mobile App Tracking code. If you haven’t acquired Google Analytics mobile app tracking code yet, then here’s some tip, https://support.google.com/analytics/answer/2587086?hl=en

Untitled7

Also this xml file allows us to set up a whole list of configuration for our App tracking, and I’ll show you some of the most important configurations you need to pay attention to.

Untitled8

I have enabled the automated Exceptions tracking, in which case I don’t need to track every single exception that occurs in the application manually, but you can also set it to false on your choice. The rest of the two Properties Advertising Id Collection, Auto Activity Tracking can be enabled or disabled upon your choice.

Untitled9

There up above as you can see I have set the Local dispatch period to “10”, which means all the tracking data will be automatically uploaded to the online GA service in every 10 seconds, this is a good approach considering we shouldn’t exhaust the network resources by uploading tracking information every single second. You can change the value to anything you wish but I recommend you keep it around this value.

POST UPDATE 16/09/2015

If anyone is wondering whether its possible to set up above configuartion from code behind, the answer is, YES ! IT IS POSSIBLE !
You can easily create an EasyTrackerConfig object and set up all the configuration from code behind instead of setting it up in the analytics.xml file as follows where I have configured it in the Initialize() method.

public void Initialize()
{
	var config = new GoogleAnalytics.EasyTrackerConfig();
	config.AppName = "You App Name";
	config.AppVersion = "1.0.0";
	config.TrackingId = "XX-XXXXXX";

	GoogleAnalytics.EasyTracker.Current.Config = config;
}

 

Well that’s the end of setting up the library and GA tracking code… let’s get into coding…

Let the Coding begin…

Just as I have done in my previous articles, I recommend that you keep a separate class for handling all the Google Analytics functionalities. So go ahead and create GAService class. In this class we are gonna implement all the GA functionalities we need to execute.

public class GAService
{
	public void Initialize()
	{
		// No requirement for initializing in Windows Phone, its done by the 
		// library itself using the values given in the analytics.xml file
	}
}

 

Now that we have done with creating the class, next let’s dive into coding the actions… 😉

Trust me its going to be very much simple, thanks to the simplicity of this library we are using…

Let’s track a Page View…

In order to track a Page you may need to add the following method to our GAService class.

public void Track_App_Page(String PageNameToTrack)
{
	GoogleAnalytics.EasyTracker.GetTracker().SendView(PageNameToTrack);
}

 

This library, Google Analytics SDK comes with a special feature called “EasyTracker” which makes it very easy to access GA functionalities. There’s no need of any instantiation,  as the library takes care of all that by itself.

As you can see above we are passing in the Page Name we need to track and we are creating a Page View with the SendView() method,  which is used to create a new a screen view hit. Keep in mind whatever the name you pass in to the method will be recorded in Google Analytics, whenever we need to track a page view, we will be calling this method.

Let’s track an Event…

In order to track any Event, such as a button click, check box selection and son on, you can use the following method. This is very much likely how we did in Xamarin Android, but the syntaxes are a bit different.

In order to track an Event in Google Analytics, we need to have an Event Category, therefore we need to create a small struct along with the type of categories you need to track, this actually depends completely upon your requirement. You can have types of event categories such as. “Sales”, “Order”, “Shopping Cart” and so on… 🙂

public struct GAEventCategory
{
	public static String Category1 { get { return "Category1"; } set { } }
	public static String Category2 { get { return "Category2"; } set { } }
	public static String Category3 { get { return "Category3"; } set { } }
};

 

Now let’s implement the method, for this method we will be passing the above struct values for the GAEventCategory parameter. And for the EventToTrack parameter, you can send whatever event/ event description you want to record in Google Analytics.

public void Track_App_Event(String GAEventCategory, String EventToTrack)
{
	GoogleAnalytics.EasyTracker.GetTracker().SendEvent(GAEventCategory, EventToTrack, "AppEvent", 0);
}

 

As shown above we are using the SendEvent() with the EasyTracker call to create a new event and we are setting up the category and event action as it’s passed in as parameters. Also you can set Labels in Google Analytics for any of your events, but as a general I have set the default label to “AppEvent”, but you can use labeling if you want to generate more complex and detailed tracking, where as Google Analytics is a truly great tool. The library also provides you an extra long parameter at the end, most probably in case you needed to add some other numeric information for the event, but I have set it to 0 by default.

Let’s track an Exception…

Now we all know that exceptions O.o are such a nasty bastard in any application. And its a good habit to keep a track on them. As I have mentioned above,

Untitled8

This Tacker setting captures all the Exceptions that occurs in the application, but keep in mind, this only tracks the unhandled exceptions. Now how about the handled exceptions ? what if we need to keep a track on a certain type of exceptions that’s already being handled in the application.

For such instances you may use the following piece of code… 😉 Just like we did in Xamarin, if you want to record an Exception event in Google Analytics, you need to mention whether it’s a Fatal Exception or not. Therefore you need to pass in a Boolean mentioning whether it’s fatal or not as coded in the below method.

public void Track_App_Exception(String ExceptionMessageToTrack, Boolean isFatalException)
{
	GoogleAnalytics.EasyTracker.GetTracker().SendException(ExceptionMessageToTrack, isFatalException);
}

 

We are using the SendException() method call for this hit in the same way I have used in the other two events above. And when you pass in the Exception message, you can either take the whole ex.StackTrace() or ex.Massage() or both together as a value of your exception in anyway you wish. 🙂 

And now just like we did in Xamarin Android we need to do some decorations to our GAService class…

Just a little decorating…

Now I have unified the access to the Google Analytics tracking by including all the methods in one single class, but in order to access it we need to instantiate right ? That’s why we need to add a little Singleton pattern decoration to our class. Or else you could also make the methods static and straightaway call them, but its your wish.. 🙂

#region Instantition...
private static GAService thisRef;
private GAService()
{
	// no code req'd
}

public static GAService GetGASInstance()
{
	if (thisRef == null)
		// it's ok, we can call this constructor
		thisRef = new GAService();
	return thisRef;
}
#endregion

 

Now that way we can keep a single instance and use it to execute all the functions.

After all that’s done your GAService class should look like this… 🙂

namespace WhateverYourNamespace
{
    public class GAService
    {
        #region Instantition...
        private static GAService thisRef;
        private GAService()
        {
            // no code req'd
        }

        public static GAService GetGASInstance()
        {
            if (thisRef == null)
                // it's ok, we can call this constructor
                thisRef = new GAService();
            return thisRef;
        }
        #endregion

        public void Initialize()
        {
            // No requirement for initializing in Windows Phone, its done by the 
            // library itself using the values given in the analytics.xml file
        }

        public void Track_App_Page(String PageNameToTrack)
        {
            GoogleAnalytics.EasyTracker.GetTracker().SendView(PageNameToTrack);
        }

        public void Track_App_Event(String GAEventCategory, String EventToTrack)
        {
            GoogleAnalytics.EasyTracker.GetTracker().SendEvent(GAEventCategory, EventToTrack, "AppEvent", 0);
        }

        public void Track_App_Exception(String ExceptionMessageToTrack, Boolean isFatalException)
        {
            GoogleAnalytics.EasyTracker.GetTracker().SendException(ExceptionMessageToTrack, isFatalException);
        }
    }
}

 

Finally let’s FIRE IT UP… ! 😉

Well as our GAService class is ready, let’s see how to access it and start tracking our App. 🙂

// Track a page
GAService.GetGASInstance().Track_App_Page("Home Page");

// Track an event
GAService.GetGASInstance().Track_App_Event(GAEventCategory.Category1, "hey ! Category 1 type event ocurred !");   

// Track an Exception
GAService.GetGASInstance().Track_App_Exception(ex.Message, false); 

 

After calling those methods you will be able to see the live tracking information in your Google Anlytics Dashboard online. Just log in to the Real-Time Overview  and you will be able to see everything there. Just keep in mind, as there is a dispatch period of 10 seconds, it might take a while for the actual tracking data to be displayed in the Real-Time tracking view in Google Analytics… 🙂

Well there it goes folks, hope this post was helpful for you and prolly saved you a lot of time… 🙂 Please share this among other developers and you may save another developer’s time as well… 😉

Cheers ! Stay Awesome ^_^ !