-
Notifications
You must be signed in to change notification settings - Fork 33
Getting Started
The SDK can be used in any Windows 10 Universal App, including those running on Windows desktop, phone, Xbox One, and HoloLens.
There is also a C# Core library that you can use in Xamarin apps (for Android and iOS) and in desktop apps written in C#, such as Windows Forms or WPF applications.
##Before you begin For using the SDK, you will need:
-
A Google Analytics account and a registered property configured for mobile app tracking.
This Google help topic has instructions on how to create your account and register your property. -
Visual Studio 2015 w/ Update 3 and the Windows SDK for compiling the app to which you are adding tracking.
The SDK ships as two nuget packages, you will need only one. The APIs, object models and features are the same across the packages; you just need to select based on the programming language you are using:
- UWP.SDKforGoogleAnalytics.Managed is for C# projects.
- UWP.SDKforGoogleAnalytics.Native is for C++ and Javascript projects. This SDK can also be consumed from UWP C# apps, but the source is written in C++.
##Adding Google Analytics to your App There are four simple steps required to add analytics to your app:
- Update your manifest to support Internet (Client) capability.
- Install the NuGet package.
- Configure your Analytics Manager
- Create a tracker and send the interactions
Here are the steps, for each supported language:
To add the Internet Client capability to your project, in Solution Explorer, double click in the package.appxmanifest file, tap in the Capabilities tab, and ensure the Internet (Client) Capability is checked
To add a NuGet package reference follow these steps:
-
In Solution Explorer, right click on the references menu item, and select "Manage NuGet Packages" from the drop-down.
-
Using nuget.org as your package source, in the Browse tab enter UWP.SDKforGoogleAnalytics to narrow it down to our pag
-
Select the appropriate package (.Managed for C#, and .Native for C++ and Javascript) and then click Install
AnalyticsManager platform specific infrastructure and global configuration for your trackers. Using AnalyticsManager you can choose your DispatchPeriod (if you want batching) or automatic handling of exceptions, and handling of application lifetime events (like suspend & resume). Here are examples for configuring your manager:
C#
AnalyticsManager.Current.IsDebug = true; //use only for debugging, returns detailed info on hits sent to analytics servers
AnalyticsManager.Current.DispatchPeriod = TimeSpan.Zero; //immediate mode, sends hits immediately
AnalyticsManager.Current.ReportUncaughtExceptions = true; //catch unhandled exceptions and send the details
AnalyticsManager.Current.AutoAppLifetimeMonitoring = true; //handle suspend/resume and empty hit batched hits on suspend
C++
// See property descriptions above in C# section
AnalyticsManager::Current->ReportUncaughtExceptions = true;
Windows::Foundation::TimeSpan dispatchPeriod;
dispatchPeriod.Duration = 30 /*seconds*/ * 10000000 ; /* in nanoseconds*/
AnalyticsManager::Current->DispatchPeriod = dispatchPeriod;
AnalyticsManager::Current->AutoAppLifetimeMonitoring = true;
Javascript
GoogleAnalytics.AnalyticsManager.current.isDebug = true;
GoogleAnalytics.AnalyticsManager.current.reportUncaughtExceptions = true;
GoogleAnalytics.AnalyticsManager.current.autoAppLifetimeMonitoring = true;
You can instantiate a Tracker from the AnalyticsManager singleton; you just need to pass the PropertyId for your mobile app. Here is sample code for each language:
C++
auto tracker = AnalyticsManager::Current->CreateTracker("UA-39959XXX-N");
C#
var tracker = AnalyticsManager.Current.CreateTracker(""UA-39959XXX-N");
Javascript
var tracker = GoogleAnalytics.AnalyticsManager.current.createTracker("UA-39959863-1");
Once you have a tracker, you can send hits to Google Analytics' servers.
You can use the fluent HitBuilder API to create the different types of interactions.
Here are a few basic examples:
C++
// send an event
tracker->Send ( HitBuilder::CreateCustomEvent("test", "userclick", nullptr, 0)->Build());
//Send a screenview
tracker->ScreenName = "Welcome Page";
tracker->Send(HitBuilder::CreateScreenView()->Build());
// Send an exception
tracker->Send(HitBuilder::CreateException("oops, something went wrong", false)->Build());
C#
// send a social interaction:
App.Tracker.Send(HitBuilder.CreateSocialInteraction("facebook", "share", "http://googleanalyticssdk.codeplex.com").Build());
// send a timing event
App.Tracker.Send(HitBuilder.CreateTiming("someaction", "loadtime", TimeSpan.FromSeconds(2)).Build());
Javascript
//send a custom event
tracker.send(GoogleAnalytics.HitBuilder.createCustomEvent("test", "userclick", "", 0).build());
// send a timing
tracker.send(GoogleAnalytics.HitBuilder.createTiming("pageload", "index.html", 10, "").build());
##Next steps
Most of the snippets above come from the samples in the repo. Explore the samples, set some breakpoints, and have fun.
You can also get more familiar with our SDK via our documentation; it is a very quick easy read.
For further learning about all the measurements, checkout Google's documentation on all the measurements. The APIs should be similar and the concepts and parameters are the same.
This project is supported by the .NET Foundation