mercredi 12 mars 2014

Google Analytics SDK for Windows 8 and Windows Phone

I found a very cool API for you guys to add google analytics to you application , i am using this one for a while now and it works just fine!


1. Update your application manifest

The only capability you need to add to your application is the IDCAPNETWORKING capability for Windows Phone or Internet (Client) capability for Windows 8 store apps. These capabilities are already selected by default but you should confirm to make sure your app can send data over the internet.

On Windows Phone (7 & 8):

GA_Capabilities_WP7.png

On Windows 8 store apps (JavaScript and Xaml):

GA_Capabilities_Win8.png

2. Install the NuGet package

Option A: From Visual Studio Nuget Package Manager

  1. Right click on your project and choose 'Manage NuGet Packages'
  2. Search for "GoogleAnalyticsSDK"
  3. Click 'Install'.
nuget1.png
nuget2.png

Option B: Run the following command in the Package Manager Console

PM> Install-Package GoogleAnalyticsSDK -Pre

3. Modify your analytics.xml file

By default, a file called analytics.xml will be automatically added to the root of your project. This file contains configuration options that you must edit. At the very least, you should always modify your tracking ID, app name and version:
<?xml version="1.0" encoding="utf-8" ?>
<analytics xmlns="http://googleanalyticssdk.codeplex.com/ns/easytracker">
  <!--Replace placeholder ID with your tracking ID-->
  <trackingId>UA-XXXX-Y</trackingId>
  <!--Replace placeholder name with your app name-->
  <appName>TestApp</appName>
  <!--Replace placeholder version with your app version-->
  <appVersion>1.0.0.0</appVersion>
</analytics>
See the analytics.xml parameters reference for the complete list of parameters you can use to configure your implementation.

4. 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)
double cost = 1.99;
long costInMicrons = (long)(cost * 1000000);
var transaction = new GoogleAnalytics.Transaction("01234", costInMicrons);
var item = new GoogleAnalytics.TransactionItem("myproduct", "My Product", costInMicrons, 1);
transaction.Items.Add(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(DateTime.Now.Subtract(startTime), "Startup", "MainPage", "Label");

JavaScript Windows Store Apps

The same APIs are available for JavaScript, C#, VB.NET, & C++ and work the same for both Windows 8 store apps and Windows Phone 7.1 and 8 applications.
JavaScript developers should note that WinRT objects are exposed to JavaScript with some minor casing changes that are more consistent with JavaScript naming conventions. Therefore to track a page in JavaScript you would need to do something like the following (note the case change on properties and methods):
// track a page view
GoogleAnalytics.EasyTracker.getTracker().sendView("main");
Warning: The Google Analytics SDK for Windows 8 and Windows Phone can automatically track exceptions and application lifetime events if autoAppLifetimeTracking is set to true in analytics.xml. However, this is not supported in JavaScript store applications. WinRT objects cannot gain access to JavaScript store application lifetime events such as suspend, resume, and unhandled exceptions like they can for Xaml based apps. Therefore, if using JavaScript you should consider manually calling EasyTracker for these events.

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

Aucun commentaire:

Enregistrer un commentaire