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
- Google account
- Visual Studio 2012
- 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'.
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.
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.
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'.
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.
Code walk-through
To track the views(or pages) in your app, you need to call 'TrackPageViewAsync' method of 'Tracker' object.
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.
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.
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
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");
}
}
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.
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.
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.
void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
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.
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.