Other platforms: Xamarin, WPF, Silverlight

The Google Analytics SDK for Windows 8 and Windows Phone can also be used on any platform that supports Portable class libraries (PCL). However, because a PCL cannot query system information like screen size or generate a user-agent that indicates what OS is being used, there is a little more work to set it up and start using it in your app. This documentation will guide you through this process.

Architecture

The SDK is divided into 2 components, at the top layer, there is the EasyTracker and underneath it sits the Core. The EasyTracker component contains all the platform specific code and is not available as a PCL. However, the core is a PCL and can be used directly by any platform that supports PCLs. The good news is: you don't need EasyTracker; the core contains everything you need to work with GA. EasyTracker reduces the amount of code you have to write by automatically configuring the core component for the given platform (e.g. screen resolution, automatic exception logging, ...etc).

Using the Core PCL directly in your app

To use the Core component in your app directly without the EasyTracker component, you need to create a TrackerManager object, initialize it with the required platform specific information, and then call TrackerManager.GetTracker() to create an instance of the Tracker object (this is the same object that you would get back from the EasyTracker and the one that your app calls to log to GA).

var trackerManager = new TrackerManager(new PlatformInfoProvider()
{
    AnonymousClientId = myUserId,
    ScreenResolution = new Dimensions(1920, 1080),
    UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko",
    UserLanguage = "en-us",
    ViewPortResolution = new Dimensions(1920, 1080)
});

tracker = trackerManager.GetTracker("UA-39959863-1"); // saves as default
tracker.AppName = "My app";
tracker.AppVersion = "1.0.0.0";

// Log something to GA
tracker.SendView("MainPage");

The user ID

The user ID should be a unique and anonymous ID for your user that is remembered by your application. It is of type string but GUIDs work great for this purpose. How you chose to generate and store your user ID is the responsibility of your app.

Platform info

The object in the code above "PlatformInfoProvider" is essential to supply the Core with all the parameters needed by GA. There are 2 ways to provide this information:
  • Create an instance of PlatformInfoProvider (a class supplied with the core PCL) and populate. This is what is being done in the code above.
  • Implement the interface IPlatformInfoProvider and supply an instance of this your custom implementation. This can be useful to help isolate and organize code for platform specific scenarios.

NOTE: if you create your own implementation for a given platform and want to share with the community, please contact us. Please do NOT contact us through the comments on this page, CodePlex does not send notifications for page comments and therefore we may miss your offer.

IPlatformInfoProvider Interface

If you choose to implement this interface yourself instead of using the provided PlatformInfoProvider implementation, there are only a few properties and methods to implement. Listed below is the required API with info on how to support each member.

string AnonymousClientId (read/write)
Simply available to store the anonymous user ID. You may choose to implement a way to persist this value the first time it is called for a given user and revive that value for subsequent uses. Or you may choose to off load this responsibility to the application using this object.

Dimensions ScreenResolution (read only)
Provides the native resolution of the device in pixels. Be sure to adjust for scaling if applicable. Note: this typically does not change depending on device orientation.

Dimensions ViewPortResolution (read only)
Provides the resolution of the area that the app is displayed in. In some platforms like WPF and Silverlight, this may be different from the monitor's native resolution. Do not adjust for scaling in this case; logical dimensions are best to use here.

string UserLanguage (read only)
The ISO user language. For example "en-US"

string GetUserAgent()
Allows you to create a custom user agent. Note: this is important and required by Google Analytics to determine what OS and device the user is using.

event EventHandler ViewPortResolutionChanged
An event that can be raised to indicate that the view port resolution has changed.

event EventHandler ScreenResolutionChanged
An event that can be raised to indicate that the screen resolution has changed. This may be useful in multiple monitor situations.

void OnTracking();
Most implementations do not do anything in this method. This method is called just before something is about to be tracked. This can offer you an opportunity to initialize various properties on this object if you want to wait for first use.

int? ScreenColorDepthBits (read only)
Can be used to provide color depth information in bits (e.g. 16 for 16-bit color). Supply null if unable to get this information.

Xamarin

You can follow the same instructions above when working with Xamarin. However, please note the following extra step required to support Xamarin.

Last edited Aug 30, 2014 at 12:36 AM by timgreenfield, version 4