From 6e5a11421fae7781d21ae11d6bcd4dfad7f75eab Mon Sep 17 00:00:00 2001 From: Thaina Date: Wed, 12 Aug 2015 02:13:17 +0700 Subject: [PATCH 1/5] Move prefab into resource Fix GetInstance logic to ensure GAv3 instance from prefab No need to drag GAv3 in scene --- source/Plugins/GoogleAnalyticsV3/GAv3.prefab | Bin 6528 -> 0 bytes .../GoogleAnalyticsV3/HitBuilders/GAExt.cs | 95 ++++++++++++++++++ .../GoogleAnalyticsV3/Resources/GAv3.prefab | Bin 0 -> 5396 bytes 3 files changed, 95 insertions(+) delete mode 100644 source/Plugins/GoogleAnalyticsV3/GAv3.prefab create mode 100644 source/Plugins/GoogleAnalyticsV3/HitBuilders/GAExt.cs create mode 100644 source/Plugins/GoogleAnalyticsV3/Resources/GAv3.prefab diff --git a/source/Plugins/GoogleAnalyticsV3/GAv3.prefab b/source/Plugins/GoogleAnalyticsV3/GAv3.prefab deleted file mode 100644 index edb986eb703adaf0d039b784d1208fbba16bbcd7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6528 zcmeHLNsrt_6s`sm684=**d`l{S0oN_LrN+2-k1-7lVb28Z&F?ZXEsU_D3wjrM6s zxi}DRYWDB4n4%@zUZB3dPDbY=y)rNXzq?!DT66ZMGS+tZ{IR}AR?DxsdkCqeZW4|% zbkAOK*X|WHaM{?k`veHxL#cJ`e*2v1J5ZV>Vbmv!=g7Sr#1BZ+BzI|&=nXZ;|A_ww z34<2+XqTMQ@QUG3dx&15I1sM8t2$AE&NM&8w}B!N1sUtP;{%>jbT zY_*B4Zh4jL9u>{D+70X;la1upKD);o5IMUn(cSmSXUF5fI&4N@)yK`0b`i$)v~{|H z(r(d6E01QM(i07coYE+XgK;-2hVV%N-HOpu|HSBN(MU6+XBrSWqm^+KknZ?nH!JX4 z(b_|psgBw!8fm6Q3eJU_$zAEux@Z!}DaX@(0p5z!0m4k>^sH#4nbUI(2z;MGn2vPT zUG118j06634hkp>0NwWnvCdSghogZxnV1j8ChBf5`uPSl8Xm;`GiJ>U81zB|$ZDF7 z;%H+?U+qGNyZE9+F4!B343dq-am1jP8bGj_niPIr4u>WlXKeXp3C+g>t`zcJ%buKp zCQ3nJD~ft6d%8%36B{JHDSNKbgdn~mjz*uN_m^x| zN2*3LM4842`EV+urgZ2tl1Y$FQIkA~%Vs?rC+6@8OluZs6nPMl4^bSnaZ=;uM2oLa zYP__VgDQh~POqIc>1sKaGCw*7l{!nA&cd#phmI>k6@mrlsqQu1VL zUIvsJ=US|NI8x6XsA`s4rQHF_f-vn1TiFrMw6`u`e>BWgW~nt4xU1!X7UMWOp9Fc{ z-NPt{vp2ueXxr_Gx{kuNYN8Z28~&}1@*d0CIG|-`*M70X%ib*#D;pvs+yOF(2(0_A zP<*>(!%hSe#df`140=xFS+D@1LT&u~Z5Ke4Dxy>}PB~P)4VZ}~Pz_aRsUy}ROf6L9 zyb(Z@P7;qyl5I$nHeTVWofcGh3SDDQx$T@xCw-H#Z{&m$tvmCcZ=`lQ3N_zI0d=5! zjzsHari*Pac8nBLbQ<`oV<4z_&PTgsR(!)!QJq15k|ZV6A#zGt$_pq`o>(5xI+I7_XV2nE%`8l|qXuII4pr zg%XuJbu7X|N5&8;25 zs9%UO-S1Q9Dur#+Gk12c(LSzSU)^)E^yZ~c4!3W9^yk-CZ~pQX?#rV@xvOdIx_3DL fwDGyCQadvd_02Jd2Vh4&Gndb573l%^w+Qq%3co>( diff --git a/source/Plugins/GoogleAnalyticsV3/HitBuilders/GAExt.cs b/source/Plugins/GoogleAnalyticsV3/HitBuilders/GAExt.cs new file mode 100644 index 0000000..00e39db --- /dev/null +++ b/source/Plugins/GoogleAnalyticsV3/HitBuilders/GAExt.cs @@ -0,0 +1,95 @@ +using UnityEngine; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +static class GAExt +{ + public static T Validate(this T hitBuilder) where T : HitBuilder + { + if(hitBuilder != null && hitBuilder.IsValid) + return hitBuilder; + + return null; + } + + + public static T SetCustomDimension(this T hitBuilder,int dimensionNumber,string value) where T : HitBuilder + { + hitBuilder.CustomDimensions.Add(dimensionNumber,value); + return hitBuilder; + } + + public static T SetCustomMetric(this T hitBuilder,int metricNumber,string value) where T : HitBuilder + { + hitBuilder.CustomMetrics.Add(metricNumber,value); + return hitBuilder; + } + + public static T SetCampaignName(this T hitBuilder,string campaignName) where T : HitBuilder + { + if(campaignName != null) + hitBuilder.CampaignName = campaignName; + + return hitBuilder; + } + + public static T SetCampaignSource(this T hitBuilder,string campaignSource) where T : HitBuilder + { + if(campaignSource != null) + hitBuilder.CampaignSource = campaignSource; + else Debug.Log("Campaign source cannot be null or empty"); + + return hitBuilder; + } + + public static T SetCampaignMedium(this T hitBuilder,string campaignMedium) where T : HitBuilder + { + if(campaignMedium != null) + hitBuilder.CampaignMedium = campaignMedium; + + return hitBuilder; + } + + public static T SetCampaignKeyword(this T hitBuilder,string campaignKeyword) where T : HitBuilder + { + if(campaignKeyword != null) + hitBuilder.CampaignKeyword = campaignKeyword; + + return hitBuilder; + } + + public static T SetCampaignContent(this T hitBuilder,string campaignContent) where T : HitBuilder + { + if(campaignContent != null) + hitBuilder.CampaignContent = campaignContent; + + return hitBuilder; + } + + public static T SetCampaignID(this T hitBuilder,string campaignID) where T : HitBuilder + { + if(campaignID != null) + hitBuilder.CampaignID = campaignID; + + return hitBuilder; + } + + public static T SetGclid(this T hitBuilder,string gclid) where T : HitBuilder + { + if(gclid != null) + hitBuilder.gclid = gclid; + + return hitBuilder; + } + + public static T SetDclid(this T hitBuilder,string dclid) where T : HitBuilder + { + if(dclid != null) + hitBuilder.dclid = dclid; + + return hitBuilder; + } +} diff --git a/source/Plugins/GoogleAnalyticsV3/Resources/GAv3.prefab b/source/Plugins/GoogleAnalyticsV3/Resources/GAv3.prefab new file mode 100644 index 0000000000000000000000000000000000000000..0f1f3a76b54532afbdbeea1d1ecc410affb0d18b GIT binary patch literal 5396 zcmd^DTWnNC7@pHBpn}{KxfTVjTq;;VqKI3eEe#8;%f(BA^{_kLUFhy9XHVBIO5B8q zn)qte5KVnC5-&y;$G2wr$NoySP0*K5jJtKtDe3jSC-~`|<5f@19(K zdHz{h+}xSiuxjqn?JtQ-W8?2XU)%`2+h`VAvn9%p2~p-ai1P$Ld7+JD;a9pdaLE3gAJW<_d0! zb>J!bJe;E=!k(!CJjgSxf?Hw~cJ2cYS9k}S)uGo$KWqoib#cwc4;jq+yFc$;!1>hr zVY+zJOyV)(^~%o+FoyQbBCcA!cm}!$&?wKY z;FchNyU)*)6U9H5cx)m+_cMzB-U_byTOrfVdFaKP98(vv|32z(o5(+(I5v&z3fjM* zf@}UwkjcLgeH8y9>PPS|CLY1h^QdvnABRl-7W7g4O94mm-%mV(|A7jw`Co)g{$=Q+ z_?J^Z!u|(|NAN#X!8JejUGcY~kK%ur`Vssqh)3|Rtl*me709%I75XUt)zpvRZzCST z|40S5)OmgjGSBlGG$sn>2nBQ!awoxM+5jM&iQG`Jg@D#K#*e41ypNiYV`DCWEh#5u7EhUEYfD4>$cQz-TVNZGb^TkbHyfD4> z#ElzIy9YMT`I(WGMWdHa!DIjB%5o7Wrxokq$;Y8Il_iwfrdIH9X0BG2da(qRf|;4w zos~sbs0MYyj{~Q#SXm6d4EobBH!)TgjW;TNQ(;+I3|?DVG_ET+PdxcKHy;as!HP4J zV3~M0W2?#%BOYE?wX%dWwyG?kGW;fEv$90-tI86=U#%=*epVJ;i7fFI{Id-*{lm(_ zr?&E{%Hrce|ES8MaP6O7{HA|cSqwib{!PE)v$6;gQ|pRjCCud1TkQ9f%>cRnElzI8 z$upXA{c*d55YF}Qw$r;jml2oi?@1z#bNxH*;tpBJ7>%AnN7Bm{r84r5dFk)3f4KGH zhJ)YC-g%_)GRw~q-w+Q*hk{Mv|G&?;t2i7CS`%zyN{c!iA7S%00_SG)-~{oTmXOe#U9s5?A5>7({ZbU<>e=c~!6la5FE1Ey!=?Wv~VL`yi5^!4}jv z{3_TY_!(?LeZ$XSGyF_;`yeveG1znx2`2>xo5sUQfx%|*3x2YroeVaO>ttt=7lX~< z!Qf=DY1|Uz$v`HL4mL~e;~4hQ@Tku?6`FM-$kT!`)aD=c4?*TWE>*Y?lMmXsle7mD zj%TwziV;x~hsDbbVB&pFA**m--UUqJ<(5o#Fy%@{a~JBriu8ozc)e~4zpvqP^z6bJ zD{gn6`tzs6jP}*#C-(6B?-#T>v@t9^6v$=0n18yD@t|37snhTly3}nsU6tYv@$6_4QtoV(c z3}nsI9J+@vkc0fMK_))~If}npHN*VXsu||5R!!}P*TKs*srwk`5Me*?;2Zx0_p4ep z^?tnrJ=)1Y*824!KdAeddKca=2C~*~Q2WCDqCM5BslP(ZzA%uDe}eWfkTtIF`6nQg zM+Y+B>HjYoo!JlLzNywa2l&Q*>LjjncT^mdlM06hi$&bN{g zE=G^hDf-Ew(=O8g-BX^=pcY~XErrGb4LCgLMC|6L?8GIsSJ8N+7-D=z)3+MgKL@g3 z(KD@GtH(G^&R60}=`+=<*}uJfcFxN4i{i&`o$uar`pi-0q)9wx9;P<681%+wV~eqq zbkynYD7G{8?8$p}IxV@Z2~WVx&^t39!Jyx&O68}jzxj)@Y2i0UQtLN@-$%`3^w=BU z)eM^;auc{fGd8F&g?SlI?YmmnGo97cw6+m{+!_-6%@Z}Q1YOSeZ^s>;#5iMy*%!|9 E7XxoKD*ylh literal 0 HcmV?d00001 From c7ee71d140e8c16dc14735059b5a0eb73be090da Mon Sep 17 00:00:00 2001 From: Thaina Date: Wed, 12 Aug 2015 02:14:10 +0700 Subject: [PATCH 2/5] Refactor HitBuilder clean and decouple with extension method Replace Field with just string --- source/Plugins/GoogleAnalyticsV3/Field.cs | 29 - source/Plugins/GoogleAnalyticsV3/Fields.cs | 178 ++-- .../Plugins/GoogleAnalyticsV3/GAIHandler.cs | 4 +- .../GoogleAnalyticsAndroidV3.cs | 6 +- .../GoogleAnalyticsV3/GoogleAnalyticsMPV3.cs | 785 ++++++++-------- .../GoogleAnalyticsV3/GoogleAnalyticsV3.cs | 835 ++++++++---------- .../HitBuilders/AppViewHitBuilder.cs | 52 +- .../HitBuilders/EventHitBuilder.cs | 149 ++-- .../HitBuilders/ExceptionHitBuilder.cs | 67 +- .../HitBuilders/HitBuilder.cs | 153 +--- .../HitBuilders/ItemHitBuilder.cs | 247 +++--- .../HitBuilders/SocialHitBuilder.cs | 137 +-- .../HitBuilders/TimingHitBuilder.cs | 145 +-- .../HitBuilders/TransactionHitBuilder.cs | 210 +++-- 14 files changed, 1474 insertions(+), 1523 deletions(-) delete mode 100644 source/Plugins/GoogleAnalyticsV3/Field.cs diff --git a/source/Plugins/GoogleAnalyticsV3/Field.cs b/source/Plugins/GoogleAnalyticsV3/Field.cs deleted file mode 100644 index 74b0780..0000000 --- a/source/Plugins/GoogleAnalyticsV3/Field.cs +++ /dev/null @@ -1,29 +0,0 @@ -/* - Copyright 2014 Google Inc. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -public class Field { - - private readonly string parameter; - - public Field(string parameter){ - this.parameter = parameter; - } - - public override string ToString(){ - return parameter; - } - -} diff --git a/source/Plugins/GoogleAnalyticsV3/Fields.cs b/source/Plugins/GoogleAnalyticsV3/Fields.cs index 1feade0..608efe2 100644 --- a/source/Plugins/GoogleAnalyticsV3/Fields.cs +++ b/source/Plugins/GoogleAnalyticsV3/Fields.cs @@ -1,7 +1,7 @@ /* Copyright 2014 Google Inc. All rights reserved. - Licensed under the Apache License, Version 2.0 (the "License"); + Licensed under the Apache License, Version 2.0 (the "License"; you may not use this file except in compliance with the License. You may obtain a copy of the License at @@ -18,91 +18,91 @@ limitations under the License. using System.Collections; /* - Available fields to use with SetOnTracker(Field fieldName, object value) -*/ -public class Fields { - - //General - public readonly static Field ANONYMIZE_IP = new Field("&aip"); - public readonly static Field HIT_TYPE = new Field("&t"); - public readonly static Field SESSION_CONTROL = new Field("&sc"); - - public readonly static Field SCREEN_NAME = new Field("&cd"); - public readonly static Field LOCATION = new Field("&dl"); - public readonly static Field REFERRER = new Field("&dr"); - public readonly static Field PAGE = new Field("&dp"); - public readonly static Field HOSTNAME = new Field("&dh"); - public readonly static Field TITLE = new Field("&dt"); - public readonly static Field LANGUAGE = new Field("&ul"); - public readonly static Field ENCODING = new Field("&de"); - - // System - public readonly static Field SCREEN_COLORS = new Field("&sd"); - public readonly static Field SCREEN_RESOLUTION = new Field("&sr"); - public readonly static Field VIEWPORT_SIZE = new Field("&vp"); - - // Application - public readonly static Field APP_NAME = new Field("&an"); - public readonly static Field APP_ID = new Field("&aid"); - public readonly static Field APP_INSTALLER_ID = new Field("&aiid"); - public readonly static Field APP_VERSION = new Field("&av"); - - // Visitor - public readonly static Field CLIENT_ID = new Field("&cid"); - public readonly static Field USER_ID = new Field("&uid"); - - // Campaign related fields; used in all hits. - public readonly static Field CAMPAIGN_NAME = new Field("&cn"); - public readonly static Field CAMPAIGN_SOURCE = new Field("&cs"); - public readonly static Field CAMPAIGN_MEDIUM = new Field("&cm"); - public readonly static Field CAMPAIGN_KEYWORD = new Field("&ck"); - public readonly static Field CAMPAIGN_CONTENT = new Field("&cc"); - public readonly static Field CAMPAIGN_ID = new Field("&ci"); - // Autopopulated campaign fields - public readonly static Field GCLID = new Field("&gclid"); - public readonly static Field DCLID = new Field("&dclid"); - - - // Event Hit (&t=event) - public readonly static Field EVENT_CATEGORY = new Field("&ec"); - public readonly static Field EVENT_ACTION = new Field("&ea"); - public readonly static Field EVENT_LABEL = new Field("&el"); - public readonly static Field EVENT_VALUE = new Field("&ev"); - - // Social Hit (&t=social) - public readonly static Field SOCIAL_NETWORK = new Field("&sn"); - public readonly static Field SOCIAL_ACTION = new Field("&sa"); - public readonly static Field SOCIAL_TARGET = new Field("&st"); - - // Timing Hit (&t=timing) - public readonly static Field TIMING_VAR = new Field("&utv"); - public readonly static Field TIMING_VALUE = new Field("&utt"); - public readonly static Field TIMING_CATEGORY = new Field("&utc"); - public readonly static Field TIMING_LABEL = new Field("&utl"); - - - // Exception Hit (&t=exception) - public readonly static Field EX_DESCRIPTION = new Field("&exd"); - public readonly static Field EX_FATAL = new Field("&exf"); - - // Ecommerce (&t=transaction / &t=item) - public readonly static Field CURRENCY_CODE = new Field("&cu"); - public readonly static Field TRANSACTION_ID = new Field("&ti"); - public readonly static Field TRANSACTION_AFFILIATION = new Field("&ta"); - public readonly static Field TRANSACTION_SHIPPING = new Field("&ts"); - public readonly static Field TRANSACTION_TAX = new Field("&tt"); - public readonly static Field TRANSACTION_REVENUE = new Field("&tr"); - public readonly static Field ITEM_SKU = new Field("&ic"); - public readonly static Field ITEM_NAME = new Field("&in"); - public readonly static Field ITEM_CATEGORY = new Field("&iv"); - public readonly static Field ITEM_PRICE = new Field("&ip"); - public readonly static Field ITEM_QUANTITY = new Field("&iq"); - - // General Configuration - public readonly static Field TRACKING_ID = new Field("&tid"); - public readonly static Field SAMPLE_RATE = new Field("&sf"); - public readonly static Field DEVELOPER_ID = new Field("&did"); - - public readonly static Field CUSTOM_METRIC = new Field("&cm"); - public readonly static Field CUSTOM_DIMENSION = new Field("&cd"); -} + Available strings to use with SetOnTracker(string stringName, object value) +*/ +public class Fields +{ + //General + public const string ANONYMIZE_IP = "&aip"; + public const string HIT_TYPE = "&t"; + public const string SESSION_CONTROL = "&sc"; + + public const string SCREEN_NAME = "&cd"; + public const string LOCATION = "&dl"; + public const string REFERRER = "&dr"; + public const string PAGE = "&dp"; + public const string HOSTNAME = "&dh"; + public const string TITLE = "&dt"; + public const string LANGUAGE = "&ul"; + public const string ENCODING = "&de"; + + // System + public const string SCREEN_COLORS = "&sd"; + public const string SCREEN_RESOLUTION = "&sr"; + public const string VIEWPORT_SIZE = "&vp"; + + // Application + public const string APP_NAME = "&an"; + public const string APP_ID = "&aid"; + public const string APP_INSTALLER_ID = "&aiid"; + public const string APP_VERSION = "&av"; + + // Visitor + public const string CLIENT_ID = "&cid"; + public const string USER_ID = "&uid"; + + // Campaign related strings; used in all hits. + public const string CAMPAIGN_NAME = "&cn"; + public const string CAMPAIGN_SOURCE = "&cs"; + public const string CAMPAIGN_MEDIUM = "&cm"; + public const string CAMPAIGN_KEYWORD = "&ck"; + public const string CAMPAIGN_CONTENT = "&cc"; + public const string CAMPAIGN_ID = "&ci"; + // Autopopulated campaign strings + public const string GCLID = "&gclid"; + public const string DCLID = "&dclid"; + + + // Event Hit (&t=event) + public const string EVENT_CATEGORY = "&ec"; + public const string EVENT_ACTION = "&ea"; + public const string EVENT_LABEL = "&el"; + public const string EVENT_VALUE = "&ev"; + + // Social Hit (&t=social) + public const string SOCIAL_NETWORK = "&sn"; + public const string SOCIAL_ACTION = "&sa"; + public const string SOCIAL_TARGET = "&st"; + + // Timing Hit (&t=timing) + public const string TIMING_VAR = "&utv"; + public const string TIMING_VALUE = "&utt"; + public const string TIMING_CATEGORY = "&utc"; + public const string TIMING_LABEL = "&utl"; + + + // Exception Hit (&t=exception) + public const string EX_DESCRIPTION = "&exd"; + public const string EX_FATAL = "&exf"; + + // Ecommerce (&t=transaction / &t=item) + public const string CURRENCY_CODE = "&cu"; + public const string TRANSACTION_ID = "&ti"; + public const string TRANSACTION_AFFILIATION = "&ta"; + public const string TRANSACTION_SHIPPING = "&ts"; + public const string TRANSACTION_TAX = "&tt"; + public const string TRANSACTION_REVENUE = "&tr"; + public const string ITEM_SKU = "&ic"; + public const string ITEM_NAME = "&in"; + public const string ITEM_CATEGORY = "&iv"; + public const string ITEM_PRICE = "&ip"; + public const string ITEM_QUANTITY = "&iq"; + + // General Configuration + public const string TRACKING_ID = "&tid"; + public const string SAMPLE_RATE = "&sf"; + public const string DEVELOPER_ID = "&did"; + + public const string CUSTOM_METRIC = "&cm"; + public const string CUSTOM_DIMENSION = "&cd"; +} \ No newline at end of file diff --git a/source/Plugins/GoogleAnalyticsV3/GAIHandler.cs b/source/Plugins/GoogleAnalyticsV3/GAIHandler.cs index 0be59de..6f59a60 100644 --- a/source/Plugins/GoogleAnalyticsV3/GAIHandler.cs +++ b/source/Plugins/GoogleAnalyticsV3/GAIHandler.cs @@ -180,7 +180,7 @@ public void _sendTiming(TimingHitBuilder builder){ [DllImport("__Internal")] private static extern void addCustomDimensionToDictionary(int key, string value); public void _buildCustomDimensionsDictionary(HitBuilder builder){ - foreach(KeyValuePair entry in builder.GetCustomDimensions()) + foreach(var entry in builder.GetCustomDimensions()) { addCustomDimensionToDictionary(entry.Key, entry.Value); } @@ -189,7 +189,7 @@ public void _buildCustomDimensionsDictionary(HitBuilder builder){ [DllImport("__Internal")] private static extern void addCustomMetricToDictionary(int key, string value); public void _buildCustomMetricsDictionary(HitBuilder builder){ - foreach(KeyValuePair entry in builder.GetCustomMetrics()) + foreach(var entry in builder.GetCustomMetrics()) { addCustomMetricToDictionary(entry.Key, entry.Value); } diff --git a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsAndroidV3.cs b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsAndroidV3.cs index 8b3332b..63be8e0 100644 --- a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsAndroidV3.cs +++ b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsAndroidV3.cs @@ -175,7 +175,7 @@ private AndroidJavaObject BuildMap(string methodName, object[] simpleArgs, hashMap.GetRawClass(), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); object[] args = new object[2]; - foreach (KeyValuePair kvp in parameters) { + foreach (var kvp in parameters) { using (AndroidJavaObject k = kvp.Key) { using (AndroidJavaObject v = new AndroidJavaObject( "java.lang.String", kvp.Value)) { @@ -211,12 +211,12 @@ private Dictionary Dictionary parameters = new Dictionary(); AndroidJavaObject fieldName; - foreach (KeyValuePair entry in builder.GetCustomDimensions()) { + foreach (var entry in builder.GetCustomDimensions()) { fieldName = analyticsTrackingFields.CallStatic( "customDimension", entry.Key); parameters.Add(fieldName, entry.Value); } - foreach (KeyValuePair entry in builder.GetCustomMetrics()) { + foreach (var entry in builder.GetCustomMetrics()) { fieldName = analyticsTrackingFields.CallStatic( "customMetric", entry.Key); parameters.Add(fieldName, entry.Value); diff --git a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsMPV3.cs b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsMPV3.cs index bfbe119..ccc3008 100644 --- a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsMPV3.cs +++ b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsMPV3.cs @@ -26,398 +26,449 @@ limitations under the License. appropriate methods in this class if the application is built for platforms other than Android and iOS. */ -public class GoogleAnalyticsMPV3 { +public class GoogleAnalyticsMPV3 +{ #if UNITY_ANDROID && !UNITY_EDITOR #elif UNITY_IPHONE && !UNITY_EDITOR #else - private string trackingCode; - private string bundleIdentifier; - private string appName; - private string appVersion; - private GoogleAnalyticsV3.DebugMode logLevel; - private bool anonymizeIP; - private bool dryRun; - private bool optOut; - private int sessionTimeout; - private string screenRes; - private string clientId; - private string url; - private float timeStarted; - private Dictionary trackerValues = new Dictionary(); - private bool startSessionOnNextHit = false; - private bool endSessionOnNextHit = false; - private bool trackingCodeSet = true; - - public void InitializeTracker() { - if(String.IsNullOrEmpty(trackingCode)){ - Debug.Log("No tracking code set for 'Other' platforms - hits will not be set"); - trackingCodeSet = false; - return; - } - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.INFO)) { - Debug.Log("Platform is not Android or iOS - " + - "hits will be sent using measurement protocol."); - } - screenRes = Screen.width + "x" + Screen.height; - clientId = SystemInfo.deviceUniqueIdentifier; - string language = Application.systemLanguage.ToString(); - optOut = false; + private string trackingCode; + private string bundleIdentifier; + private string appName; + private string appVersion; + private GoogleAnalyticsV3.DebugMode logLevel; + private bool anonymizeIP; + private bool dryRun; + private bool optOut; + private int sessionTimeout; + private string screenRes; + private string clientId; + private string url; + private float timeStarted; + private bool startSessionOnNextHit = false; + private bool endSessionOnNextHit = false; + private bool trackingCodeSet = true; + + readonly Dictionary trackerValues = new Dictionary(); + public void InitializeTracker() + { + if(String.IsNullOrEmpty(trackingCode)) + { + Debug.Log("No tracking code set for 'Other' platforms - hits will not be set"); + trackingCodeSet = false; + return; + } + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.INFO)) + { + Debug.Log("Platform is not Android or iOS - hits will be sent using measurement protocol."); + } + screenRes = Screen.width + "x" + Screen.height; + clientId = SystemInfo.deviceUniqueIdentifier; + string language = Application.systemLanguage.ToString(); + optOut = false; #if !UNITY_WP8 - CultureInfo[] cultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures); - foreach (CultureInfo info in cultureInfos) { - if (info.EnglishName == Application.systemLanguage.ToString()) { - language = info.Name; - } - } + CultureInfo[] cultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures); + foreach(var info in cultureInfos) + { + if(info.EnglishName == Application.systemLanguage.ToString()) + { + language = info.Name; + } + } #endif - try { - url = "https://www.google-analytics.com/collect?v=1" - + AddRequiredMPParameter(Fields.LANGUAGE, language) - + AddRequiredMPParameter(Fields.SCREEN_RESOLUTION, screenRes) - + AddRequiredMPParameter(Fields.APP_NAME, appName) - + AddRequiredMPParameter(Fields.TRACKING_ID, trackingCode) - + AddRequiredMPParameter(Fields.APP_ID, bundleIdentifier) - + AddRequiredMPParameter(Fields.CLIENT_ID, clientId) - + AddRequiredMPParameter(Fields.APP_VERSION, appVersion); - if(anonymizeIP){ - url += AddOptionalMPParameter(Fields.ANONYMIZE_IP, 1); - } - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) { - Debug.Log("Base URL for hits: " + url); - } - } catch (Exception) { - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) { - Debug.Log("Error building url."); - } - } - } - - public void SetTrackerVal(Field field, object value) { - trackerValues[field] = value; - } - - private string AddTrackerVals() { - if(!trackingCodeSet){ - return ""; - } - string vals = ""; - foreach (KeyValuePair pair in trackerValues){ - vals += AddOptionalMPParameter(pair.Key, pair.Value); - } - return vals; - } - - internal void StartSession() { - startSessionOnNextHit = true; - } - - internal void StopSession() { - endSessionOnNextHit = true; - } - - private void SendGaHitWithMeasurementProtocol(string url) { - if (String.IsNullOrEmpty(url)) { - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) { - Debug.Log("No tracking code set for 'Other' platforms - hit will not be sent."); - } - return; - } - if (dryRun || optOut) { - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) { - Debug.Log("Dry run or opt out enabled - hits will not be sent."); - } - return; - } - if (startSessionOnNextHit) { - url += AddOptionalMPParameter(Fields.SESSION_CONTROL, "start"); - startSessionOnNextHit = false; - } else if (endSessionOnNextHit) { - url += AddOptionalMPParameter(Fields.SESSION_CONTROL, "end"); - endSessionOnNextHit = false; - } - // Add random z to avoid caching - string newUrl = url + "&z=" + UnityEngine.Random.Range(0, 500); - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) { - Debug.Log(newUrl); - } - GoogleAnalyticsV3.getInstance().StartCoroutine(this.HandleWWW(new WWW(newUrl))); - } - - /* - Make request using yield and coroutine to prevent lock up waiting on request to return. - */ - public IEnumerator HandleWWW(WWW request) - { - while (!request.isDone) - { - yield return request; - if (request.responseHeaders.ContainsKey("STATUS")) { - if (request.responseHeaders["STATUS"].Contains("200 OK")) { - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.INFO)) { - Debug.Log("Successfully sent Google Analytics hit."); - } - } else { - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) { - Debug.LogWarning("Google Analytics hit request rejected with " + - "status code " + request.responseHeaders["STATUS"]); - } - } - } else { - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) { - Debug.LogWarning("Google Analytics hit request failed with error " - + request.error); - } - } - } - } - - private string AddRequiredMPParameter(Field parameter, object value) { - if(!trackingCodeSet){ - return ""; - } else if (value == null) { - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) { - Debug.LogWarning("Value was null for required parameter " + parameter + ". Hit cannot be sent"); - } - throw new ArgumentNullException(); - } else { - return parameter + "=" + WWW.EscapeURL(value.ToString()); - } - } - - private string AddRequiredMPParameter(Field parameter, string value) { - if(!trackingCodeSet){ - return ""; - } else if (value == null) { - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.WARNING)) { - Debug.LogWarning("Value was null for required parameter " + parameter + ". Hit cannot be sent"); - } - throw new ArgumentNullException(); - } else { - return parameter + "=" + WWW.EscapeURL(value); - } - } - - private string AddOptionalMPParameter(Field parameter, object value) { - if (value == null || !trackingCodeSet) { - return ""; - } else { - return parameter + "=" + WWW.EscapeURL(value.ToString()); - } - } - - private string AddOptionalMPParameter(Field parameter, string value) { - if (String.IsNullOrEmpty(value) || !trackingCodeSet) { - return ""; - } else { - return parameter + "=" + WWW.EscapeURL(value); - } - } - - private string AddCustomVariables(HitBuilder builder) { - if(!trackingCodeSet){ - return ""; - } - String url = ""; - foreach(KeyValuePair entry in builder.GetCustomDimensions()) - { - if (entry.Value != null) { - url += Fields.CUSTOM_DIMENSION.ToString() + entry.Key + "=" + - WWW.EscapeURL(entry.Value.ToString()); - } - } - foreach(KeyValuePair entry in builder.GetCustomMetrics()) - { - if (entry.Value != null) { - url += Fields.CUSTOM_METRIC.ToString() + entry.Key + "=" + - WWW.EscapeURL(entry.Value.ToString()); - } - } - - if(!String.IsNullOrEmpty(url)){ - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) { - Debug.Log("Added custom variables to hit."); - } - } - return url; - } - - - private string AddCampaignParameters(HitBuilder builder) { - if(!trackingCodeSet){ - return ""; - } - String url = ""; - url += AddOptionalMPParameter(Fields.CAMPAIGN_NAME, builder.GetCampaignName()); - url += AddOptionalMPParameter(Fields.CAMPAIGN_SOURCE, builder.GetCampaignSource()); - url += AddOptionalMPParameter(Fields.CAMPAIGN_MEDIUM, builder.GetCampaignMedium()); - url += AddOptionalMPParameter(Fields.CAMPAIGN_KEYWORD, builder.GetCampaignKeyword()); - url += AddOptionalMPParameter(Fields.CAMPAIGN_CONTENT, builder.GetCampaignContent()); - url += AddOptionalMPParameter(Fields.CAMPAIGN_ID, builder.GetCampaignID()); - url += AddOptionalMPParameter(Fields.GCLID, builder.GetGclid()); - url += AddOptionalMPParameter(Fields.DCLID, builder.GetDclid()); - - if(!String.IsNullOrEmpty(url)){ - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) { - Debug.Log("Added campaign parameters to hit. url:" + url); - } - } - return url; - } - - public void LogScreen(AppViewHitBuilder builder) { - trackerValues[Fields.SCREEN_NAME] = null; - - SendGaHitWithMeasurementProtocol(url - + AddRequiredMPParameter(Fields.HIT_TYPE, "appview") - + AddRequiredMPParameter(Fields.SCREEN_NAME, builder.GetScreenName()) + try + { + url = "https://www.google-analytics.com/collect?v=1" + + AddRequiredMPParameter(Fields.LANGUAGE,language) + + AddRequiredMPParameter(Fields.SCREEN_RESOLUTION,screenRes) + + AddRequiredMPParameter(Fields.APP_NAME,appName) + + AddRequiredMPParameter(Fields.TRACKING_ID,trackingCode) + + AddRequiredMPParameter(Fields.APP_ID,bundleIdentifier) + + AddRequiredMPParameter(Fields.CLIENT_ID,clientId) + + AddRequiredMPParameter(Fields.APP_VERSION,appVersion); + if(anonymizeIP) + { + url += AddOptionalMPParameter(Fields.ANONYMIZE_IP,1); + } + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) + { + Debug.Log("Base URL for hits: " + url); + } + } + catch(Exception) + { + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.WARNING)) + { + Debug.Log("Error building url."); + } + } + } + + public void SetTrackerVal(string field,object value) + { + trackerValues[field] = value; + } + + private string AddTrackerVals() + { + if(!trackingCodeSet) + { + return ""; + } + string vals = ""; + foreach(var pair in trackerValues) + { + vals += AddOptionalMPParameter(pair.Key,pair.Value); + } + return vals; + } + + internal void StartSession() + { + startSessionOnNextHit = true; + } + + internal void StopSession() + { + endSessionOnNextHit = true; + } + + private void SendGaHitWithMeasurementProtocol(string url) + { + if(String.IsNullOrEmpty(url)) + { + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.WARNING)) + Debug.Log("No tracking code set for 'Other' platforms - hit will not be sent."); + + return; + } + + if(dryRun || optOut) + { + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.WARNING)) + Debug.Log("Dry run or opt out enabled - hits will not be sent."); + + return; + } + + if(startSessionOnNextHit) + { + url += AddOptionalMPParameter(Fields.SESSION_CONTROL,"start"); + startSessionOnNextHit = false; + } + else if(endSessionOnNextHit) + { + url += AddOptionalMPParameter(Fields.SESSION_CONTROL,"end"); + endSessionOnNextHit = false; + } + + // Add random z to avoid caching + string newUrl = url + "&z=" + UnityEngine.Random.Range(0,500); + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) + Debug.Log(newUrl); + + GoogleAnalyticsV3.getInstance().StartCoroutine(this.HandleWWW(new WWW(newUrl))); + } + + /* + Make request using yield and coroutine to prevent lock up waiting on request to return. + */ + public IEnumerator HandleWWW(WWW request) + { + while(!request.isDone) + { + yield return request; + + string status; + if(request.responseHeaders.TryGetValue("STATUS",out status)) + { + if(status.Contains("200 OK")) + { + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.INFO)) + Debug.Log("Successfully sent Google Analytics hit."); + } + else + { + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.WARNING)) + Debug.LogWarning("Google Analytics hit request rejected with status code " + request.responseHeaders["STATUS"]); + } + } + else + { + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.WARNING)) + Debug.LogWarning("Google Analytics hit request failed with error " + request.error); + } + } + } + + private string AddRequiredMPParameter(string parameter,object value) + { + if(!trackingCodeSet) + return ""; + + if(value == null) + { + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.WARNING)) + Debug.LogWarning("Value was null for required parameter " + parameter + ". Hit cannot be sent"); + + throw new ArgumentNullException(); + } + + return parameter + "=" + WWW.EscapeURL(value.ToString()); + } + + private string AddRequiredMPParameter(string parameter,string value) + { + if(!trackingCodeSet) + return ""; + + if(value == null) + { + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.WARNING)) + Debug.LogWarning("Value was null for required parameter " + parameter + ". Hit cannot be sent"); + + throw new ArgumentNullException(); + } + + return parameter + "=" + WWW.EscapeURL(value); + } + + private string AddOptionalMPParameter(string parameter,object value) + { + if(value == null || !trackingCodeSet) + return ""; + + return parameter + "=" + WWW.EscapeURL(value.ToString()); + } + + private string AddOptionalMPParameter(string parameter,string value) + { + if(String.IsNullOrEmpty(value) || !trackingCodeSet) + return ""; + + return parameter + "=" + WWW.EscapeURL(value); + } + + private string AddCustomVariables(HitBuilder builder) + { + if(!trackingCodeSet) + { + return ""; + } + + string url = ""; + foreach(var entry in builder.CustomDimensions) + { + if(entry.Value != null) + url += Fields.CUSTOM_DIMENSION.ToString() + entry.Key + "=" + WWW.EscapeURL(entry.Value.ToString()); + } + + foreach(var entry in builder.CustomMetrics) + { + if(entry.Value != null) + url += Fields.CUSTOM_METRIC.ToString() + entry.Key + "=" + WWW.EscapeURL(entry.Value.ToString()); + } + + if(!string.IsNullOrEmpty(url)) + { + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) + Debug.Log("Added custom variables to hit."); + } + + return url; + } + + + private string AddCampaignParameters(HitBuilder builder) + { + if(!trackingCodeSet) + { + return ""; + } + String url = ""; + url += AddOptionalMPParameter(Fields.CAMPAIGN_NAME,builder.GetCampaignName()); + url += AddOptionalMPParameter(Fields.CAMPAIGN_SOURCE,builder.GetCampaignSource()); + url += AddOptionalMPParameter(Fields.CAMPAIGN_MEDIUM,builder.GetCampaignMedium()); + url += AddOptionalMPParameter(Fields.CAMPAIGN_KEYWORD,builder.GetCampaignKeyword()); + url += AddOptionalMPParameter(Fields.CAMPAIGN_CONTENT,builder.GetCampaignContent()); + url += AddOptionalMPParameter(Fields.CAMPAIGN_ID,builder.GetCampaignID()); + url += AddOptionalMPParameter(Fields.GCLID,builder.GetGclid()); + url += AddOptionalMPParameter(Fields.DCLID,builder.GetDclid()); + + if(!String.IsNullOrEmpty(url)) + { + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) + Debug.Log("Added campaign parameters to hit. url:" + url); + } + + return url; + } + + public void LogScreen(AppViewHitBuilder builder) + { + trackerValues[Fields.SCREEN_NAME] = null; + + SendGaHitWithMeasurementProtocol(url + + AddRequiredMPParameter(Fields.HIT_TYPE,"appview") + + AddRequiredMPParameter(Fields.SCREEN_NAME,builder.GetScreenName()) + AddCustomVariables(builder) + AddCampaignParameters(builder) + AddTrackerVals()); - } - - public void LogEvent(EventHitBuilder builder) { - trackerValues[Fields.EVENT_CATEGORY] = null; - trackerValues[Fields.EVENT_ACTION] = null; - trackerValues[Fields.EVENT_LABEL] = null; - trackerValues[Fields.EVENT_VALUE] = null; - - SendGaHitWithMeasurementProtocol(url - + AddRequiredMPParameter(Fields.HIT_TYPE, "event") - + AddOptionalMPParameter(Fields.EVENT_CATEGORY, builder.GetEventCategory()) - + AddOptionalMPParameter(Fields.EVENT_ACTION, builder.GetEventAction()) - + AddOptionalMPParameter(Fields.EVENT_LABEL, builder.GetEventLabel()) - + AddOptionalMPParameter(Fields.EVENT_VALUE, builder.GetEventValue()) + } + + public void LogEvent(EventHitBuilder builder) + { + trackerValues[Fields.EVENT_CATEGORY] = null; + trackerValues[Fields.EVENT_ACTION] = null; + trackerValues[Fields.EVENT_LABEL] = null; + trackerValues[Fields.EVENT_VALUE] = null; + + SendGaHitWithMeasurementProtocol(url + + AddRequiredMPParameter(Fields.HIT_TYPE,"event") + + AddOptionalMPParameter(Fields.EVENT_CATEGORY,builder.GetEventCategory()) + + AddOptionalMPParameter(Fields.EVENT_ACTION,builder.GetEventAction()) + + AddOptionalMPParameter(Fields.EVENT_LABEL,builder.GetEventLabel()) + + AddOptionalMPParameter(Fields.EVENT_VALUE,builder.GetEventValue()) + AddCustomVariables(builder) + AddCampaignParameters(builder) + AddTrackerVals()); - } - - public void LogTransaction(TransactionHitBuilder builder) { - trackerValues[Fields.TRANSACTION_ID] = null; - trackerValues[Fields.TRANSACTION_AFFILIATION] = null; - trackerValues[Fields.TRANSACTION_REVENUE] = null; - trackerValues[Fields.TRANSACTION_SHIPPING] = null; - trackerValues[Fields.TRANSACTION_TAX] = null; - trackerValues[Fields.CURRENCY_CODE] = null; - - SendGaHitWithMeasurementProtocol(url - + AddRequiredMPParameter(Fields.HIT_TYPE, "transaction") - + AddRequiredMPParameter(Fields.TRANSACTION_ID, builder.GetTransactionID()) - + AddOptionalMPParameter(Fields.TRANSACTION_AFFILIATION, builder.GetAffiliation()) - + AddOptionalMPParameter(Fields.TRANSACTION_REVENUE, builder.GetRevenue()) - + AddOptionalMPParameter(Fields.TRANSACTION_SHIPPING, builder.GetShipping()) - + AddOptionalMPParameter(Fields.TRANSACTION_TAX, builder.GetTax()) - + AddOptionalMPParameter(Fields.CURRENCY_CODE, builder.GetCurrencyCode()) + } + + public void LogTransaction(TransactionHitBuilder builder) + { + trackerValues[Fields.TRANSACTION_ID] = null; + trackerValues[Fields.TRANSACTION_AFFILIATION] = null; + trackerValues[Fields.TRANSACTION_REVENUE] = null; + trackerValues[Fields.TRANSACTION_SHIPPING] = null; + trackerValues[Fields.TRANSACTION_TAX] = null; + trackerValues[Fields.CURRENCY_CODE] = null; + + SendGaHitWithMeasurementProtocol(url + + AddRequiredMPParameter(Fields.HIT_TYPE,"transaction") + + AddRequiredMPParameter(Fields.TRANSACTION_ID,builder.GetTransactionID()) + + AddOptionalMPParameter(Fields.TRANSACTION_AFFILIATION,builder.GetAffiliation()) + + AddOptionalMPParameter(Fields.TRANSACTION_REVENUE,builder.GetRevenue()) + + AddOptionalMPParameter(Fields.TRANSACTION_SHIPPING,builder.GetShipping()) + + AddOptionalMPParameter(Fields.TRANSACTION_TAX,builder.GetTax()) + + AddOptionalMPParameter(Fields.CURRENCY_CODE,builder.GetCurrencyCode()) + AddCustomVariables(builder) + AddCampaignParameters(builder) + AddTrackerVals()); - } - - public void LogItem(ItemHitBuilder builder) { - - trackerValues[Fields.TRANSACTION_ID] = null; - trackerValues[Fields.ITEM_NAME] = null; - trackerValues[Fields.ITEM_SKU] = null; - trackerValues[Fields.ITEM_CATEGORY] = null; - trackerValues[Fields.ITEM_PRICE] = null; - trackerValues[Fields.ITEM_QUANTITY] = null; - trackerValues[Fields.CURRENCY_CODE] = null; - - SendGaHitWithMeasurementProtocol(url - + AddRequiredMPParameter(Fields.HIT_TYPE, "item") - + AddRequiredMPParameter(Fields.TRANSACTION_ID, builder.GetTransactionID()) - + AddRequiredMPParameter(Fields.ITEM_NAME, builder.GetName()) - + AddOptionalMPParameter(Fields.ITEM_SKU, builder.GetSKU()) - + AddOptionalMPParameter(Fields.ITEM_CATEGORY, builder.GetCategory()) - + AddOptionalMPParameter(Fields.ITEM_PRICE, builder.GetPrice()) - + AddOptionalMPParameter(Fields.ITEM_QUANTITY, builder.GetQuantity()) - + AddOptionalMPParameter(Fields.CURRENCY_CODE, builder.GetCurrencyCode()) + } + + public void LogItem(ItemHitBuilder builder) + { + trackerValues[Fields.TRANSACTION_ID] = null; + trackerValues[Fields.ITEM_NAME] = null; + trackerValues[Fields.ITEM_SKU] = null; + trackerValues[Fields.ITEM_CATEGORY] = null; + trackerValues[Fields.ITEM_PRICE] = null; + trackerValues[Fields.ITEM_QUANTITY] = null; + trackerValues[Fields.CURRENCY_CODE] = null; + + SendGaHitWithMeasurementProtocol(url + + AddRequiredMPParameter(Fields.HIT_TYPE,"item") + + AddRequiredMPParameter(Fields.TRANSACTION_ID,builder.GetTransactionID()) + + AddRequiredMPParameter(Fields.ITEM_NAME,builder.GetName()) + + AddOptionalMPParameter(Fields.ITEM_SKU,builder.GetSKU()) + + AddOptionalMPParameter(Fields.ITEM_CATEGORY,builder.GetCategory()) + + AddOptionalMPParameter(Fields.ITEM_PRICE,builder.GetPrice()) + + AddOptionalMPParameter(Fields.ITEM_QUANTITY,builder.GetQuantity()) + + AddOptionalMPParameter(Fields.CURRENCY_CODE,builder.GetCurrencyCode()) + AddCustomVariables(builder) + AddCampaignParameters(builder) + AddTrackerVals()); - } + } - public void LogException(ExceptionHitBuilder builder) { + public void LogException(ExceptionHitBuilder builder) + { + trackerValues[Fields.EX_DESCRIPTION] = null; + trackerValues[Fields.EX_FATAL] = null; - trackerValues[Fields.EX_DESCRIPTION] = null; - trackerValues[Fields.EX_FATAL] = null; - - SendGaHitWithMeasurementProtocol(url - + AddRequiredMPParameter(Fields.HIT_TYPE, "exception") - + AddOptionalMPParameter(Fields.EX_DESCRIPTION, builder.GetExceptionDescription()) - + AddOptionalMPParameter(Fields.EX_FATAL, builder.IsFatal()) + SendGaHitWithMeasurementProtocol(url + + AddRequiredMPParameter(Fields.HIT_TYPE,"exception") + + AddOptionalMPParameter(Fields.EX_DESCRIPTION,builder.GetExceptionDescription()) + + AddOptionalMPParameter(Fields.EX_FATAL,builder.IsFatal()) + AddTrackerVals()); - } - - public void LogSocial(SocialHitBuilder builder) { - - trackerValues[Fields.SOCIAL_NETWORK] = null; - trackerValues[Fields.SOCIAL_ACTION] = null; - trackerValues[Fields.SOCIAL_TARGET] = null; - - SendGaHitWithMeasurementProtocol(url - + AddRequiredMPParameter(Fields.HIT_TYPE, "social") - + AddRequiredMPParameter(Fields.SOCIAL_NETWORK, builder.GetSocialNetwork()) - + AddRequiredMPParameter(Fields.SOCIAL_ACTION, builder.GetSocialAction()) - + AddRequiredMPParameter(Fields.SOCIAL_TARGET, builder.GetSocialTarget()) + } + + public void LogSocial(SocialHitBuilder builder) + { + trackerValues[Fields.SOCIAL_NETWORK] = null; + trackerValues[Fields.SOCIAL_ACTION] = null; + trackerValues[Fields.SOCIAL_TARGET] = null; + + SendGaHitWithMeasurementProtocol(url + + AddRequiredMPParameter(Fields.HIT_TYPE,"social") + + AddRequiredMPParameter(Fields.SOCIAL_NETWORK,builder.GetSocialNetwork()) + + AddRequiredMPParameter(Fields.SOCIAL_ACTION,builder.GetSocialAction()) + + AddRequiredMPParameter(Fields.SOCIAL_TARGET,builder.GetSocialTarget()) + AddCustomVariables(builder) + AddCampaignParameters(builder) + AddTrackerVals()); - } - - public void LogTiming(TimingHitBuilder builder) { - - trackerValues[Fields.TIMING_CATEGORY] = null; - trackerValues[Fields.TIMING_VALUE] = null; - trackerValues[Fields.TIMING_LABEL] = null; - trackerValues[Fields.TIMING_VAR] = null; - - SendGaHitWithMeasurementProtocol(url - + AddRequiredMPParameter(Fields.HIT_TYPE, "timing") - + AddOptionalMPParameter(Fields.TIMING_CATEGORY, builder.GetTimingCategory()) - + AddOptionalMPParameter(Fields.TIMING_VALUE, builder.GetTimingInterval()) - + AddOptionalMPParameter(Fields.TIMING_LABEL, builder.GetTimingLabel()) - + AddOptionalMPParameter(Fields.TIMING_VAR, builder.GetTimingName()) + } + + public void LogTiming(TimingHitBuilder builder) + { + trackerValues[Fields.TIMING_CATEGORY] = null; + trackerValues[Fields.TIMING_VALUE] = null; + trackerValues[Fields.TIMING_LABEL] = null; + trackerValues[Fields.TIMING_VAR] = null; + + SendGaHitWithMeasurementProtocol(url + + AddRequiredMPParameter(Fields.HIT_TYPE,"timing") + + AddOptionalMPParameter(Fields.TIMING_CATEGORY,builder.GetTimingCategory()) + + AddOptionalMPParameter(Fields.TIMING_VALUE,builder.GetTimingInterval()) + + AddOptionalMPParameter(Fields.TIMING_LABEL,builder.GetTimingLabel()) + + AddOptionalMPParameter(Fields.TIMING_VAR,builder.GetTimingName()) + AddCustomVariables(builder) + AddCampaignParameters(builder) + AddTrackerVals()); - } - - public void ClearUserIDOverride() { - SetTrackerVal(Fields.USER_ID, null); - } - - public void SetTrackingCode(string trackingCode) { - this.trackingCode = trackingCode; - } - - public void SetBundleIdentifier(string bundleIdentifier) { - this.bundleIdentifier = bundleIdentifier; - } - - public void SetAppName(string appName) { - this.appName = appName; - } - - public void SetAppVersion(string appVersion) { - this.appVersion = appVersion; - } - - public void SetLogLevelValue(GoogleAnalyticsV3.DebugMode logLevel) { - this.logLevel = logLevel; - } - - public void SetAnonymizeIP(bool anonymizeIP) { - this.anonymizeIP = anonymizeIP; - } - - public void SetDryRun(bool dryRun) { - this.dryRun = dryRun; - } - - public void SetOptOut(bool optOut) { - this.optOut = optOut; - } + } + + public void ClearUserIDOverride() + { + SetTrackerVal(Fields.USER_ID,null); + } + + public void SetTrackingCode(string trackingCode) + { + this.trackingCode = trackingCode; + } + + public void SetBundleIdentifier(string bundleIdentifier) + { + this.bundleIdentifier = bundleIdentifier; + } + + public void SetAppName(string appName) + { + this.appName = appName; + } + + public void SetAppVersion(string appVersion) + { + this.appVersion = appVersion; + } + + public void SetLogLevelValue(GoogleAnalyticsV3.DebugMode logLevel) + { + this.logLevel = logLevel; + } + + public void SetAnonymizeIP(bool anonymizeIP) + { + this.anonymizeIP = anonymizeIP; + } + + public void SetDryRun(bool dryRun) + { + this.dryRun = dryRun; + } + + public void SetOptOut(bool optOut) + { + this.optOut = optOut; + } #endif -} +} \ No newline at end of file diff --git a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs index 95fc32f..477b8c3 100755 --- a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs +++ b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs @@ -15,7 +15,9 @@ limitations under the License. */ using UnityEngine; + using System; +using System.Linq; using System.Collections; using System.Collections.Generic; @@ -30,466 +32,409 @@ The class will delegate the hits to the appropriate helper class depending on pass a builder to the same method name in order to add custom metrics or custom dimensions to the hit. */ -public class GoogleAnalyticsV3 : MonoBehaviour { - private string uncaughtExceptionStackTrace = null; - private bool initialized = false; - - public enum DebugMode { - ERROR, - WARNING, - INFO, - VERBOSE }; - - [Tooltip("The tracking code to be used for Android. Example value: UA-XXXX-Y.")] - public string androidTrackingCode; - [Tooltip("The tracking code to be used for iOS. Example value: UA-XXXX-Y.")] - public string IOSTrackingCode; - [Tooltip("The tracking code to be used for platforms other than Android and iOS. Example value: UA-XXXX-Y.")] - public string otherTrackingCode; - - [Tooltip("The application name. This value should be modified in the " + - "Unity Player Settings.")] - public string productName; - - [Tooltip("The application identifier. Example value: com.company.app.")] - public string bundleIdentifier; - - [Tooltip("The application version. Example value: 1.2")] - public string bundleVersion; - - [RangedTooltip("The dispatch period in seconds. Only required for Android " + - "and iOS.", 0, 3600)] - public int dispatchPeriod = 5; - - [RangedTooltip("The sample rate to use. Only required for Android and" + - " iOS.", 0, 100)] - public int sampleFrequency = 100; - - [Tooltip("The log level. Default is WARNING.")] - public DebugMode logLevel = DebugMode.WARNING; - - [Tooltip("If checked, the IP address of the sender will be anonymized.")] - public bool anonymizeIP = false; - - [Tooltip("Automatically report uncaught exceptions.")] - public bool UncaughtExceptionReporting = false; - - [Tooltip("Automatically send a launch event when the game starts up.")] - public bool sendLaunchEvent = false; - - [Tooltip("If checked, hits will not be dispatched. Use for testing.")] - public bool dryRun = false; - - // TODO: Create conditional textbox attribute - [Tooltip("The amount of time in seconds your application can stay in" + - "the background before the session is ended. Default is 30 minutes" + - " (1800 seconds). A value of -1 will disable session management.")] - public int sessionTimeout = 1800; - - public static GoogleAnalyticsV3 instance = null; - - [HideInInspector] - public readonly static string currencySymbol = "USD"; - public readonly static string EVENT_HIT = "createEvent"; - public readonly static string APP_VIEW = "createAppView"; - public readonly static string SET = "set"; - public readonly static string SET_ALL = "setAll"; - public readonly static string SEND = "send"; - public readonly static string ITEM_HIT = "createItem"; - public readonly static string TRANSACTION_HIT = "createTransaction"; - public readonly static string SOCIAL_HIT = "createSocial"; - public readonly static string TIMING_HIT = "createTiming"; - public readonly static string EXCEPTION_HIT = "createException"; +public class GoogleAnalyticsV3 : MonoBehaviour +{ + private string uncaughtExceptionStackTrace = null; + private bool initialized = false; + + public enum DebugMode + { + ERROR = 1, + WARNING = 2, + INFO = 3, + VERBOSE = 4 + } + + static GoogleAnalyticsV3 instance = null; + // Instance for running Coroutines from platform specific classes + public static GoogleAnalyticsV3 getInstance() + { + if(instance == null) + { + instance = GameObject.FindObjectOfType(); + if(instance == null) + { + var prefab = Resources.FindObjectsOfTypeAll().FirstOrDefault(); + instance = GameObject.Instantiate(prefab).GetComponent(); + } + } + + return instance; + } + + [Tooltip("The tracking code to be used for Android. Example value: UA-XXXX-Y.")] + public string androidTrackingCode; + [Tooltip("The tracking code to be used for iOS. Example value: UA-XXXX-Y.")] + public string IOSTrackingCode; + [Tooltip("The tracking code to be used for platforms other than Android and iOS. Example value: UA-XXXX-Y.")] + public string otherTrackingCode; + + [Tooltip("The application name. This value should be modified in the Unity Player Settings.")] + public string productName; + + [Tooltip("The application identifier. Example value: com.company.app.")] + public string bundleIdentifier; + + [Tooltip("The application version. Example value: 1.2")] + public string bundleVersion; + + [RangedTooltip("The dispatch period in seconds. Only required for Android and iOS.",0,3600)] + public int dispatchPeriod = 5; + + [RangedTooltip("The sample rate to use. Only required for Android and iOS.",0,100)] + public int sampleFrequency = 100; + + [Tooltip("The log level. Default is WARNING.")] + public DebugMode logLevel = DebugMode.WARNING; + + [Tooltip("If checked, the IP address of the sender will be anonymized.")] + public bool anonymizeIP = false; + + [Tooltip("Automatically report uncaught exceptions.")] + public bool UncaughtExceptionReporting = false; + + [Tooltip("Automatically send a launch event when the game starts up.")] + public bool sendLaunchEvent = false; + + [Tooltip("If checked, hits will not be dispatched. Use for testing.")] + public bool dryRun = false; + + // TODO: Create conditional textbox attribute + [Tooltip("The amount of time in seconds your application can stay in the background before the session is ended. Default is 30 minutes (1800 seconds). A value of -1 will disable session management.")] + public int sessionTimeout = 1800; + + [Tooltip("Support only standard currency symbal")] + public string currencySymbol = "USD"; + + public readonly static string EVENT_HIT = "createEvent"; + public readonly static string APP_VIEW = "createAppView"; + public readonly static string SET = "set"; + public readonly static string SET_ALL = "setAll"; + public readonly static string SEND = "send"; + public readonly static string ITEM_HIT = "createItem"; + public readonly static string TRANSACTION_HIT = "createTransaction"; + public readonly static string SOCIAL_HIT = "createSocial"; + public readonly static string TIMING_HIT = "createTiming"; + public readonly static string EXCEPTION_HIT = "createException"; #if UNITY_ANDROID && !UNITY_EDITOR - private GoogleAnalyticsAndroidV3 androidTracker = - new GoogleAnalyticsAndroidV3(); + private GoogleAnalyticsAndroidV3 tracker = new GoogleAnalyticsAndroidV3(); #elif UNITY_IPHONE && !UNITY_EDITOR - private GoogleAnalyticsiOSV3 iosTracker = new GoogleAnalyticsiOSV3(); + private GoogleAnalyticsiOSV3 tracker = new GoogleAnalyticsiOSV3(); #else - private GoogleAnalyticsMPV3 mpTracker = new GoogleAnalyticsMPV3(); + private GoogleAnalyticsMPV3 tracker = new GoogleAnalyticsMPV3(); #endif - void Awake() { - InitializeTracker (); + void Awake() + { + InitializeTracker(); - if (sendLaunchEvent) { - LogEvent("Google Analytics", "Auto Instrumentation", "Game Launch", 0); - } + if(sendLaunchEvent) + { + LogEvent("Google Analytics","Auto Instrumentation","Game Launch",0); + } - if (UncaughtExceptionReporting) { -#if UNITY_5_0 - Application.logMessageReceived += HandleException; + if(UncaughtExceptionReporting) + { +#if UNITY_5 + Application.logMessageReceived += HandleException; #else - Application.RegisterLogCallback (HandleException); + Application.RegisterLogCallback(HandleException); #endif - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) { - Debug.Log("Enabling uncaught exception reporting."); - } - } - } - - void Update() { - if (uncaughtExceptionStackTrace != null) { - LogException(uncaughtExceptionStackTrace, true); - uncaughtExceptionStackTrace = null; - } - } - - private void HandleException(string condition, string stackTrace, LogType type) { - if (type == LogType.Exception) { - uncaughtExceptionStackTrace = condition + "\n" + stackTrace + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) + Debug.Log("Enabling uncaught exception reporting."); + } + } + + void Update() + { + if(uncaughtExceptionStackTrace != null) + { + LogException(uncaughtExceptionStackTrace,true); + uncaughtExceptionStackTrace = null; + } + } + + private void HandleException(string condition,string stackTrace,LogType type) + { + if(type == LogType.Exception) + { + uncaughtExceptionStackTrace = condition + "\n" + stackTrace + UnityEngine.StackTraceUtility.ExtractStackTrace(); - } - } - - // TODO: Error checking on initialization parameters - private void InitializeTracker() { - if (!initialized) { - instance = this; - - DontDestroyOnLoad(instance); - - Debug.Log("Initializing Google Analytics 0.1."); -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.SetTrackingCode(androidTrackingCode); - androidTracker.SetAppName(productName); - androidTracker.SetBundleIdentifier(bundleIdentifier); - androidTracker.SetAppVersion(bundleVersion); - androidTracker.SetDispatchPeriod(dispatchPeriod); - androidTracker.SetSampleFrequency(sampleFrequency); - androidTracker.SetLogLevelValue(logLevel); - androidTracker.SetAnonymizeIP(anonymizeIP); - androidTracker.SetDryRun(dryRun); - androidTracker.InitializeTracker(); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.SetTrackingCode(IOSTrackingCode); - iosTracker.SetAppName(productName); - iosTracker.SetBundleIdentifier(bundleIdentifier); - iosTracker.SetAppVersion(bundleVersion); - iosTracker.SetDispatchPeriod(dispatchPeriod); - iosTracker.SetSampleFrequency(sampleFrequency); - iosTracker.SetLogLevelValue(logLevel); - iosTracker.SetAnonymizeIP(anonymizeIP); - iosTracker.SetDryRun(dryRun); - iosTracker.InitializeTracker(); -#else - mpTracker.SetTrackingCode(otherTrackingCode); - mpTracker.SetBundleIdentifier(bundleIdentifier); - mpTracker.SetAppName(productName); - mpTracker.SetAppVersion(bundleVersion); - mpTracker.SetLogLevelValue(logLevel); - mpTracker.SetAnonymizeIP(anonymizeIP); - mpTracker.SetDryRun(dryRun); - mpTracker.InitializeTracker(); + } + } + + // TODO: Error checking on initialization parameters + private void InitializeTracker() + { + if(initialized) + return; + + instance = this; + DontDestroyOnLoad(instance); + + Debug.Log("Initializing Google Analytics 0.1."); + + tracker.SetAppName(productName); + tracker.SetTrackingCode(otherTrackingCode); + tracker.SetBundleIdentifier(bundleIdentifier); + tracker.SetAppVersion(bundleVersion); + tracker.SetLogLevelValue(logLevel); + tracker.SetAnonymizeIP(anonymizeIP); +#if !UNITY_EDITOR + tracker.SetDispatchPeriod(dispatchPeriod); + tracker.SetSampleFrequency(sampleFrequency); #endif - initialized = true; - SetOnTracker(Fields.DEVELOPER_ID, "GbOCSs"); - } - } - public void SetAppLevelOptOut(bool optOut) { - InitializeTracker(); -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.SetOptOut(optOut); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.SetOptOut(optOut); -#else - mpTracker.SetOptOut(optOut); + tracker.SetDryRun(dryRun); + tracker.InitializeTracker(); + + initialized = true; + SetOnTracker(Fields.DEVELOPER_ID,"GbOCSs"); + SetOnTracker(Fields.CLIENT_ID,SystemInfo.deviceUniqueIdentifier); + } + + public void SetAppLevelOptOut(bool optOut) + { + InitializeTracker(); + tracker.SetOptOut(optOut); + } + + public void SetUserIDOverride(string userID) + { + SetOnTracker(Fields.USER_ID,userID); + } + + public void ClearUserIDOverride() + { + InitializeTracker(); + tracker.ClearUserIDOverride(); + } + + public void DispatchHits() + { + InitializeTracker(); +#if !UNITY_EDITOR + tracker.DispatchHits(); #endif - } - - public void SetUserIDOverride(string userID) { - SetOnTracker(Fields.USER_ID, userID); - } - - public void ClearUserIDOverride() { - InitializeTracker(); + } + + public void StartSession() + { + InitializeTracker(); + tracker.StartSession(); + } + + public void StopSession() + { + InitializeTracker(); + tracker.StopSession(); + } + + // Use values from Fields for the fieldName parameter ie. Fields.SCREEN_NAME + public void SetOnTracker(string fieldName,object value) + { + InitializeTracker(); + tracker.SetTrackerVal(fieldName,value); + } + + public void LogScreen(string title) + { + LogScreen(new AppViewHitBuilder(title)); + } + + public void LogScreen(AppViewHitBuilder builder) + { + InitializeTracker(); + if(builder.Validate() == null) + { + return; + } + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) + { + Debug.Log("Logging screen."); + } + + tracker.LogScreen(builder); + } + + public void LogEvent(string eventCategory,string eventAction,string eventLabel,long value) + { + EventHitBuilder builder = new EventHitBuilder() + .SetEventCategory(eventCategory) + .SetEventAction(eventAction) + .SetEventLabel(eventLabel) + .SetEventValue(value); + + LogEvent(builder); + } + + public void LogEvent(EventHitBuilder builder) + { + InitializeTracker(); + if(builder.Validate() == null) + { + return; + } + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) + { + Debug.Log("Logging event."); + } + + tracker.LogEvent(builder); + } + + public void LogTransaction(string transID,string affiliation,double revenue,double tax,double shipping) + { + LogTransaction(transID,affiliation,revenue,tax,shipping,""); + } + + public void LogTransaction(string transID,string affiliation,double revenue,double tax,double shipping,string currencyCode) + { + TransactionHitBuilder builder = new TransactionHitBuilder() + .SetTransactionID(transID) + .SetAffiliation(affiliation) + .SetRevenue(revenue) + .SetTax(tax) + .SetShipping(shipping) + .SetCurrencyCode(currencyCode); + + LogTransaction(builder); + } + + public void LogTransaction(TransactionHitBuilder builder) + { + InitializeTracker(); + if(builder.Validate() == null) + { + return; + } + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) + { + Debug.Log("Logging transaction."); + } + + tracker.LogTransaction(builder); + } + + public void LogItem(string transID,string name,string sku,string category,double price,long quantity) + { + LogItem(transID,name,sku,category,price,quantity,null); + } + + public void LogItem(string transID,string name,string sku,string category,double price,long quantity,string currencyCode) + { + ItemHitBuilder builder = new ItemHitBuilder() + .SetTransactionID(transID) + .SetName(name) + .SetSKU(sku) + .SetCategory(category) + .SetPrice(price) + .SetQuantity(quantity) + .SetCurrencyCode(currencyCode); + + LogItem(builder); + } + + public void LogItem(ItemHitBuilder builder) + { + InitializeTracker(); + if(builder.Validate() == null) + { + return; + } + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) + { + Debug.Log("Logging item."); + } + + tracker.LogItem(builder); + } + + public void LogException(string exceptionDescription,bool isFatal) + { + ExceptionHitBuilder builder = new ExceptionHitBuilder() + .SetExceptionDescription(exceptionDescription) + .SetFatal(isFatal); + + LogException(builder); + } + + public void LogException(ExceptionHitBuilder builder) + { + InitializeTracker(); + if(builder.Validate() == null) + { + return; + } + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) + { + Debug.Log("Logging exception."); + } + + tracker.LogException(builder); + } + + public void LogSocial(string socialNetwork,string socialAction, + string socialTarget) + { + SocialHitBuilder builder = new SocialHitBuilder() + .SetSocialNetwork(socialNetwork) + .SetSocialAction(socialAction) + .SetSocialTarget(socialTarget); + + LogSocial(builder); + } + + public void LogSocial(SocialHitBuilder builder) + { + InitializeTracker(); + if(builder.Validate() == null) + { + return; + } + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) + { + Debug.Log("Logging social."); + } + + tracker.LogSocial(builder); + } + + public void LogTiming(string timingCategory,long timingInterval, + string timingName,string timingLabel) + { + TimingHitBuilder builder = new TimingHitBuilder() + .SetTimingCategory(timingCategory) + .SetTimingInterval(timingInterval) + .SetTimingName(timingName) + .SetTimingLabel(timingLabel); + + LogTiming(builder); + } + + public void LogTiming(TimingHitBuilder builder) + { + InitializeTracker(); + if(builder.Validate() == null) + return; + + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) + Debug.Log("Logging timing."); + + tracker.LogTiming(builder); + } + + public void Dispose() + { + initialized = false; #if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.ClearUserIDOverride(); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.ClearUserIDOverride(); -#else - mpTracker.ClearUserIDOverride(); + tracker.Dispose(); #endif - } + } -public void DispatchHits() { - InitializeTracker(); -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.DispatchHits(); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.DispatchHits(); -#else - //Do nothing -#endif -} - - public void StartSession() { - InitializeTracker(); -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.StartSession(); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.StartSession(); -#else - mpTracker.StartSession(); -#endif - } - - public void StopSession() { - InitializeTracker(); -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.StopSession(); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.StopSession(); -#else - mpTracker.StopSession(); -#endif - } - - // Use values from Fields for the fieldName parameter ie. Fields.SCREEN_NAME - public void SetOnTracker(Field fieldName, object value) { - InitializeTracker(); -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.SetTrackerVal(fieldName, value); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.SetTrackerVal(fieldName, value); -#else - mpTracker.SetTrackerVal(fieldName, value); -#endif - } - - public void LogScreen(string title) { - AppViewHitBuilder builder = new AppViewHitBuilder().SetScreenName(title); - LogScreen(builder); - } - - public void LogScreen(AppViewHitBuilder builder) { - InitializeTracker(); - if (builder.Validate() == null) { - return; - } - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) { - Debug.Log("Logging screen."); - } -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.LogScreen(builder); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.LogScreen(builder); -#else - mpTracker.LogScreen(builder); -#endif - } - - public void LogEvent(string eventCategory, string eventAction, - string eventLabel, long value) { - EventHitBuilder builder = new EventHitBuilder() - .SetEventCategory(eventCategory) - .SetEventAction(eventAction) - .SetEventLabel(eventLabel) - .SetEventValue(value); - - LogEvent(builder); - } - - public void LogEvent(EventHitBuilder builder) { - InitializeTracker(); - if (builder.Validate() == null) { - return; - } - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) { - Debug.Log("Logging event."); - } -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.LogEvent (builder); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.LogEvent(builder); -#else - mpTracker.LogEvent(builder); -#endif - } - - public void LogTransaction(string transID, string affiliation, - double revenue, double tax, double shipping) { - LogTransaction (transID, affiliation, revenue, tax, shipping, ""); - } - - public void LogTransaction(string transID, string affiliation, - double revenue, double tax, double shipping, string currencyCode) { - TransactionHitBuilder builder = new TransactionHitBuilder() - .SetTransactionID(transID) - .SetAffiliation(affiliation) - .SetRevenue(revenue) - .SetTax(tax) - .SetShipping(shipping) - .SetCurrencyCode(currencyCode); - - LogTransaction(builder); - } - - public void LogTransaction(TransactionHitBuilder builder) { - InitializeTracker(); - if (builder.Validate() == null) { - return; - } - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) { - Debug.Log("Logging transaction."); - } -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.LogTransaction(builder); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.LogTransaction(builder); -#else - mpTracker.LogTransaction(builder); - #endif - } - - public void LogItem(string transID, string name, string sku, - string category, double price, long quantity) { - LogItem (transID, name, sku, category, price, quantity, null); - } - - public void LogItem(string transID, string name, string sku, - string category, double price, long quantity, string currencyCode) { - ItemHitBuilder builder = new ItemHitBuilder() - .SetTransactionID(transID) - .SetName(name) - .SetSKU(sku) - .SetCategory(category) - .SetPrice(price) - .SetQuantity(quantity) - .SetCurrencyCode(currencyCode); - - LogItem(builder); - } - - public void LogItem(ItemHitBuilder builder) { - InitializeTracker(); - if (builder.Validate() == null) { - return; - } - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) { - Debug.Log("Logging item."); - } -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.LogItem(builder); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.LogItem(builder); -#else - mpTracker.LogItem(builder); -#endif - } - - public void LogException(string exceptionDescription, bool isFatal) { - ExceptionHitBuilder builder = new ExceptionHitBuilder() - .SetExceptionDescription(exceptionDescription) - .SetFatal(isFatal); - - LogException(builder); - } - - public void LogException(ExceptionHitBuilder builder) { - InitializeTracker(); - if (builder.Validate() == null) { - return; - } - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) { - Debug.Log("Logging exception."); - } -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.LogException(builder); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.LogException(builder); -#else - mpTracker.LogException(builder); -#endif - } - - public void LogSocial(string socialNetwork, string socialAction, - string socialTarget) { - SocialHitBuilder builder = new SocialHitBuilder() - .SetSocialNetwork(socialNetwork) - .SetSocialAction(socialAction) - .SetSocialTarget(socialTarget); - - LogSocial(builder); - } - - public void LogSocial(SocialHitBuilder builder) { - InitializeTracker(); - if (builder.Validate() == null) { - return; - } - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) { - Debug.Log("Logging social."); - } -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.LogSocial(builder); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.LogSocial(builder); -#else - mpTracker.LogSocial(builder); -#endif - } - - public void LogTiming(string timingCategory, long timingInterval, - string timingName, string timingLabel) { - TimingHitBuilder builder = new TimingHitBuilder() - .SetTimingCategory(timingCategory) - .SetTimingInterval(timingInterval) - .SetTimingName(timingName) - .SetTimingLabel(timingLabel); - - LogTiming(builder); - } - - public void LogTiming(TimingHitBuilder builder) { - InitializeTracker(); - if (builder.Validate() == null) { - return; - } - if (GoogleAnalyticsV3.belowThreshold(logLevel, GoogleAnalyticsV3.DebugMode.VERBOSE)) { - Debug.Log("Logging timing."); - } -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.LogTiming(builder); -#elif UNITY_IPHONE && !UNITY_EDITOR - iosTracker.LogTiming(builder); -#else - mpTracker.LogTiming(builder); -#endif - } - - public void Dispose() { - initialized = false; -#if UNITY_ANDROID && !UNITY_EDITOR - androidTracker.Dispose(); -#elif UNITY_IPHONE && !UNITY_EDITOR -#else -#endif - } - - public static bool belowThreshold(GoogleAnalyticsV3.DebugMode userLogLevel, - GoogleAnalyticsV3.DebugMode comparelogLevel) { - if (comparelogLevel == userLogLevel) { - return true; - } else if (userLogLevel == GoogleAnalyticsV3.DebugMode.ERROR) { - return false; - } else if (userLogLevel == GoogleAnalyticsV3.DebugMode.VERBOSE) { - return true; - } else if (userLogLevel == GoogleAnalyticsV3.DebugMode.WARNING && - (comparelogLevel == GoogleAnalyticsV3.DebugMode.INFO || - comparelogLevel == GoogleAnalyticsV3.DebugMode.VERBOSE)) { - return false; - } else if (userLogLevel == GoogleAnalyticsV3.DebugMode.INFO && - (comparelogLevel == GoogleAnalyticsV3.DebugMode.VERBOSE)) { - return false; - } - return true; - } - - // Instance for running Coroutines from platform specific classes - public static GoogleAnalyticsV3 getInstance() { - return instance; - } + public static bool belowThreshold(DebugMode userLogLevel,DebugMode comparelogLevel) + { + return comparelogLevel >= userLogLevel; + } } diff --git a/source/Plugins/GoogleAnalyticsV3/HitBuilders/AppViewHitBuilder.cs b/source/Plugins/GoogleAnalyticsV3/HitBuilders/AppViewHitBuilder.cs index eff37e3..f730630 100644 --- a/source/Plugins/GoogleAnalyticsV3/HitBuilders/AppViewHitBuilder.cs +++ b/source/Plugins/GoogleAnalyticsV3/HitBuilders/AppViewHitBuilder.cs @@ -19,30 +19,30 @@ limitations under the License. using System.Collections.Generic; using System; -public class AppViewHitBuilder : HitBuilder { - - private string screenName = ""; - - public string GetScreenName() { - return screenName; - } - - public AppViewHitBuilder SetScreenName(string screenName) { - if (screenName != null) { - this.screenName = screenName; - } - return this; - } - - public override AppViewHitBuilder GetThis() { - return this; - } - - public override AppViewHitBuilder Validate() { - if (String.IsNullOrEmpty(screenName)) { - Debug.Log("No screen name provided - App View hit cannot be sent."); - return null; - } - return this; - } +public class AppViewHitBuilder : HitBuilder +{ + public string ScreenName; + public AppViewHitBuilder(string screenName) + { + ScreenName = screenName; + } + + public string GetScreenName() + { + return ScreenName; + } + + public override bool IsValid + { + get + { + if(string.IsNullOrEmpty(ScreenName)) + { + Debug.Log("No screen name provided - App View hit cannot be sent."); + return false; + } + + return true; + } + } } diff --git a/source/Plugins/GoogleAnalyticsV3/HitBuilders/EventHitBuilder.cs b/source/Plugins/GoogleAnalyticsV3/HitBuilders/EventHitBuilder.cs index 9c978b3..0dd9978 100644 --- a/source/Plugins/GoogleAnalyticsV3/HitBuilders/EventHitBuilder.cs +++ b/source/Plugins/GoogleAnalyticsV3/HitBuilders/EventHitBuilder.cs @@ -17,70 +17,85 @@ limitations under the License. using UnityEngine; using System.Collections; using System.Collections.Generic; -using System; - -public class EventHitBuilder : HitBuilder { - - private string eventCategory = ""; - private string eventAction = ""; - private string eventLabel = ""; - private long eventValue; - - public string GetEventCategory() { - return eventCategory; - } - - public EventHitBuilder SetEventCategory(string eventCategory) { - if (eventCategory != null) { - this.eventCategory = eventCategory; - } - return this; - } - - public string GetEventAction() { - return eventAction; - } - - public EventHitBuilder SetEventAction(string eventAction) { - if (eventAction != null) { - this.eventAction = eventAction; - } - return this; - } - - public string GetEventLabel() { - return eventLabel; - } - - public EventHitBuilder SetEventLabel(string eventLabel) { - if (eventLabel != null) { - this.eventLabel = eventLabel; - } - return this; - } - - public long GetEventValue() { - return eventValue; - } - - public EventHitBuilder SetEventValue(long eventValue) { - this.eventValue = eventValue; - return this; - } - - public override EventHitBuilder GetThis() { - return this; - } - - public override EventHitBuilder Validate() { - if (String.IsNullOrEmpty(eventCategory)) { - Debug.LogWarning("No event category provided - Event hit cannot be sent."); - return null; - } - if (String.IsNullOrEmpty(eventAction)) { - Debug.LogWarning("No event action provided - Event hit cannot be sent."); - return null; - } - return this; - } -} +using System; + +public class EventHitBuilder : HitBuilder +{ + private string eventCategory = ""; + private string eventAction = ""; + private string eventLabel = ""; + private long eventValue; + + public string GetEventCategory() + { + return eventCategory; + } + + public EventHitBuilder SetEventCategory(string eventCategory) + { + if(eventCategory != null) + { + this.eventCategory = eventCategory; + } + return this; + } + + public string GetEventAction() + { + return eventAction; + } + + public EventHitBuilder SetEventAction(string eventAction) + { + if(eventAction != null) + { + this.eventAction = eventAction; + } + return this; + } + + public string GetEventLabel() + { + return eventLabel; + } + + public EventHitBuilder SetEventLabel(string eventLabel) + { + if(eventLabel != null) + { + this.eventLabel = eventLabel; + } + return this; + } + + public long GetEventValue() + { + return eventValue; + } + + public EventHitBuilder SetEventValue(long eventValue) + { + this.eventValue = eventValue; + return this; + } + + public override bool IsValid + { + get + { + if(String.IsNullOrEmpty(eventCategory)) + { + Debug.LogWarning("No event category provided - Event hit cannot be sent."); + return false; + } + + if(String.IsNullOrEmpty(eventAction)) + { + Debug.LogWarning("No event action provided - Event hit cannot be sent."); + return false; + } + + return true; + } + } +} \ No newline at end of file diff --git a/source/Plugins/GoogleAnalyticsV3/HitBuilders/ExceptionHitBuilder.cs b/source/Plugins/GoogleAnalyticsV3/HitBuilders/ExceptionHitBuilder.cs index 36f2e8d..a56d574 100644 --- a/source/Plugins/GoogleAnalyticsV3/HitBuilders/ExceptionHitBuilder.cs +++ b/source/Plugins/GoogleAnalyticsV3/HitBuilders/ExceptionHitBuilder.cs @@ -17,38 +17,37 @@ limitations under the License. using UnityEngine; using System.Collections; using System.Collections.Generic; -using System; - -public class ExceptionHitBuilder : HitBuilder { - - private string exceptionDescription = ""; - private bool fatal = false; - - public string GetExceptionDescription(){ - return exceptionDescription; - } - - public ExceptionHitBuilder SetExceptionDescription(string exceptionDescription) { - if(exceptionDescription != null){ - this.exceptionDescription = exceptionDescription; - } - return this; - } - - public bool IsFatal(){ - return fatal; - } - - public ExceptionHitBuilder SetFatal(bool fatal) { - this.fatal = fatal; - return this; - } - - public override ExceptionHitBuilder GetThis(){ - return this; - } - - public override ExceptionHitBuilder Validate(){ - return this; - } +using System; + +public class ExceptionHitBuilder : HitBuilder +{ + private string exceptionDescription = ""; + private bool fatal = false; + + public string GetExceptionDescription() + { + return exceptionDescription; + } + + public ExceptionHitBuilder SetExceptionDescription(string exceptionDescription) + { + if(exceptionDescription != null) + { + this.exceptionDescription = exceptionDescription; + } + return this; + } + + public bool IsFatal() + { + return fatal; + } + + public ExceptionHitBuilder SetFatal(bool fatal) + { + this.fatal = fatal; + return this; + } + + public override bool IsValid { get { return true; } } } diff --git a/source/Plugins/GoogleAnalyticsV3/HitBuilders/HitBuilder.cs b/source/Plugins/GoogleAnalyticsV3/HitBuilders/HitBuilder.cs index 0fde4c5..eae4538 100644 --- a/source/Plugins/GoogleAnalyticsV3/HitBuilders/HitBuilder.cs +++ b/source/Plugins/GoogleAnalyticsV3/HitBuilders/HitBuilder.cs @@ -21,130 +21,29 @@ limitations under the License. /* Base class for building hits. This class stores data which can be sent with any hit type but cannot be sent independent of other hits. - */ -public abstract class HitBuilder { - - private Dictionary customDimensions = - new Dictionary(); - private Dictionary customMetrics = new Dictionary(); - - private string campaignName = ""; - private string campaignSource = ""; - private string campaignMedium = ""; - private string campaignKeyword = ""; - private string campaignContent = ""; - private string campaignID = ""; - private string gclid = ""; - private string dclid = ""; - - public abstract T GetThis(); - public abstract T Validate(); - - public T SetCustomDimension(int dimensionNumber, string value) { - customDimensions.Add(dimensionNumber, value); - return GetThis(); - } - - public Dictionary GetCustomDimensions() { - return customDimensions; - } - - public T SetCustomMetric(int metricNumber, string value) { - customMetrics.Add(metricNumber, value); - return GetThis(); - } - - public Dictionary GetCustomMetrics() { - return customMetrics; - } - - public string GetCampaignName() { - return campaignName; - } - - public T SetCampaignName(string campaignName) { - if (campaignName != null) { - this.campaignName = campaignName; - } - return GetThis(); - } - - public string GetCampaignSource() { - return campaignSource; - } - - public T SetCampaignSource(string campaignSource) { - if (campaignSource != null) { - this.campaignSource = campaignSource; - } else { - Debug.Log("Campaign source cannot be null or empty"); - } - return GetThis(); - } - - public string GetCampaignMedium() { - return campaignMedium; - } - - public T SetCampaignMedium(string campaignMedium) { - if (campaignMedium != null) { - this.campaignMedium = campaignMedium; - } - return GetThis(); - } - - public string GetCampaignKeyword() { - return campaignKeyword; - } - - public T SetCampaignKeyword(string campaignKeyword) { - if (campaignKeyword != null) { - this.campaignKeyword = campaignKeyword; - } - return GetThis(); - } - - public string GetCampaignContent() { - return campaignContent; - } - - public T SetCampaignContent(string campaignContent) { - if (campaignContent != null) { - this.campaignContent = campaignContent; - } - return GetThis(); - } - - public string GetCampaignID() { - return campaignID; - } - - public T SetCampaignID(string campaignID) { - if (campaignID != null) { - this.campaignID = campaignID; - } - return GetThis(); - } - - public string GetGclid() { - return gclid; - } - - public T SetGclid(string gclid) { - if (gclid != null) { - this.gclid = gclid; - } - return GetThis(); - } - - public string GetDclid() { - return dclid; - } - - public T SetDclid(string dclid) { - if (dclid != null) { - this.dclid = dclid; - } - return GetThis(); - } -} + */ +public abstract class HitBuilder +{ + public abstract bool IsValid { get; } + + public readonly Dictionary CustomDimensions = new Dictionary(); + public readonly Dictionary CustomMetrics = new Dictionary(); + + public string CampaignName = ""; + public string CampaignSource = ""; + public string CampaignMedium = ""; + public string CampaignKeyword = ""; + public string CampaignContent = ""; + public string CampaignID = ""; + public string gclid = ""; + public string dclid = ""; + + public string GetCampaignName() { return CampaignName; } + public string GetCampaignSource() { return CampaignSource; } + public string GetCampaignMedium() { return CampaignMedium; } + public string GetCampaignKeyword() { return CampaignKeyword; } + public string GetCampaignContent() { return CampaignContent; } + public string GetCampaignID() { return CampaignID; } + public string GetGclid() { return gclid; } + public string GetDclid() { return dclid; } +} \ No newline at end of file diff --git a/source/Plugins/GoogleAnalyticsV3/HitBuilders/ItemHitBuilder.cs b/source/Plugins/GoogleAnalyticsV3/HitBuilders/ItemHitBuilder.cs index d2c04f8..7b65c14 100644 --- a/source/Plugins/GoogleAnalyticsV3/HitBuilders/ItemHitBuilder.cs +++ b/source/Plugins/GoogleAnalyticsV3/HitBuilders/ItemHitBuilder.cs @@ -17,114 +17,139 @@ limitations under the License. using UnityEngine; using System.Collections; using System.Collections.Generic; -using System; - -public class ItemHitBuilder : HitBuilder { - - private string transactionID = ""; - private string name = ""; - private string SKU = ""; - private double price; - private string category = ""; - private long quantity; - private string currencyCode = ""; - - public string GetTransactionID() { - return transactionID; - } - - public ItemHitBuilder SetTransactionID(string transactionID) { - if(transactionID != null){ - this.transactionID = transactionID; - } - return this; - } - - public string GetName() { - return name; - } - - public ItemHitBuilder SetName(string name) { - if(name != null){ - this.name = name; - } - return this; - } - - public string GetSKU() { - return name; - } - - public ItemHitBuilder SetSKU(string SKU) { - if(SKU != null){ - this.SKU = SKU; - } - return this; - } - - public double GetPrice() { - return price; - } - - public ItemHitBuilder SetPrice(double price) { - this.price = price; - return this; - } - - public string GetCategory() { - return category; - } - - public ItemHitBuilder SetCategory(string category) { - if(category != null){ - this.category = category; - } - return this; - } - - public long GetQuantity() { - return quantity; - } - - public ItemHitBuilder SetQuantity(long quantity) { - this.quantity = quantity; - return this; - } - - public string GetCurrencyCode() { - return currencyCode; - } - - public ItemHitBuilder SetCurrencyCode(string currencyCode) { - if(currencyCode != null){ - this.currencyCode = currencyCode; - } - return this; - } - - public override ItemHitBuilder GetThis(){ - return this; - } - - public override ItemHitBuilder Validate(){ - if(String.IsNullOrEmpty(transactionID)){ - Debug.LogWarning("No transaction ID provided - Item hit cannot be sent."); - return null; - } - if(String.IsNullOrEmpty(name)){ - Debug.LogWarning("No name provided - Item hit cannot be sent."); - return null; - } - if(String.IsNullOrEmpty(SKU)){ - Debug.LogWarning("No SKU provided - Item hit cannot be sent."); - return null; - } - if(price == 0.0D){ - Debug.Log("Price in item hit is 0."); - } - if(quantity == 0L){ - Debug.Log("Quantity in item hit is 0."); - } - return this; - } -} +using System; + +public class ItemHitBuilder : HitBuilder +{ + private string transactionID = ""; + private string name = ""; + private string SKU = ""; + private double price; + private string category = ""; + private long quantity; + private string currencyCode = ""; + + public string GetTransactionID() + { + return transactionID; + } + + public ItemHitBuilder SetTransactionID(string transactionID) + { + if(transactionID != null) + { + this.transactionID = transactionID; + } + return this; + } + + public string GetName() + { + return name; + } + + public ItemHitBuilder SetName(string name) + { + if(name != null) + { + this.name = name; + } + return this; + } + + public string GetSKU() + { + return name; + } + + public ItemHitBuilder SetSKU(string SKU) + { + if(SKU != null) + { + this.SKU = SKU; + } + return this; + } + + public double GetPrice() + { + return price; + } + + public ItemHitBuilder SetPrice(double price) + { + this.price = price; + return this; + } + + public string GetCategory() + { + return category; + } + + public ItemHitBuilder SetCategory(string category) + { + if(category != null) + { + this.category = category; + } + return this; + } + + public long GetQuantity() + { + return quantity; + } + + public ItemHitBuilder SetQuantity(long quantity) + { + this.quantity = quantity; + return this; + } + + public string GetCurrencyCode() + { + return currencyCode; + } + + public ItemHitBuilder SetCurrencyCode(string currencyCode) + { + if(currencyCode != null) + { + this.currencyCode = currencyCode; + } + return this; + } + + public override bool IsValid + { + get + { + if(string.IsNullOrEmpty(transactionID)) + { + Debug.LogWarning("No transaction ID provided - Item hit cannot be sent."); + return false; + } + + if(string.IsNullOrEmpty(name)) + { + Debug.LogWarning("No name provided - Item hit cannot be sent."); + return false; + } + + if(string.IsNullOrEmpty(SKU)) + { + Debug.LogWarning("No SKU provided - Item hit cannot be sent."); + return false; + } + + if(price == 0.0D) + Debug.Log("Price in item hit is 0."); + + if(quantity == 0L) + Debug.Log("Quantity in item hit is 0."); + + return true; + } + } +} \ No newline at end of file diff --git a/source/Plugins/GoogleAnalyticsV3/HitBuilders/SocialHitBuilder.cs b/source/Plugins/GoogleAnalyticsV3/HitBuilders/SocialHitBuilder.cs index ef07407..bd1fc15 100644 --- a/source/Plugins/GoogleAnalyticsV3/HitBuilders/SocialHitBuilder.cs +++ b/source/Plugins/GoogleAnalyticsV3/HitBuilders/SocialHitBuilder.cs @@ -17,64 +17,79 @@ limitations under the License. using UnityEngine; using System.Collections; using System.Collections.Generic; -using System; - -public class SocialHitBuilder : HitBuilder { - - private string socialNetwork = ""; - private string socialAction = ""; - private string socialTarget = ""; - - public string GetSocialNetwork(){ - return socialNetwork; - } - - public SocialHitBuilder SetSocialNetwork(string socialNetwork) { - if(socialNetwork != null){ - this.socialNetwork = socialNetwork; - } - return this; - } - - public string GetSocialAction(){ - return socialAction; - } - - public SocialHitBuilder SetSocialAction(string socialAction) { - if(socialAction != null){ - this.socialAction = socialAction; - } - return this; - } - - public string GetSocialTarget(){ - return socialTarget; - } - - public SocialHitBuilder SetSocialTarget(string socialTarget) { - if(socialTarget != null){ - this.socialTarget = socialTarget; - } - return this; - } - - public override SocialHitBuilder GetThis(){ - return this; - } - - public override SocialHitBuilder Validate(){ - if(String.IsNullOrEmpty(socialNetwork)){ - Debug.LogError("No social network provided - Social hit cannot be sent"); - return null; - } - if(String.IsNullOrEmpty(socialAction)){ - Debug.LogError("No social action provided - Social hit cannot be sent"); - return null; - } - if(String.IsNullOrEmpty(socialTarget)){ - Debug.LogError("No social target provided - Social hit cannot be sent"); - return null; - } - return this; - } -} +using System; + +public class SocialHitBuilder : HitBuilder +{ + private string socialNetwork = ""; + private string socialAction = ""; + private string socialTarget = ""; + + public string GetSocialNetwork() + { + return socialNetwork; + } + + public SocialHitBuilder SetSocialNetwork(string socialNetwork) + { + if(socialNetwork != null) + { + this.socialNetwork = socialNetwork; + } + return this; + } + + public string GetSocialAction() + { + return socialAction; + } + + public SocialHitBuilder SetSocialAction(string socialAction) + { + if(socialAction != null) + { + this.socialAction = socialAction; + } + return this; + } + + public string GetSocialTarget() + { + return socialTarget; + } + + public SocialHitBuilder SetSocialTarget(string socialTarget) + { + if(socialTarget != null) + { + this.socialTarget = socialTarget; + } + return this; + } + + public override bool IsValid + { + get + { + if(String.IsNullOrEmpty(socialNetwork)) + { + Debug.LogError("No social network provided - Social hit cannot be sent"); + return false; + } + + if(String.IsNullOrEmpty(socialAction)) + { + Debug.LogError("No social action provided - Social hit cannot be sent"); + return false; + } + + if(String.IsNullOrEmpty(socialTarget)) + { + Debug.LogError("No social target provided - Social hit cannot be sent"); + return false; + } + + return true; + } + } +} \ No newline at end of file diff --git a/source/Plugins/GoogleAnalyticsV3/HitBuilders/TimingHitBuilder.cs b/source/Plugins/GoogleAnalyticsV3/HitBuilders/TimingHitBuilder.cs index d8a05c1..c257f73 100644 --- a/source/Plugins/GoogleAnalyticsV3/HitBuilders/TimingHitBuilder.cs +++ b/source/Plugins/GoogleAnalyticsV3/HitBuilders/TimingHitBuilder.cs @@ -16,69 +16,82 @@ limitations under the License. using UnityEngine; using System.Collections.Generic; -using System; - -public class TimingHitBuilder : HitBuilder { - - private string timingCategory = ""; - private long timingInterval; - private string timingName = ""; - private string timingLabel = ""; - - public string GetTimingCategory(){ - return timingCategory; - } - - public TimingHitBuilder SetTimingCategory(string timingCategory) { - if(timingCategory != null){ - this.timingCategory = timingCategory; - } - return this; - } - - public long GetTimingInterval(){ - return timingInterval; - } - - public TimingHitBuilder SetTimingInterval(long timingInterval) { - this.timingInterval = timingInterval; - return this; - } - - public string GetTimingName(){ - return timingName; - } - - public TimingHitBuilder SetTimingName(string timingName) { - if(timingName != null){ - this.timingName = timingName; - } - return this; - } - - public string GetTimingLabel(){ - return timingLabel; - } - - public TimingHitBuilder SetTimingLabel(string timingLabel) { - if(timingLabel != null){ - this.timingLabel = timingLabel; - } - return this; - } - - public override TimingHitBuilder GetThis(){ - return this; - } - - public override TimingHitBuilder Validate(){ - if(String.IsNullOrEmpty(timingCategory)){ - Debug.LogError("No timing category provided - Timing hit cannot be sent"); - return null; - } - if(timingInterval == 0L){ - Debug.Log("Interval in timing hit is 0."); - } - return this; - } -} +using System; + +public class TimingHitBuilder : HitBuilder +{ + private string timingCategory = ""; + private long timingInterval; + private string timingName = ""; + private string timingLabel = ""; + + public string GetTimingCategory() + { + return timingCategory; + } + + public TimingHitBuilder SetTimingCategory(string timingCategory) + { + if(timingCategory != null) + { + this.timingCategory = timingCategory; + } + return this; + } + + public long GetTimingInterval() + { + return timingInterval; + } + + public TimingHitBuilder SetTimingInterval(long timingInterval) + { + this.timingInterval = timingInterval; + return this; + } + + public string GetTimingName() + { + return timingName; + } + + public TimingHitBuilder SetTimingName(string timingName) + { + if(timingName != null) + { + this.timingName = timingName; + } + return this; + } + + public string GetTimingLabel() + { + return timingLabel; + } + + public TimingHitBuilder SetTimingLabel(string timingLabel) + { + if(timingLabel != null) + { + this.timingLabel = timingLabel; + } + return this; + } + + public override bool IsValid + { + get + { + if(String.IsNullOrEmpty(timingCategory)) + { + Debug.LogError("No timing category provided - Timing hit cannot be sent"); + return false; + } + + if(timingInterval == 0L) + Debug.Log("Interval in timing hit is 0."); + + return true; + } + } +} \ No newline at end of file diff --git a/source/Plugins/GoogleAnalyticsV3/HitBuilders/TransactionHitBuilder.cs b/source/Plugins/GoogleAnalyticsV3/HitBuilders/TransactionHitBuilder.cs index 80481e5..08a81af 100644 --- a/source/Plugins/GoogleAnalyticsV3/HitBuilders/TransactionHitBuilder.cs +++ b/source/Plugins/GoogleAnalyticsV3/HitBuilders/TransactionHitBuilder.cs @@ -15,101 +15,119 @@ limitations under the License. */ using UnityEngine; + using System.Collections; using System.Collections.Generic; -using System; - -public class TransactionHitBuilder : HitBuilder { - - private string transactionID = ""; - private string affiliation = ""; - private double revenue; - private double tax; - private double shipping; - private string currencyCode = ""; - - public string GetTransactionID() { - return transactionID; - } - - public TransactionHitBuilder SetTransactionID(string transactionID) { - if(transactionID != null){ - this.transactionID = transactionID; - } - return this; - } - - public string GetAffiliation() { - return affiliation; - } - - public TransactionHitBuilder SetAffiliation(string affiliation) { - if(affiliation != null){ - this.affiliation = affiliation; - } - return this; - } - - public double GetRevenue() { - return revenue; - } - - public TransactionHitBuilder SetRevenue(double revenue) { - this.revenue = revenue; - return this; - } - - public double GetTax() { - return tax; - } - - public TransactionHitBuilder SetTax(double tax) { - this.tax = tax; - return this; - } - - public double GetShipping() { - return shipping; - } - - public TransactionHitBuilder SetShipping(double shipping) { - this.shipping = shipping; - return this; - } - - public string GetCurrencyCode() { - return currencyCode; - } - - public TransactionHitBuilder SetCurrencyCode(string currencyCode) { - if(currencyCode != null){ - this.currencyCode = currencyCode; - } - return this; - } - - public override TransactionHitBuilder GetThis(){ - return this; - } - - public override TransactionHitBuilder Validate(){ - if(String.IsNullOrEmpty(transactionID)){ - Debug.LogWarning("No transaction ID provided - Transaction hit cannot be sent."); - return null; - } - if(String.IsNullOrEmpty(affiliation)){ - Debug.LogWarning("No affiliation provided - Transaction hit cannot be sent."); - return null; - } - if(revenue == 0){ - Debug.Log("Revenue in transaction hit is 0."); - } - if(tax == 0){ - Debug.Log("Tax in transaction hit is 0."); - } - if(shipping == 0){ - Debug.Log("Shipping in transaction hit is 0."); - } - return this; - } -} +using System; + +public class TransactionHitBuilder : HitBuilder +{ + private string transactionID = ""; + private string affiliation = ""; + private double revenue; + private double tax; + private double shipping; + private string currencyCode = ""; + + public string GetTransactionID() + { + return transactionID; + } + + public TransactionHitBuilder SetTransactionID(string transactionID) + { + if(transactionID != null) + { + this.transactionID = transactionID; + } + return this; + } + + public string GetAffiliation() + { + return affiliation; + } + + public TransactionHitBuilder SetAffiliation(string affiliation) + { + if(affiliation != null) + { + this.affiliation = affiliation; + } + return this; + } + + public double GetRevenue() + { + return revenue; + } + + public TransactionHitBuilder SetRevenue(double revenue) + { + this.revenue = revenue; + return this; + } + + public double GetTax() + { + return tax; + } + + public TransactionHitBuilder SetTax(double tax) + { + this.tax = tax; + return this; + } + + public double GetShipping() + { + return shipping; + } + + public TransactionHitBuilder SetShipping(double shipping) + { + this.shipping = shipping; + return this; + } + + public string GetCurrencyCode() + { + return currencyCode; + } + + public TransactionHitBuilder SetCurrencyCode(string currencyCode) + { + if(currencyCode != null) + { + this.currencyCode = currencyCode; + } + return this; + } + + public override bool IsValid + { + get + { + if(string.IsNullOrEmpty(transactionID)) + { + Debug.LogWarning("No transaction ID provided - Transaction hit cannot be sent."); + return false; + } + + if(string.IsNullOrEmpty(affiliation)) + { + Debug.LogWarning("No affiliation provided - Transaction hit cannot be sent."); + return false; + } + + if(revenue == 0) + Debug.Log("Revenue in transaction hit is 0."); + if(tax == 0) + Debug.Log("Tax in transaction hit is 0."); + if(shipping == 0) + Debug.Log("Shipping in transaction hit is 0."); + + return true; + } + } +} \ No newline at end of file From d74f4ed44479b02fdf473e2e3985095079171f91 Mon Sep 17 00:00:00 2001 From: Thaina Date: Wed, 12 Aug 2015 13:55:38 +0700 Subject: [PATCH 3/5] Clean android/ios --- .../Plugins/GoogleAnalyticsV3/GAIHandler.cs | 8 +-- .../GoogleAnalyticsAndroidV3.cs | 29 ++++------ .../GoogleAnalyticsV3/GoogleAnalyticsV3.cs | 36 +++++-------- .../GoogleAnalyticsV3/GoogleAnalyticsiOSV3.cs | 4 +- .../HitBuilders/HitBuilder.cs | 53 ++++++++++--------- 5 files changed, 59 insertions(+), 71 deletions(-) diff --git a/source/Plugins/GoogleAnalyticsV3/GAIHandler.cs b/source/Plugins/GoogleAnalyticsV3/GAIHandler.cs index 6f59a60..0dfa7f6 100644 --- a/source/Plugins/GoogleAnalyticsV3/GAIHandler.cs +++ b/source/Plugins/GoogleAnalyticsV3/GAIHandler.cs @@ -23,7 +23,7 @@ limitations under the License. Wrapper for Objective-C iOS SDK */ public class GAIHandler { -#if UNITY_IPHONE && !UNITY_EDITOR +#if UNITY_IPHONE [DllImport("__Internal")] private static extern void setOptOut(bool optOut); public void _setOptOut(bool optOut){ @@ -179,7 +179,7 @@ public void _sendTiming(TimingHitBuilder builder){ [DllImport("__Internal")] private static extern void addCustomDimensionToDictionary(int key, string value); - public void _buildCustomDimensionsDictionary(HitBuilder builder){ + public void _buildCustomDimensionsDictionary(HitBuilder builder){ foreach(var entry in builder.GetCustomDimensions()) { addCustomDimensionToDictionary(entry.Key, entry.Value); @@ -188,7 +188,7 @@ public void _buildCustomDimensionsDictionary(HitBuilder builder){ [DllImport("__Internal")] private static extern void addCustomMetricToDictionary(int key, string value); - public void _buildCustomMetricsDictionary(HitBuilder builder){ + public void _buildCustomMetricsDictionary(HitBuilder builder){ foreach(var entry in builder.GetCustomMetrics()) { addCustomMetricToDictionary(entry.Key, entry.Value); @@ -197,7 +197,7 @@ public void _buildCustomMetricsDictionary(HitBuilder builder){ [DllImport("__Internal")] private static extern void buildCampaignParametersDictionary(string source, string medium, string name, string content, string keyword); - public void _buildCampaignParametersDictionary(HitBuilder builder){ + public void _buildCampaignParametersDictionary(HitBuilder builder){ if(!String.IsNullOrEmpty(builder.GetCampaignSource())){ buildCampaignParametersDictionary(builder.GetCampaignSource(), builder.GetCampaignMedium() != null ? builder.GetCampaignMedium() : "", diff --git a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsAndroidV3.cs b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsAndroidV3.cs index 63be8e0..0860dae 100644 --- a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsAndroidV3.cs +++ b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsAndroidV3.cs @@ -25,7 +25,7 @@ limitations under the License. appropriate methods in this class if the application is built for Android. */ public class GoogleAnalyticsAndroidV3 : IDisposable { -#if UNITY_ANDROID && !UNITY_EDITOR +#if UNITY_ANDROID private string trackingCode; private string appVersion; private string appName; @@ -84,7 +84,7 @@ internal void InitializeTracker() { } } - internal void SetTrackerVal(Field fieldName, object value) { + internal void SetTrackerVal(string fieldName,object value) { object[] args = new object[] { fieldName.ToString(), value }; tracker.Call(GoogleAnalyticsV3.SET, args); } @@ -206,8 +206,7 @@ private AndroidJavaObject BuildMap(string methodName, object[] simpleArgs, } } - private Dictionary - AddCustomVariablesAndCampaignParameters(HitBuilder builder) { + private Dictionary AddCustomVariablesAndCampaignParameters(HitBuilder builder) { Dictionary parameters = new Dictionary(); AndroidJavaObject fieldName; @@ -283,8 +282,7 @@ internal void LogScreen (AppViewHitBuilder builder) { tracker.Call (GoogleAnalyticsV3.SET, args); } - Dictionary parameters = - AddCustomVariablesAndCampaignParameters(builder); + Dictionary parameters = AddCustomVariablesAndCampaignParameters(builder); if (parameters != null) { object map = BuildMap(GoogleAnalyticsV3.APP_VIEW, parameters); tracker.Call(GoogleAnalyticsV3.SEND, map); @@ -304,8 +302,7 @@ internal void LogEvent(EventHitBuilder builder) { args[3] = valueObj; object map; - Dictionary parameters = - AddCustomVariablesAndCampaignParameters(builder); + Dictionary parameters = AddCustomVariablesAndCampaignParameters(builder); if (parameters != null) { map = BuildMap(GoogleAnalyticsV3.EVENT_HIT, args, parameters); } else { @@ -336,8 +333,7 @@ internal void LogTransaction(TransactionHitBuilder builder) { args[5] = builder.GetCurrencyCode(); } object map; - Dictionary parameters = - AddCustomVariablesAndCampaignParameters(builder); + Dictionary parameters = AddCustomVariablesAndCampaignParameters(builder); if (parameters != null){ map = BuildMap(GoogleAnalyticsV3.TRANSACTION_HIT, args, parameters); } else { @@ -362,8 +358,7 @@ internal void LogItem(ItemHitBuilder builder) { args[5] = new AndroidJavaObject("java.lang.Long", builder.GetQuantity()); object map; - Dictionary parameters = - AddCustomVariablesAndCampaignParameters(builder); + Dictionary parameters = AddCustomVariablesAndCampaignParameters(builder); if (parameters != null) { map = BuildMap(GoogleAnalyticsV3.ITEM_HIT, args, parameters); } else { @@ -377,8 +372,7 @@ public void LogException(ExceptionHitBuilder builder) { args[0] = builder.GetExceptionDescription(); args[1] = new AndroidJavaObject("java.lang.Boolean", builder.IsFatal()); object map; - Dictionary parameters = - AddCustomVariablesAndCampaignParameters(builder); + Dictionary parameters = AddCustomVariablesAndCampaignParameters(builder); if (parameters != null) { map = BuildMap(GoogleAnalyticsV3.EXCEPTION_HIT, args, parameters); } else { @@ -398,8 +392,7 @@ public void LogSocial(SocialHitBuilder builder) { args[2] = builder.GetSocialTarget(); object map; - Dictionary parameters = - AddCustomVariablesAndCampaignParameters(builder); + Dictionary parameters = AddCustomVariablesAndCampaignParameters(builder); if (parameters != null) { map = BuildMap(GoogleAnalyticsV3.SOCIAL_HIT, args, parameters); } else { @@ -417,8 +410,7 @@ public void LogTiming(TimingHitBuilder builder) { args[2] = builder.GetTimingName(); args[3] = builder.GetTimingLabel(); object map; - Dictionary parameters = - AddCustomVariablesAndCampaignParameters(builder); + Dictionary parameters = AddCustomVariablesAndCampaignParameters(builder); if (parameters != null) { map = BuildMap(GoogleAnalyticsV3.TIMING_HIT, args, parameters); } else { @@ -465,6 +457,7 @@ public void SetDryRun(bool dryRun) { } #endif + public void Dispose() { #if UNITY_ANDROID && !UNITY_EDITOR diff --git a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs index 477b8c3..ecab791 100755 --- a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs +++ b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs @@ -103,9 +103,7 @@ public static GoogleAnalyticsV3 getInstance() [Tooltip("The amount of time in seconds your application can stay in the background before the session is ended. Default is 30 minutes (1800 seconds). A value of -1 will disable session management.")] public int sessionTimeout = 1800; - [Tooltip("Support only standard currency symbal")] - public string currencySymbol = "USD"; - + public readonly static string currencySymbol = "USD"; public readonly static string EVENT_HIT = "createEvent"; public readonly static string APP_VIEW = "createAppView"; public readonly static string SET = "set"; @@ -115,13 +113,13 @@ public static GoogleAnalyticsV3 getInstance() public readonly static string TRANSACTION_HIT = "createTransaction"; public readonly static string SOCIAL_HIT = "createSocial"; public readonly static string TIMING_HIT = "createTiming"; - public readonly static string EXCEPTION_HIT = "createException"; - + public readonly static string EXCEPTION_HIT = "createException"; + #if UNITY_ANDROID && !UNITY_EDITOR private GoogleAnalyticsAndroidV3 tracker = new GoogleAnalyticsAndroidV3(); #elif UNITY_IPHONE && !UNITY_EDITOR private GoogleAnalyticsiOSV3 tracker = new GoogleAnalyticsiOSV3(); -#else +#else private GoogleAnalyticsMPV3 tracker = new GoogleAnalyticsMPV3(); #endif @@ -157,11 +155,10 @@ void Update() private void HandleException(string condition,string stackTrace,LogType type) { - if(type == LogType.Exception) - { - uncaughtExceptionStackTrace = condition + "\n" + stackTrace - + UnityEngine.StackTraceUtility.ExtractStackTrace(); - } + if(type != LogType.Exception) + return; + + uncaughtExceptionStackTrace = condition + "\n" + stackTrace + UnityEngine.StackTraceUtility.ExtractStackTrace(); } // TODO: Error checking on initialization parameters @@ -191,7 +188,6 @@ private void InitializeTracker() initialized = true; SetOnTracker(Fields.DEVELOPER_ID,"GbOCSs"); - SetOnTracker(Fields.CLIENT_ID,SystemInfo.deviceUniqueIdentifier); } public void SetAppLevelOptOut(bool optOut) @@ -239,7 +235,7 @@ public void SetOnTracker(string fieldName,object value) } public void LogScreen(string title) - { + { LogScreen(new AppViewHitBuilder(title)); } @@ -247,13 +243,10 @@ public void LogScreen(AppViewHitBuilder builder) { InitializeTracker(); if(builder.Validate() == null) - { return; - } + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) - { Debug.Log("Logging screen."); - } tracker.LogScreen(builder); } @@ -363,10 +356,9 @@ public void LogException(string exceptionDescription,bool isFatal) public void LogException(ExceptionHitBuilder builder) { InitializeTracker(); - if(builder.Validate() == null) - { + if(!builder.IsValid) return; - } + if(GoogleAnalyticsV3.belowThreshold(logLevel,GoogleAnalyticsV3.DebugMode.VERBOSE)) { Debug.Log("Logging exception."); @@ -427,10 +419,10 @@ public void LogTiming(TimingHitBuilder builder) public void Dispose() { - initialized = false; + initialized = false; #if UNITY_ANDROID && !UNITY_EDITOR tracker.Dispose(); -#endif +#endif } public static bool belowThreshold(DebugMode userLogLevel,DebugMode comparelogLevel) diff --git a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsiOSV3.cs b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsiOSV3.cs index 27522e9..c9f401f 100644 --- a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsiOSV3.cs +++ b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsiOSV3.cs @@ -25,7 +25,7 @@ limitations under the License. */ public class GoogleAnalyticsiOSV3 { -#if UNITY_IPHONE && !UNITY_EDITOR +#if UNITY_IPHONE private string trackingCode; private string appName; private string bundleIdentifier; @@ -121,7 +121,7 @@ public void SetOptOut(bool optOut) { handler._setOptOut(optOut); } - public void SetTrackerVal(Field fieldName, object value){ + public void SetTrackerVal(string fieldName, object value){ handler._set(fieldName.ToString(), value); } diff --git a/source/Plugins/GoogleAnalyticsV3/HitBuilders/HitBuilder.cs b/source/Plugins/GoogleAnalyticsV3/HitBuilders/HitBuilder.cs index eae4538..5094f5b 100644 --- a/source/Plugins/GoogleAnalyticsV3/HitBuilders/HitBuilder.cs +++ b/source/Plugins/GoogleAnalyticsV3/HitBuilders/HitBuilder.cs @@ -21,29 +21,32 @@ limitations under the License. /* Base class for building hits. This class stores data which can be sent with any hit type but cannot be sent independent of other hits. - */ -public abstract class HitBuilder -{ - public abstract bool IsValid { get; } - - public readonly Dictionary CustomDimensions = new Dictionary(); - public readonly Dictionary CustomMetrics = new Dictionary(); - - public string CampaignName = ""; - public string CampaignSource = ""; - public string CampaignMedium = ""; - public string CampaignKeyword = ""; - public string CampaignContent = ""; - public string CampaignID = ""; - public string gclid = ""; - public string dclid = ""; - - public string GetCampaignName() { return CampaignName; } - public string GetCampaignSource() { return CampaignSource; } - public string GetCampaignMedium() { return CampaignMedium; } - public string GetCampaignKeyword() { return CampaignKeyword; } - public string GetCampaignContent() { return CampaignContent; } - public string GetCampaignID() { return CampaignID; } - public string GetGclid() { return gclid; } - public string GetDclid() { return dclid; } + */ +public abstract class HitBuilder +{ + public abstract bool IsValid { get; } + + public readonly Dictionary CustomDimensions = new Dictionary(); + public readonly Dictionary CustomMetrics = new Dictionary(); + + public Dictionary GetCustomDimensions() { return CustomDimensions; } + public Dictionary GetCustomMetrics() { return CustomMetrics; } + + public string CampaignName = ""; + public string CampaignSource = ""; + public string CampaignMedium = ""; + public string CampaignKeyword = ""; + public string CampaignContent = ""; + public string CampaignID = ""; + public string gclid = ""; + public string dclid = ""; + + public string GetCampaignName() { return CampaignName; } + public string GetCampaignSource() { return CampaignSource; } + public string GetCampaignMedium() { return CampaignMedium; } + public string GetCampaignKeyword() { return CampaignKeyword; } + public string GetCampaignContent() { return CampaignContent; } + public string GetCampaignID() { return CampaignID; } + public string GetGclid() { return gclid; } + public string GetDclid() { return dclid; } } \ No newline at end of file From 297f8a70f464c5f8edd93a0a27dd4975fe617d64 Mon Sep 17 00:00:00 2001 From: Thaina Date: Wed, 12 Aug 2015 14:00:54 +0700 Subject: [PATCH 4/5] Use latest version of ios GA SDK --- source/Plugins/iOS/GAI.h | 25 +++++++++++-------- source/Plugins/iOS/GAIDictionaryBuilder.h | 4 ++- source/Plugins/iOS/GAIFields.h | 5 +++- source/Plugins/iOS/GAITrackedViewController.h | 3 +-- source/Plugins/iOS/GAITracker.h | 1 - 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/source/Plugins/iOS/GAI.h b/source/Plugins/iOS/GAI.h index fa394a5..09c6bfd 100644 --- a/source/Plugins/iOS/GAI.h +++ b/source/Plugins/iOS/GAI.h @@ -1,15 +1,15 @@ /*! @header GAI.h @abstract Google Analytics iOS SDK Header - @version 3.0 - @copyright Copyright 2013 Google Inc. All rights reserved. + @version 3.13 + @copyright Copyright 2015 Google Inc. All rights reserved. */ #import + #import "GAILogger.h" -#import "GAITracker.h" #import "GAITrackedViewController.h" - +#import "GAITracker.h" typedef NS_ENUM(NSUInteger, GAIDispatchResult) { kGAIDispatchNoData, @@ -174,14 +174,19 @@ typedef enum { (returning kGAIDispatchError). If there is no network connection or there is no data to send, kGAIDispatchNoData is returned. + Note that calling this method with a non-nil completionHandler disables periodic dispatch. + Periodic dispatch can be reenabled by setting the dispatchInterval to a positive number when + the app resumes from the background. + Calling this method with a nil completionHandler is the same as calling the dispatch above. - This method can be used for background data fetching in iOS 7.0 or later. + This method can be used for background data fetching in iOS 7.0 or later. It would be wise to + call this when the application is exiting to initiate the submission of any unsubmitted + tracking information. - It would be wise to call this when application is exiting to initiate the - submission of any unsubmitted tracking information. Note that this does not - have any effect on dispatchInterval, and can be used in conjunction with - periodic dispatch. */ -- (void)dispatchWithCompletionHandler:(void (^)(GAIDispatchResult))completionHandler; + @param completionHandler The block to run after a single dispatch request. The GAIDispatchResult + param indicates whether the dispatch succeeded, had an error, or had no hits to dispatch. + */ +- (void)dispatchWithCompletionHandler:(void (^)(GAIDispatchResult result))completionHandler; @end diff --git a/source/Plugins/iOS/GAIDictionaryBuilder.h b/source/Plugins/iOS/GAIDictionaryBuilder.h index 5aadbf9..28418cb 100644 --- a/source/Plugins/iOS/GAIDictionaryBuilder.h +++ b/source/Plugins/iOS/GAIDictionaryBuilder.h @@ -103,6 +103,8 @@ *
  • dclid
  • *
  • gclid
  • *
  • gmob_t
  • + *
  • aclid
  • + *
  • anid
  • * *

    * Example: @@ -127,7 +129,7 @@ This method is deprecated. Use createScreenView instead. */ -+ (GAIDictionaryBuilder *)createAppView; ++ (GAIDictionaryBuilder *)createAppView DEPRECATED_MSG_ATTRIBUTE("Use createScreenView instead."); /*! Returns a GAIDictionaryBuilder object with parameters specific to a screenview diff --git a/source/Plugins/iOS/GAIFields.h b/source/Plugins/iOS/GAIFields.h index 6a862c0..e54cef0 100644 --- a/source/Plugins/iOS/GAIFields.h +++ b/source/Plugins/iOS/GAIFields.h @@ -19,6 +19,7 @@ extern NSString *const kGAIClientId; extern NSString *const kGAIDataSource; extern NSString *const kGAIAnonymizeIp; extern NSString *const kGAISessionControl; +extern NSString *const kGAIDeviceModelVersion; extern NSString *const kGAIScreenResolution; extern NSString *const kGAIViewportSize; extern NSString *const kGAIEncoding; @@ -69,6 +70,8 @@ extern NSString *const kGAICampaignName; extern NSString *const kGAICampaignKeyword; extern NSString *const kGAICampaignContent; extern NSString *const kGAICampaignId; +extern NSString *const kGAICampaignAdNetworkClickId; +extern NSString *const kGAICampaignAdNetworkId; extern NSString *const kGAITimingCategory; extern NSString *const kGAITimingVar; @@ -84,7 +87,7 @@ extern NSString *const kGAIIdfa; extern NSString *const kGAIAdTargetingEnabled; // hit types -extern NSString *const kGAIAppView; // deprecated, use kGAIScreenView instead +extern NSString *const kGAIAppView DEPRECATED_MSG_ATTRIBUTE("Use kGAIScreenView instead."); extern NSString *const kGAIScreenView; extern NSString *const kGAIEvent; extern NSString *const kGAISocial; diff --git a/source/Plugins/iOS/GAITrackedViewController.h b/source/Plugins/iOS/GAITrackedViewController.h index 5cbabdd..de19def 100644 --- a/source/Plugins/iOS/GAITrackedViewController.h +++ b/source/Plugins/iOS/GAITrackedViewController.h @@ -1,7 +1,6 @@ /*! @header GAITrackedViewController.h @abstract Google Analytics for iOS Tracked View Controller Header - @version 2.0 @copyright Copyright 2012 Google Inc. All rights reserved. */ @@ -11,7 +10,7 @@ @protocol GAITracker; /*! - Extends UIViewController to generate Google Analytics appview calls + Extends UIViewController to generate Google Analytics screenview calls whenever the view appears; this is done by overriding the `viewDidAppear:` method. The screen name must be set for any tracking calls to be made. diff --git a/source/Plugins/iOS/GAITracker.h b/source/Plugins/iOS/GAITracker.h index 605a2e4..fdc5c5d 100644 --- a/source/Plugins/iOS/GAITracker.h +++ b/source/Plugins/iOS/GAITracker.h @@ -1,7 +1,6 @@ /*! @header GAITracker.h @abstract Google Analytics iOS SDK Tracker Header - @version 3.0 @copyright Copyright 2013 Google Inc. All rights reserved. */ From c5f97d7b8c01a061ce257fc0d6996d6c06bb2d1e Mon Sep 17 00:00:00 2001 From: Thaina Date: Wed, 12 Aug 2015 22:40:31 +0700 Subject: [PATCH 5/5] Fix bug --- source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs index ecab791..9013b98 100755 --- a/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs +++ b/source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsV3.cs @@ -53,9 +53,9 @@ public static GoogleAnalyticsV3 getInstance() { instance = GameObject.FindObjectOfType(); if(instance == null) - { - var prefab = Resources.FindObjectsOfTypeAll().FirstOrDefault(); - instance = GameObject.Instantiate(prefab).GetComponent(); + { + instance = Resources.LoadAll("").FirstOrDefault(); + instance = GameObject.Instantiate(instance.gameObject).GetComponent(); } }