Overview

The Google Analytics SDK for Windows 8 and Windows Phone uses two classes to manage the global state of the implementation and sending data to Google Analytics servers. EasyTracker wraps these classes to provide simplified configuration and session management:
  • GoogleAnalytics – a singleton that handles the global state of your implementation, including getting Tracker objects.
  • Tracker – the class with which you send data to Google Analytics.

For example, to measure a page view:
using GoogleAnalytics;
...
GoogleAnalytics gaInstance;
Tracker gaTracker;

// Constructor
public MainPage()
{
    InitializeComponent();

    // Get the GoogleAnalytics singleton.
    gaInstance = GoogleAnalytics.Current;

    // Use the GoogleAnalytics singleton to get a Tracker.
    gaTracker = gaInstance.GetTracker("UA-XXXX-Y"); // Placeholder tracking ID.
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    tracker.SendView("Main");
}

Using Multiple Trackers

You can also use multiple trackers in a single implementation, one per unique tracking ID. All trackers share the same global state held by your GoogleAnalytics singleton.

In the following example, a screen view is sent to two separate properties by using two trackers, each with its own unique property ID:
using GoogleAnalytics;
...

GoogleAnalytics gaInstance;
Tracker gaTracker1;
Tracker gaTracker2;

// Constructor
public MainPage()
{
    InitializeComponent();

    // Get the GoogleAnalytics singleton.
    gaInstance = GoogleAnalytics.Current;

    // Use the GoogleAnalytics singleton to get two Trackers with unique property IDs.
    gaTracker1 = gaInstance.GetTracker("UA-XXXX-Y");
    gaTracker2 = gaInstance.GetTracker("UA-XXXX-Z");
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    // Send a screen view to "UA-XXXX-Y" the Activity is displayed to the user.
    gaTracker1.SendView("Main");

    // Send another screen view to the second property, "UA-XXXX-Z".
    gaTracker2.SendView("Home");
}

Keep in mind that automated measurement features provided by EasyTracker will only use the default tracker to make their calls. If you are using these EasyTracker features and want to send data using other trackers, you will need to do so manually.

Default Tracker

Though an implementation may use multiple trackers, globally it has one default tracker. The first Tracker retrieved becomes the default tracker.

To get the default tracker, use:
// Get singleton.
GoogleAnalytics myInstance = GoogleAnalytics.Current;

// Get default tracker.
Tracker myDefault = myInstance.DefaultTracker;
To set the default tracker, use:
// First get a tracker using a new property ID.
Tracker newTracker = myInstance.GetTracker("UA-XXXX-2");

// Then make newTracker the default tracker globally.
myInstance.DefaultTracker = newTracker;

Dispatching

Dispatch settings are managed by the GAServiceManager singleton. To set the dispatch period:
// Set dispatch period to 30 seconds.
GAServiceManager.Current.DispatchPeriod = TimeSpan.FromSeconds(30);
To manually dispatch outside of the scheduled interval:
// Manually dispatch any queued hits. Note: you can also await this call.
GAServiceManager.Current.Dispatch();

Sampling

You can enable client-side sampling to limit the number of hits sent to Google Analytics. If your app has a large number of users or otherwise sends a large volume of data to Google Analytics, enabling sampling will help ensure un-interrupted reporting.

For example, to enable client-side sampling at a rate of 50% via EasyTracker and XML, use the following parameter in your analytics.xml file:
<sampleFrequency>50.0</sampleFrequency>

You can also enable client-side sampling programmatically:
// Get tracker from singleton.
GoogleAnalytics gaInstance = GoogleAnalytics.Current;
Tracker tracker = gaInstance.GetTracker("UA-XXXX-Y"); // Placeholder property ID.

// Set a sample rate of 50%.
tracker.SampleRate = 50.0;

To avoid reporting inconsistencies, each of your app profiles should contain data collected at the same sampling rate. If different versions of your app use different sampling rates, you'll need to configure profile filters using the app version dimension to keep the data from each version of your app separate.

App-level Opt Out

You can enable an app-level opt out flag that will disable Google Analytics across the entire app. Once set, the flag will persist for the life of the app session or until it is reset.

To ask the user if they would like to opt out, use:
// Get singleton.
GoogleAnalytics myInstance = GoogleAnalytics.Current;

// Ask the user if they would like to opt out.
appPreferences.userOptOut = await myInstance.RequestAppOptOut();
// TODO: Persist the opt out preferences.

To set the app-level opt out flag, use:
myInstance.AppOptOut = appPreferences.userOptOut;

Testing and Debugging

The Google Analytics SDK for Windows 8 and Windows Phone provides a debug mode that will print useful information about what data is being sent to Google Analytics in your logs.

To enable debug mode using EasyTracker, add the following to your analytics.xml resource file:
<debug>true</debug>

To enable debug mode programmatically, use:
// Get singleton using application context.
GoogleAnalytics myInstance = GoogleAnalytics.Current;

// Enable debug mode.
myInstance.Debug = true;

The output will be printed to the Output window in visual studio. If you are running a JavaScript application, you will need to set your project debug mode to 'Managed' in order to see this output.

Last edited Aug 11, 2013 at 11:38 PM by timgreenfield, version 6