A separate native (C++/CX) version of the SDK is recommended for Windows 8 JavaScript applications. This getting started guide covers how to add this special version of the Google Analytics SDK to your JavaScript Windows 8 application.
Note: Windows 8 and Windows Phone 8 C++ applications may also want to use this version to avoid the dependency on the CLR.

Getting Started

If you have not already done so, create a Google Analytics account. Be sure to choose "App" instead of "Web Site" when creating your account.
Note: only newer Google Analytics properties created after March 2013 will work with this SDK.
ga_setup.png

Once you have an account and tracking ID you are ready to start using the SDK.

There are four steps to getting started with this version of the SDK:
  1. Update your application manifest
  2. Install the Visual Studio Extension SDK (available from the Downloads page)
  3. Add a reference to the SDK
  4. Change your build configuration
  5. Configure the EasyTracker in code
  6. Call the EasyTracker methods to log important information

After completing these steps, you'll be able to measure the following with Google Analytics:
  • App installations
  • Active users and demographics
  • Screens and user engagement
  • Crashes and exceptions

1. Update your application manifest

The only capability you need to add to your application is the Internet (Client) capability. This capabilities is already selected by default but you should confirm to make sure your app can send data over the internet.
GA_Capabilities_Win8.png

2. Install the Google Analytics SDK vsix installer

Downloading and double clicking on the .visx (Visual Studio Extension SDK) file from the Downloads page
ga_sdk_install.png

3. Add a reference to the Google Analytics SDK

Right click on References in your project and choose "Add Reference...". Under Windows/Extensions, check "Google Analytics SDK" and click OK.
ga_reference.png

4. Change your build configuration.

Set your target platform to x86, x64, or ARM. Because the native version of the Google Analytics SDK is written in unmanaged code (C++), AnyCPU will not work and instead you must build your application for each platform that you wish to support.
ga_buildconfig1.png
ga_buildconfig2.png

5. Configure the EasyTracker in code

First you need to tell the EasyTracker (the main object used to log to Google Analytics) about your app and your Google Analytics property. To do this, simply add a few lines of code to the very beginning of your project (typically in the main function of default.js).
var config = new GoogleAnalytics.EasyTrackerConfig();
config.trackingId = "UA-XXXX-Y";
config.appName = "Test App";
config.appVersion = "1.0.0.0";
GoogleAnalytics.EasyTracker.current.config = config;
There are other settings available to set but at the very least, you should always modify your tracking ID, app name and version. See the analytics.xml parameters reference for the complete list of parameters you can use to configure your implementation.

6. Add EasyTracker methods

Add a single line of code to your app for each event you want to track. Here are examples of the different types of activities you can track in your app:
// track a page view
GoogleAnalytics.EasyTracker.getTracker().sendView("main");
// track a custom event
GoogleAnalytics.EasyTracker.getTracker().sendEvent("test", "userclick", null, 0);
// manually track an exception
GoogleAnalytics.EasyTracker.getTracker().sendException("oops, something went wrong", false);
// track a transaction (in app purchase)
var cost = 1.99;
var costInMicrons = cost * 1000000;
var transaction = new GoogleAnalytics.Transaction("01234", costInMicrons);
var item = new GoogleAnalytics.TransactionItem("myproduct", "My Product", costInMicrons, 1);
transaction.items.push(item);
GoogleAnalytics.EasyTracker.getTracker().sendTransaction(transaction);
// track a social networking interaction
GoogleAnalytics.EasyTracker.getTracker().sendSocial("facebook", "share", "http://googleanalyticssdk.codeplex.com");
// track a timing (how long it takes your app to run a specific task)
GoogleAnalytics.EasyTracker.getTracker().sendTiming(startupTimeInSeconds, "Startup", "MainPage", "Label");

Other recommendations for JavaScript apps

App suspend and resume

Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", function (args) {
    // tell Google Analytics  that we have resumed.
    GoogleAnalytics.EasyTracker.current.onAppResuming();
    // optionally log an event that the app has resumed
    GoogleAnalytics.EasyTracker.getTracker().sendEvent("app", "resume", null, 0);
});

app.oncheckpoint = function (args) {
    app.sessionState.history = nav.history;

    // optionally log an event that the app has suspended
    GoogleAnalytics.EasyTracker.getTracker().sendEvent("app", "suspend", null, 0);
    // make sure all Google Analytics data has been dispatched.
    args.setPromise(GoogleAnalytics.EasyTracker.current.onAppSuspending());
};

Unhandled exception reporting

var isReportingException = false; // flag to let GA dispatch the error before app crashes
app.onerror = function (eventInfo) {
    if (!isReportingException) {
        var error = eventInfo.detail;
        var errorInfo = error.errorMessage + "\n" + error.errorUrl + " (" + error.errorLine + ")"
        GoogleAnalytics.EasyTracker.getTracker().sendException(errorInfo, true);
        isReportingException = true;
        GoogleAnalytics.EasyTracker.current.dispatch().done(function () {
            // once done logging the error, rethrow to resume normal course of action
            throw error;
        });
        return true;
    }
    else return false;
};

Congratulations! Your app is now setup to send data to Google Analytics.

Last edited Jul 30, 2015 at 4:08 PM by timgreenfield, version 10