Scripting API

Initialization.

There are two ways to initialize google analytics. 

1) Simply add Google Analytics GameObject on your initial(menu) scene. 

You may use: Window → Google Analytics → Create Analytics GameObject

2) You can initialize analytics with StartTracking method code, Best practise to call it right after your application start.

SA.Analytics.Google.Manager.StartTracking();

Note: After analytics is initialized,  automatic data tracking will be started.

WARNING: This call will be ignored if you have already had Google Analytics game object on your scene as in the example scene.

WARNING: if you do not have Google Analytics game object and you skip StartTracking call, GoogleAnalytics game object will be initialized on first GoogleAnalytics.Client call.

 

Data reporting.

Additional data can be sent from any part of your code. You can send common hit types described in Measurement Protocol Developer Guide using GoogleAnalytics.Client class functions

  • SendPageHit
  • SendEventHit
  • SendTransactionHit
  • SendItemHit
  • SendSocialHit
  • SendExceptionHit
  • SendUserTimingHit
  • SendScreenHit

The following code snippet contains basic data reporting examples:

using SA.Analytics.Google;

Manager.Client.SendPageHit("mydemo.com ", "/home", "homepage");
Manager.Client.SendEventHit("video", "play", "holiday", 300);
Manager.Client.SendTransactionHit("12345", "westernWear", "EUR", 50.00f, 32.00f, 12.00f);
Manager.Client.SendItemHit("12345", "sofa", "u3eqds43", 300f, 2, "furniture", "EUR");
Manager.Client.SendSocialHit("like", "facebook", "/home ");
Manager.Client.SendExceptionHit("IOException", true);
Manager.Client.SendUserTimingHit("jsonLoader", "load", 5000, "jQuery");
Manager.Client.SendScreenHit("MainMenu");

 

Those methods will help to send hits with the most frequently used parameters. But you can always create hits with the parameters you need with the GoogleAnalytics.Client.CreateHit method. Then add Measurement Protocol data using the correspondent function and send hit using GoogleAnalytics.Client.Send function. The examples with google official API can be found here.

The same google examples but adapted for plugin API can be found below.

 

Page Tracking

Google Example:
v=1             // Version.
&tid=UA-XXXX-Y  // Tracking ID / Property ID.
&cid=555        // Anonymous Client ID.

&t=pageview     // Pageview hit type.
&dh=mydemo.com  // Document hostname.
&dp=/home       // Page.
&dt=homepage    // Title.
Plugin Example:
using SA.Analytics.Google

Manager.Client.CreateHit(GoogleAnalyticsHitType.PAGEVIEW);
Manager.Client.SetDocumentHostName("mydemo.com");
Manager.Client.SetDocumentPath("/home");
Manager.Client.SetDocumentTitle("homepage");

Manager.Client.Send();

 

Event Tracking

Google Example:
v=1             // Version.
&tid=UA-XXXX-Y  // Tracking ID / Property ID.
&cid=555        // Anonymous Client ID.

&t=event        // Event hit type
&ec=video       // Event Category. Required.
&ea=play        // Event Action. Required.
&el=holiday     // Event label.
&ev=300         // Event value.

 

Plugin Example
using SA.Analytics.Google;

Manager.Client.CreateHit(GoogleAnalyticsHitType.EVENT);
Manager.Client.SetEventCategory("video");
Manager.Client.SetEventAction("play");
Manager.Client.SetEventLabel("holiday");
Manager.Client.SetEventValue(300);

Manager.Client.Send();

 

Measuring Purchases

Google Example:
v=1                                  // Version.
&tid=UA-XXXX-Y                       // Tracking ID / Property ID.
&cid=555                             // Anonymous Client ID.
&t=pageview                          // Pageview hit type.
&dh=mydemo.com                       // Document hostname.
&dp=/receipt                         // Page.
&dt=Receipt%20Page                   // Title.

&ti=T12345                           // Transaction ID. Required.
&ta=Google%20Store%20-%20Online      // Affiliation.
&tr=37.39                            // Revenue.
&tt=2.85                             // Tax.
&ts=5.34                             // Shipping.
&tcc=SUMMER2013                      // Transaction coupon.

&pa=purchase                         // Product action (purchase). Required.
&pr1id=P12345                        // Product 1 ID. Either ID or name must be set.
&pr1nm=Android%20Warhol%20T-Shirt    // Product 1 name. Either ID or name must be set.
&pr1ca=Apparel                       // Product 1 category.
&pr1br=Google                        // Product 1 brand.
&pr1va=Black                         // Product 1 variant.
&pr1ps=1                             // Product 1 position.

 

Plugin Example:
using SA.Analytics.Google;

Manager.Client.CreateHit(GoogleAnalyticsHitType.PAGEVIEW);
Manager.Client.SetDocumentHostName("mydemo.com");
Manager.Client.SetDocumentPath("/receipt");
Manager.Client.SetDocumentTitle("Receipt Page");

Manager.Client.SetTransactionID("T12345");
Manager.Client.SetTransactionAffiliation("Google Store - Online");
Manager.Client.SetTransactionRevenue(37.39f);
Manager.Client.SetTransactionTax(2.85f);
Manager.Client.SetTransactionShipping(5.34f);
Manager.Client.SetTransactionCouponCode("SUMMER2013");

Manager.Client.SetProductAction("purchase");
Manager.Client.SetProductSKU(1, "P12345");
Manager.Client.SetSetProductName(1, "Android Warhol T-Shirt");
Manager.Client.SetProductCategory(1, "Apparel");
Manager.Client.SetProductBrand(1, "Google");
Manager.Client.SetProductVariant(1, "Black");
Manager.Client.SetProductPosition(1, 1);

Manager.Client.Send();

 

Measuring Refunds

If you need to send refund data using an event and the event is not a part of normally measured onsite behavior (i.e. not user initiated), then it’s recommended that you send a non-interaction event. This will prevent metrics such as bounce rate, session duration, etc. from being affected by the event.

Refund an entire transaction and send with a non-interaction event.

Google Example:
v=1                                  // Version.
&tid=UA-XXXX-Y                       // Tracking ID / Property ID.
&cid=555                             // Anonymous Client ID.
&t=event                             // Event hit type.
&ec=Ecommerce                        // Event Category. Required.
&ea=Refund                           // Event Action. Required.
&ni=1                                // Non-interaction parameter.

&ti=T12345                           // Transaction ID. Required.
&pa=refund                           // Product action (refund). Required.

 

Plugin Example:
using SA.Analytics.Google;

Manager.Client.CreateHit(GoogleAnalyticsHitType.EVENT);
Manager.Client.SetEventCategory("Ecommerce");
Manager.Client.SetEventAction("Refund");
Manager.Client.SetNonInteractionFlag();
Manager.Client.SetTransactionID("T12345");
Manager.Client.SetProductAction("refund");

Manager.Client.Send();

 

Refund a single product.

Google Example:
v=1                                  // Version.
&tid=UA-XXXX-Y                       // Tracking ID / Property ID.
&cid=555                             // Anonymous Client ID.
&t=event                             // Event hit type.
&ec=Ecommerce                        // Event Category. Required.
&ea=Refund                           // Event Action. Required.
&ni=1                                // Non-interaction parameter.

&ti=T12345                           // Transaction ID. Required.
&pa=refund                           // Product action (refund). Required.
&pr1id=P12345                        // Product 1 ID. Required.
&pr1qt=1                             // Product 1 quantity. Required.

 

Plugin Example:
using SA.Analytics.Google;

Manager.Client.CreateHit(GoogleAnalyticsHitType.EVENT);
Manager.Client.SetEventCategory("Ecommerce");
Manager.Client.SetEventAction("Refund");
Manager.Client.SetNonInteractionFlag();
Manager.Client.SetTransactionID("T12345");
Manager.Client.SetProductAction("refund");
Manager.Client.SetProductSKU(1, "P12345");
Manager.Client.SetProductQuantity(1, 1);

Manager.Client.Send();

 

Measuring the Checkout Process

1. Measuring Checkout Steps

Google Example:
v=1                                  // Version.
&tid=UA-XXXX-Y                       // Tracking ID / Property ID.
&cid=555                             // Anonymous Client ID.
&t=pageview                          // Pageview hit type.
&dh=mydemo.com                       // Document hostname.
&dp=/checkout                        // Page.
&dt=Checkout                         // Title.

&pa=checkout                         // Product action (checkout).
&pr1id=P12345                        // Product 1 ID. Either ID or name must be set.
&pr1nm=Android%20Warhol%20T-Shirt    // Product 1 name. Either ID or name must be set.
&pr1ca=Apparel                       // Product 1 category.
&pr1br=Google                        // Product 1 brand.
&pr1va=Black                         // Product 1 variant.
&pr1pr=29.20                         // Product 1 Price.
&pr1qt=1                             // Product 1 quantity.
&cos=1                               // Checkout step #1.
&col=Visa                            // Checkout step option.

 

Plugin Example:
using SA.Analytics.Google;

Manager.Client.CreateHit(GoogleAnalyticsHitType.PAGEVIEW);
Manager.Client.SetDocumentHostName("mydemo.com");
Manager.Client.SetDocumentPath("/receipt");
Manager.Client.SetDocumentTitle("Receipt Page");

Manager.Client.SetTransactionID("T12345");
Manager.Client.SetTransactionAffiliation("Google Store - Online");
Manager.Client.SetTransactionRevenue(37.39f);
Manager.Client.SetTransactionTax(2.85f);
Manager.Client.SetTransactionShipping(5.34f);
Manager.Client.SetTransactionCouponCode("SUMMER2013");

Manager.Client.SetProductAction("purchase");
Manager.Client.SetProductSKU(1, "P12345");
Manager.Client.SetSetProductName(1, "Android Warhol T-Shirt");
Manager.Client.SetProductCategory(1, "Apparel");
Manager.Client.SetProductBrand(1, "Google");
Manager.Client.SetProductVariant(1, "Black");
Manager.Client.SetProductPrice(1, 29.90f);
Manager.Client.SetProductQuantity(1, 1);
Manager.Client.SetCheckoutStep(1);
Manager.Client.SetCheckoutStepOption("Visa");

Manager.Client.Send();

 

2. Measuring Checkout Options

Google Example:
v=1                                  // Version.
&tid=UA-XXXX-Y                       // Tracking ID / Property ID.
&cid=555                             // Anonymous Client ID.
&t=event                             // Event hit type
&ec=Checkout                         // Event Category. Required.
&ea=Option                           // Event Action. Required.

&pa=checkout_option                  // Product action (checkout_option).
&cos=2                               // Checkout step.
&col=FedEx                           // Checkout step option.

 

Plugin Example:
using SA.Analytics.Google;

Manager.Client.CreateHit(GoogleAnalyticsHitType.EVENT);
Manager.Client.SetEventCategory("Checkout");
Manager.Client.SetEventAction("Option");
Manager.Client.SetProductAction("checkout_option");
Manager.Client.SetCheckoutStep(2);
Manager.Client.SetCheckoutStepOption("FedEx");

Manager.Client.Send();

 

Measuring Internal Promotions

Promotion Impressions

Google Example:
v=1                                     // Version.
&tid=UA-XXXX-Y                          // Tracking ID / Property ID.
&cid=555                                // Anonymous Client ID.
&t=pageview                             // Pageview hit type.
&dh=mydemo.com                          // Document hostname.
&dp=/home                               // Page.
&dt=homepage                            // Title.

&promo1id=PROMO_1234                    // Promotion 1 ID. Either ID or name must be set.
&promo1nm=Summer%20Sale                 // Promotion 1 name. Either ID or name must be set.
&promo1cr=summer_banner2                // Promotion Creative.
&promo1ps=banner_slot1                  // Promotion Position.

 

Plugin Example:
using SA.Analytics.Google;

Manager.Client.CreateHit(GoogleAnalyticsHitType.PAGEVIEW);
Manager.Client.SetDocumentHostName("mydemo.com");
Manager.Client.SetDocumentPath("/home");
Manager.Client.SetDocumentTitle("homepage");
Manager.Client.SetPromotionID(1, "PROMO_1234");
Manager.Client.SetPromotionName(1,"Summer Sale");
Manager.Client.SetPromotionCreative(1, "summer_banner2");
Manager.Client.SetPromotionPosition(1, "banner_slot1");

Manager.Client.Send();

 

Promotion Clicks

Google Example:
v=1                                  // Version.
&tid=UA-XXXX-Y                       // Tracking ID / Property ID.
&cid=555                             // Anonymous Client ID.
&t=event                             // Event hit type
&ec=Internal%Promotions              // Event Category. Required.
&ea=click                            // Event Action. Required.
&el=Summer%20Sale                    // Event label.

&promoa=click                        // Promotion action (click). Required.
&promo1id=PROMO_1234                 // Promotion 1 ID. Either ID or name must be set.
&promo1nm=Summer%20Sale              // Promotion 1 name. Either ID or name must be set.
&promo1cr=summer_banner2             // Promotion Creative.
&promo1ps=banner_slot1               // Promotion Position.

 

Plugin Example:
using SA.Analytics.Google;

Manager.Client.CreateHit(GoogleAnalyticsHitType.EVENT);
Manager.Client.SetEventCategory("Internal Promotions");
Manager.Client.SetEventAction("click");
Manager.Client.SetEventLabel("Summer Sale");
Manager.Client.SetPromotionAction("click");
Manager.Client.SetPromotionID(1, "PROMO_1234");
Manager.Client.SetPromotionName(1,"Summer Sale");
Manager.Client.SetPromotionCreative(1, "summer_banner2");
Manager.Client.SetPromotionPosition(1, "banner_slot1");


Manager.Client.Send();

 

Ecommerce Tracking

To send ecommerce data, send one transaction hit to represent an entire transaction, then send an item hit for each item in the transaction. The transaction ID links all the hits together to represent the entire purchase.

Transaction Hit

Google Example:
v=1              // Version.
&tid=UA-XXXX-Y   // Tracking ID / Property ID.
&cid=555         // Anonymous Client ID.

&t=transaction   // Transaction hit type.
&ti=12345        // transaction ID. Required.
&ta=westernWear  // Transaction affiliation.
&tr=50.00        // Transaction revenue.
&ts=32.00        // Transaction shipping.
&tt=12.00        // Transaction tax.
&cu=EUR          // Currency code.

 

Plugin Example:
using SA.Analytics.Google;

Manager.Client.CreateHit(GoogleAnalyticsHitType.TRANSACTION);
Manager.Client.SetTransactionID("12345");
Manager.Client.SetTransactionAffiliation("westernWear");
Manager.Client.SetTransactionRevenue(50);
Manager.Client.SetTransactionShipping(32f);
Manager.Client.SetTransactionTax(12f);
Manager.Client.SetCurrencyCode("EUR");

Manager.Client.Send();

 

Item Hit

Google Example:
v=1              // Version.
&tid=UA-XXXX-Y   // Tracking ID / Property ID.
&cid=555         // Anonymous Client ID.

&t=item          // Item hit type.
&ti=12345        // Transaction ID. Required.
&in=sofa         // Item name. Required.
&ip=300          // Item price.
&iq=2            // Item quantity.
&ic=u3eqds43     // Item code / SKU.
&iv=furniture    // Item variation / category.
&cu=EUR          // Currency code.

 

Plugin Example:
using SA.Analytics.Google;

Manager.Client.CreateHit(GoogleAnalyticsHitType.ITEM);
Manager.Client.SetTransactionID("12345");
Manager.Client.SetItemName("sofa");
Manager.Client.SetItemPrice(300f);
Manager.Client.SetItemQuantity(2);
Manager.Client.SetItemCode("u3eqds43");
Manager.Client.SetItemCategory("furniture");
Manager.Client.SetCurrencyCode("EUR");

Manager.Client.Send();

 

Social Interactions

Google Example:
v=1             // Version.
&tid=UA-XXXX-Y  // Tracking ID / Property ID.
&cid=555        // Anonymous Client ID.

&t=social       // Social hit type.
&sa=like        // Social Action. Required.
&sn=facebook    // Social Network. Required.
&st=/home       // Social Target. Required.

 

Plugin Example:
using SA.Analytics.Google;

Manager.Client.CreateHit(GoogleAnalyticsHitType.SOCIAL);     
Manager.Client.SetSocialAction("like");     				
Manager.Client.SetSocialNetwork("facebook"); 
Manager.Client.SetSocialActionTarget("/home  ");

Manager.Client.Send();

 

Exception Tracking

Google Example:
v=1             // Version.
&tid=UA-XXXX-Y  // Tracking ID / Property ID.
&cid=555        // Anonymous Client ID.

&t=exception      // Exception hit type.
&exd=IOException  // Exception description.
&exf=1            // Exception is fatal?

 

Plugin Example:
using SA.Analytics.Google;

Manager.Client.CreateHit(GoogleAnalyticsHitType.EXCEPTION);  
Manager.Client.SetExceptionDescription("IOException");
Manager.Client.SetIsFatalException(true);

Manager.Client.Send();

 

User Timing Tracking

Google Example:
v=1             // Version.
&tid=UA-XXXX-Y  // Tracking ID / Property ID.
&cid=555        // Anonymous Client ID.

&t=timing       // Timing hit type.
&utc=jsonLoader // Timing category.
&utv=load       // Timing variable.
&utt=5000       // Timing time.
&utl=jQuery     // Timing label.

// These values are part of browser load times

&dns=100        // DNS load time.
&pdt=20         // Page download time.
&rrt=32         // Redirect time.
&tcp=56         // TCP connect time.
&srt=12         // Server response time.

 

Plugin Example:
using SA.Analytics.Google;

Manager.Client.CreateHit(GoogleAnalyticsHitType.TIMING); 
Manager.Client.SetUserTimingCategory("jsonLoader");
Manager.Client.SetUserTimingVariableName("load");
Manager.Client.SetUserTimingTime(5000);
Manager.Client.SetUserTimingLabel("jQuery");

Manager.Client.SetDNSTime(100);
Manager.Client.SetPageDownloadTime(20);
Manager.Client.SetRedirectResponseTime(32);
Manager.Client.SetTCPConnectTime(56);
Manager.Client.SetServerResponseTime(12);

Manager.Client.Send();

 

 Screen Tracking

Google Example:
v=1                        // Version.
&tid=UA-XXXX-Y             // Tracking ID / Property ID.
&cid=555                   // Anonymous Client ID.

&t=screenview              // Screenview hit type.
&an=funTimes               // App name.
&av=4.2.0                  // App version.
&aid=com.foo.App           // App Id.
&aiid=com.android.vending  // App Installer Id.

&cd=Home                   // Screen name / content description.

 

Plugin Example:
SA.Analytics.Google.Manager.Client.SendScreenHit("MainMenu");

 

Custom Hit builder

If, for example, you haven't found plugin methods for the hit parameter described in the Measurement Protocol Parameter Reference, you can always add data manually to hit as shown in the snippet below.

using SA.Analytics.Google;

//Simple Page hit
Manager.Client.CreateHit(GoogleAnalyticsHitType.PAGEVIEW);
Manager.Client.SetDocumentHostName("mydemo.com");
Manager.Client.SetDocumentPath("/home");
Manager.Client.SetDocumentTitle("homepage");

Manager.Client.Send();

//Constructing Same page hit without plugin methods
Manager.Client.CreateHit(GoogleAnalyticsHitType.PAGEVIEW);
Manager.Client.AppendData("dh", "mydemo.com");
Manager.Client.AppendData("dp", "/home");
Manager.Client.AppendData("dt", "homepage");


Manager.Client.Send();