Monday, December 14, 2015

How to make "Google Analytics SDK for Windows and Windows Phone" work with Windows 10 Javascript application.






 In past day I had to implement Google Analytics (GA) into Universal Windows 10 app written in HTML5/Javascript. Due to security restrictions it is not possible to add it in standard way - loading external Javascript libraries. I searched internet and found "Google Analytics SDK for Windows and Windows Phone" SDK at Codeplex. Unfortunately, making it work took me some time, so in this article I will share my findings.


Google Analytics SDK for Windows and Windows Phone


"Google Analytics SDK for Windows and Windows Phone" is available here. It comes in two flavours:
  • NuGet package written in C#,
  • vsix install package written in C++ (recommended for Javascript and C++)
 As author (timgrenfield) says:
 "The vsix installs a 100% native version (written in C++). The NuGet package is 100% managed (written in C#). While the C# version will work just fine in JS apps, the native version is recommended for JavaScript apps because it removes the dependency on the CLR (Common Language Runtime) and therefore will result in slightly better performance, lower memory usage, and better battery life for apps."
 In case of using NuGet version you can use "Any CPU" as architecture. In case of vsix package you have to to use separate x86, x64 and ARM architectures - "Any CPU" will not work. Three separate architectures result into Windows Store package to be 3 times bigger as it contains your game or app three times (once for every architecture). Fortunately, Windows Store should be clever enough to deliver only the correct one to user, when downloading your app from store.


NuGet package


 Now come the bad news: NuGet nor vsix did not work for me. I am not experienced in NuGet packages, so I would be happy if anyone could update this article if they find working solution. Problem with NuGet was: references under References in Visual Studio Community 2015 (VS) were marked with small yellow warning triangle after installation. Removing them and adding back by hand removed these warnings, but during compilation there were complains about missing win10{} in project.json file (which was missing completely). As I am not NuGet guy, I got lost.


vsix package


 I focused on vsix package, as it is recommended for Javascript apps and it was my case. After vsix installation go to References in Solution Explorer (right click → Add References)


 In Extensions tick Google Analytics SDK. But be sure to tick the right one – in right panel you can see properties, so choose the one with Targets UAP 10.0 (Universal application):


When compiling, be sure to set target architecture to x86, x64 or ARM:


 Now, building for ARM and x86 works, while attempt to build for x64 fails:


 Reason for this is missing dll library. Go to:
c:\Program Files (x86)\Microsoft SDKs\Windows Kits\10\ExtensionSDKs\GoogleAnalytics.Native.SDK.Win10\1.3\Redist\CommonConfiguration\
 (you can get this path if you place mouse cursor over extension name)

And you will find, that x64 directory is missing (it is missing already in vsix package – you can open it with zip and watch):


 Solution is to build missing dll from source:

1) go to https://googleanalyticssdk.codeplex.com/SourceControl/latest and download latest source code.

2) open GoogleAnalytics.VS2015.sln in VS,

3) change build to Release and select Configuration Manager … at the top:


4) change platform for Google.Analytics.Native.Win10 to x64:


5) build Google.Analytics.Native.Win10 from Build menu:


6) to make things more dramatic, there is syntax error in PlatformInfoProvider.cpp – you have to delete the "=" sign and run build again:


7) in your project root, there should be now new directory x64\Release\GoogleAnalytics.Native.Win10\ with compiled dll:


8) take it and copy it into x64 directory where library is missing (c:\Program Files (x86)\Microsoft SDKs\Windows Kits\10\ExtensionSDKs\GoogleAnalytics.Native.SDK.Win10\1.3\Redist\CommonConfiguration\)

 Now, when building package for Windows Store select architectures like this:



Using Google analytics in code


 To use Google Analytics in your Javascript code, initialize it first with config, where most important part is your Google Analytics tracking Id:

    var config = new GoogleAnalytics.EasyTrackerConfig();
    config.trackingId = "UA-XXXXXXXX-Y";
    config.appName = "My app name";
    config.appVersion = "1.0.0.0";
    GoogleAnalytics.EasyTracker.current.config = config;

 Track events and page views like this:

    GoogleAnalytics.EasyTracker.getTracker().sendView("main");
    GoogleAnalytics.EasyTracker.getTracker().sendEvent("category", "action", "label", 0);

 Now you can switch to you Goole Analytics console and check if you see new users, page views and events.







No comments:

Post a Comment