Add Google Analytics to your Windows Phone and Windows Store apps in 3 easy steps

Even though there's often little love in the clash of Google and Microsoft titans, which we recently witnessed in the whole 'YouTube API and Windows Phone app' saga, there is a large number of users who use what they think is the best of both worlds. When it comes to websites analytics, the most popular solution today is Google Analytics. How can you use it in your Windows Phone and Windows Store apps to gain insight into who uses your app and how?

I'll try to make this as simple as possible. You only need 3 easy steps and Google Analaytics is up and running in your apps. Of course, I assume that you are familiar with Google Analytics and know what a tracking code is and how to get it. Just in case, as a real programmer, I'll start counting from 0 and show you how to get the tracking code.

0. Get Google Analytics tracking code

If you ever used a Google Analytics code on your websites, you'll know how simple it is. Just go to:

Login with your Google account, go to administration and open the dropdown menu shown on the following screenshot and select 'Create new account'

Google Analytics dropdown menu

Let's say you're developing an app about movies called AppAboutMovies. You probably want to build an app for both Windows Phone and Windows Store, so you first get the tracking code for "AppAboutMovies WP8", by choosing that you wish to track an app and filling in the necessary details:

Google Analytics app setup

You'll then get a tracking ID in the following form: UA-XXXXXXXX-1 which you can use for WP8 app only (and create a new tracking code for your Win8 app), or simply use the same tracking ID for both. Oh and Google will offer you SDKs for Android and iOS only, but don't worry - open source community comes to rescue.

Let's move on to how to add it to your app.

1. Installing Google Analytics SDK for Windows 8 and Windows Phone

Google Analytics SDK for Windows 8 and Windows Phone is availabe on CodePlex

but I'll just go ahead and install it through Nuget in my dummy Windows Phone app. The same procedure described in this post can be applied to Windows 8 app!

Open the Windows Phone app solution and find the Google Analytics SDK with Nuget. Install it. Ta-da, first step done!

Google Analytics on Nuget

2. Set up Google Analytics SDK to work with your tracking code

After the SDK has been installed, it will add a new file to your project called analytics.xml

Google Analytics file in project

Open it, uncomment and edit at least the first 3 entries - add the tracking code, app name and app version.




Session times out after 30 seconds, so if you wish, you can change it under the sessionTimeout property in analytics.xml file to make it longer.

Only one step left!

3. Track events, page views, exceptions etc.

Now you can start tracking events, page views, exceptions etc. The app uses a single tracker throughout your app.

To track page views, just add the following code to the Loaded event handler of the page you wish to track:

void MainPage_Loaded(object sender, RoutedEventArgs e)  

 To track events, just add the following code when the event occurrs:

private void Button_Click(object sender, RoutedEventArgs e)  
    GoogleAnalytics.EasyTracker.GetTracker().SendEvent("Button clicks", "Upload picture", null, 0);

The signature of the method is:

public void SendEvent(string category, string action, string label, int value);  

So it means that in the case of the button click event, I tracked 'Upload picture' action (let's assume that the click on the button uploads a picture somewhere).

Let's also assume that our Button uploading can fail, so an exception can occur. We can track it easily like this:

private void Button_Click(object sender, RoutedEventArgs e)  
    GoogleAnalytics.EasyTracker.GetTracker().SendEvent("Button clicks", "Upload picture", null, 0);
    catch (NotImplementedException exc)
        GoogleAnalytics.EasyTracker.GetTracker().SendException(exc.Message, false);

private void UploadPicture()  
    throw new NotImplementedException();

SendException method takes 2 parameters - the first tells us what happened, and the second whether the exception is fatal.

These are the 3 most interesting parts of your app to track. See the official documentation for more use cases and examples, such as tracking social network sharing, times it takes to run different tasks etc.

What does it look like on Google Analytics portal when you get some data from your app?

You can see info about new users, active users, their approximate location (country), used devices:

Google Analytics overview

Then some info about events and the time users spent on certain parts of your app:

Google Analytics page views

And even the flow how users use your app:

Google Analytics engagement flow

Very interesting!


About Igor Ralic

Software engineer at Microsoft. Running for Office. Passionate about making an impact with great apps & services. Stays close to coffee and away from coriander. Opinions expressed here are my own.