Click here to Skip to main content
15,879,008 members
Articles / Programming Languages / C#

Google Analytics Integration with Windows 8

Rate me:
Please Sign up or sign in to vote.
4.50/5 (5 votes)
12 May 2013CPOL4 min read 30.2K   583   19   5
This article shows how to track your windows 8 app in GA

Introduction  

Google Analytics is a service offered by Google that generates detailed statistics & reports about the visits to your website. It is the most widely used website statistics service. However the product is not limited to track visits, but also provides search analysis, event tracking, eCommerce and many more cool features that you might think off. It provides easy to integrate api's that you can use with any web application. The tracking is not limited to websites but also to android and iOS apps.

In this article we'll see how to setup Google Analytics, integration with windows 8 metro app using a nice utility called GoogleAnalyticsTracker and monitoring it in GA. 

Pre-requisite 

  1. Google account  
  2. Visual Studio 2012
  3. Windows 8 

Signup and Configure Google Analytics (GA) 

In the following steps we'll see how to setup GA to track windows store app. 

  • Goto www.google.com/analytics page. 
  • Click on 'Create an Account'. 
  • Provide the login credentials and click on 'Sign in'. 
  • Click on 'Sign up'. 

By default, Web site tracking will be selected. Change this to App. Select the tracking method. Fill up the required fields and click 'Get tracking Id'. 

Image 1

Remember that, the Tracking ID 'UA-XXXXXXXX-XX' is unique per profile and this plays a major role in tacking our site/app. Let's see the structure of GA.

Image 2 

This shows that you can have multiple sub-accounts(properties) under the main account. You can also categorize the sites/apps by creating multiple profiles under each sub-account. Each sub account will have a unique tracking Id and this Id helps you to track the sites/apps under that sub-account.

Image 3

You can manage these things in Admin panel. There is off course a limit to the sites you can track. This link provides you the details about these numbers.

App tracking in GA is currently supported for Android and iOS. There is no api support from Google for Windows store apps. However there is nice utility 'GoogleAnalyticsTracker' available in here, which allows you to track the windows store apps. GoogleAnalyticsTracker is C# library for tracking Google Analytics. You can download it from this link

How do we use GoogleAnalyticsTracker?

Let quickly create a sample windows 8 app using the default Grid template in Visual studio 2012.

Open VS2012. Go to File -> New Project. Select Templates -> Visual C# -> Windows Store. Select Grid App(XAML) template from templates list. Let me name it as 'GAInt1'. Click 'OK'. 

Image 4

Now, we need two packages GoogleAnalyticsTracker.RT and Newtonsoft.Json. We can get the second package from Nuget Package manager.
To install these packages goto Tools -> Library Package Manager -> Package Manager Console. In the console type the following command 

Install-Package Newtonsoft.Json 
Install-Package GoogleAnalyticsTracker.RT 

Alternately you can download GoogleAnalyticsTracker from the this link. Extract it and open the VS solution file. Build GoogleAnalyticsTracker.RT project and refer the GoogleAnalyticsTracker.RT.dll file in you project. 

If the above steps are executed successfully, you should see them in project reference. 

Image 5

Code walk-through 

To track the views(or pages) in your app, you need to call 'TrackPageViewAsync' method of 'Tracker' object.

C#
public Task<TrackingResult> TrackPageViewAsync(string pageTitle, string pageUrl); 

So, whichever page you wanna track, you have to create Tracker instance and call the above method.

C#
using (Tracker tracker = new Tracker("UA-40653799-3", ""))
{
    tracker.TrackPageViewAsync("GroupDetailsPage", "data load uri");
}

If you observe Tacker class, it has two parametric constructors where the first parameter is trackingAccount which is the unique tracking Id in GA and the second parameter is trackingDomain. 

C#
public Tracker(string trackingAccount, string trackingDomain);
public Tracker(string trackingAccount, string trackingDomain, IAnalyticsSession analyticsSession);  

Although trackingDomain looks mandatory in the signature, you can pass empty string to it. The tracking domain need not have value in GA. 

So, once the page is loaded, call the tracking method in it. Lets add the tracking code to the GroupDetailPage.xaml.cs

C#
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
   using (Tracker tracker = new Tracker("UA-40653799-3", ""))
   {
      tracker.TrackPageViewAsync("My API - Create", "api/create");
   }
   // load page data
}

Similarly paste the above code in all the pages.

Now, if you run the app, you should be able to see the tracking in GA. 

Image 6

As mentioned before, the GA is not limited to track pages, it can also track events. The events can be an custom action in your app. It could be as simple as clicking a button, downloading a file etc. So, how to track/monitor them? To track them, we need to call TrackEventAsync method of Tracker class. 

C#
public Task<TrackingResult> TrackEventAsync(string category, string action, string label, int value);

Let's call this method in ItemView_ItemClick event of GroupDetailPage.xaml.cs.

C#
 void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
   // Navigate to the appropriate destination page, configuring the new page
   // by passing required information as a navigation parameter
   var itemId = ((SampleDataItem)e.ClickedItem).UniqueId;
   using (Tracker tracker = new Tracker("UA-40653799-3", ""))
   {
        tracker.TrackEventAsync("GroupDetailPageItem", "Click", itemId, default(int));
   }
   this.Frame.Navigate(typeof(ItemDetailPage), itemId);
}  

Now if you run the app, you will see the events getting tracked in Real-time -> Events -> Overview tab in GA.

Image 7

What's more?

There are many more functionalities provided by GA to track the transactions happening in your eCommerce site. Also, there is a provision for search.  

So, now you can go ahead and try this out in your website or app and monitor your app usage. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to resolve Framework compatibilty issue? Pin
Member 1134765310-Mar-15 1:52
Member 1134765310-Mar-15 1:52 
QuestionQuery regarding storing activities/page view when offline Pin
Sharath C V21-May-13 2:41
professionalSharath C V21-May-13 2:41 
AnswerRe: Query regarding storing activities/page view when offline Pin
pramod.hegde21-May-13 3:02
professionalpramod.hegde21-May-13 3:02 
SuggestionThanks and about tracking property type Pin
Sharath C V21-May-13 2:39
professionalSharath C V21-May-13 2:39 
Nice sample. Thanks Smile | :)

I have one observation: Tracking property should be set to "All Web Site Data". You can give any URL, but when creating the tracker, you can set the URL as "none" or "". "All Mobile App Data" works only with Android and iOS SDK is my conclusion, since this never worked for me with GATracker as well as your sample.
QuestionThanks Pin
Member 669800212-May-13 23:23
Member 669800212-May-13 23:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.