This project has moved and is read-only. For the latest updates, please go here.

Universal apps

Using the Google Analytics SDK in a universal app is easy. Simply add the NuGet the package to both your Windows 8.1 and Windows Phone 8.1 project references and call the APIs in the same way you would normally call them from a Windows Phone or Windows 8 app. More details on setting up your projects to use the Google Analytics SDK can be found here.

The EasyTracker objects used by your Windows Phone vs. Windows app are technically different objects but have the same API. This makes it easy to call from within your shared project code but still retains the usual platform specific benefits such as automatic screen resolution detection and using the appropriate user agent.

Universal class libraries and WinRT components

Sometimes it is important to be able to track events directly from a universal class library that is referenced by your universal app. Version 2.0.11 and later supports this functionality by allowing you to pass a reference to the Tracker object into your universal class library. To support this in your app, first, follow the same instructions above to add the SDK to your main project. Then, additionally add the NuGet the package to universal class library. You project should look something like this:
GAUniversalSolution.png

Next, create a method or property in your universal class library that allows you to pass in an instance of the Tracker object to it. For example:
public sealed class Class1
{
    public GoogleAnalytics.Core.Tracker Tracker { get; set; }
}

From your main projects, set this property by getting a reference to the Tracker object from the EasyTracker. For example:
var c1 = new WindowsRuntimeComponent.Class1();
c1.Tracker = GoogleAnalytics.EasyTracker.GetTracker();

Then when you want to track something from within the EasyTrack component, all you have to do is call the Tracker object that you provided.
public sealed class Class1
{
    public GoogleAnalytics.Core.Tracker Tracker { get; set; }

    public void DoSomething()
    {
        Tracker.SendEvent("category1", "action1", "label1", 0);
    }
}

Note: EasyTracker and all other types outside the GoogleAnalytics.Core namespace will not be available from your universal class library. This is because there are platform specific operations done from these classes that cannot be compiled to the portable subset required by universal class libraries.

Last edited Aug 29, 2014 at 6:53 PM by timgreenfield, version 3