void SendDeathEvent() { try { // Sending Kii Analytics event for game over stats KiiEvent ev = KiiAnalytics.NewEvent("PlayerDeath"); // Set key-value pairs ev ["time"] = Time.time; ev ["level"] = 1; // Upload Event Data to Kii Cloud KiiAnalytics.Upload((Exception e) => { if (e == null) { Debug.Log("Analytics event upload successful"); } else { Debug.Log("Analytics event upload error: " + e.ToString()); } }, ev); } catch (CloudException e) { Debug.Log("Analytics event upload error: " + e.ToString()); } }
/// <summary> /// Initializes the KiiCloudStorageSDK and KiiAnalyticsSDK. /// </summary> /// <remarks> /// This method is called by UnityGameEngine. Do not use it from your application. /// </remarks> public virtual void Awake() { if (!KiiInitializeBehaviour.isInitialized) { KiiInitializeBehaviour.isInitialized = true; DontDestroyOnLoad(this); INSTANCE = this; if (!Utils.IsEmpty(this.ServerUrl)) { Kii.Initialize(this.AppID, this.AppKey, this.ServerUrl, new KiiUnityClientFactoryImpl(), new KiiAsyncUnityClientFactoryImpl()); KiiAnalytics.Initialize(this.AppID, this.AppKey, this.ServerUrl, this.GetDeviceID(), new KiiUnityClientFactoryImpl(), new KiiAsyncUnityClientFactoryImpl()); } else { Kii.Initialize(this.AppID, this.AppKey, this.Site, new KiiUnityClientFactoryImpl(), new KiiAsyncUnityClientFactoryImpl()); KiiAnalytics.Initialize(this.AppID, this.AppKey, ToAnalyticsSite(this.Site), this.GetDeviceID(), new KiiUnityClientFactoryImpl(), new KiiAsyncUnityClientFactoryImpl()); } WWWRequestLooper.ApiTimeout = this.ApiTimeout; StartCoroutine(WWWRequestLooper.RunLoop()); } }
static KiiAutoInitialize() { //Sign up on developer.kii.com and create a Unity app to get these parameters! //See the Assets/Readme.txt file in this project for more info //Your backend location options: Kii.Site.US, Kii.Site.JP, Kii.Site.CN //IMPORTANT: backend location here must match backend location configured in your app at developer.kii.com Debug.Log("Initializing Kii..."); KiiAutoInitialize init = Instance; string appID = init.GetAppId("__KII_APP_ID__"); string appKey = init.GetAppKey("__KII_APP_KEY__"); Kii.Site appSite = init.GetAppSiteValue(); Debug.Log("AppId:" + appID + " / AppKey:" + appKey + " / AppSite:" + appSite.ToString()); Kii.Initialize(appID, appKey, appSite); //Interested in Game Analytics? Get our Analytics SDK http://developer.kii.com/#/sdks //More info: http://documentation.kii.com/en/guides/unity/managing-analytics KiiAnalytics.Site analyticsAppSite = init.GetAnalyticsAppSiteValue(); string deviceID = GetDeviceID(); Debug.Log("KiiAnalyticsInit - DeviceId: " + deviceID); // Create a Unity app at developer.kii.com and enter your own app_id, app_key and backend location here KiiAnalytics.Initialize(appID, appKey, analyticsAppSite, deviceID); }
public void Test_Non_Initialize_AnalyticsAPI_Async() { try { KiiEvent ev = KiiAnalytics.NewEvent("MyUser"); KiiAnalytics.Upload((Exception e) => { }, ev); Assert.Fail("InvalidOperationException isn't thrown"); } catch (InvalidOperationException e) { Assert.AreEqual(ErrorInfo.UTILS_KIICLIENT_NULL, e.Message); } try { ResultCondition condition = new ResultCondition(); KiiAnalytics.GetResult("1234", condition, (string ruleId, ResultCondition c, GroupedResult r, Exception e) => { }); Assert.Fail("InvalidOperationException isn't thrown"); } catch (InvalidOperationException e) { Assert.AreEqual(ErrorInfo.UTILS_KIICLIENT_NULL, e.Message); } }
/// <summary> /// Switchs the app. /// </summary> /// <param name="appId">AppID.</param> /// <param name="appKey">AppID.</param> /// <param name="serverUrl">Server URL.</param> /// <remarks> /// This method is for internal use only. Do not use it from your application. /// </remarks> public void SwitchApp(string appId, string appKey, string serverUrl) { Kii.Initialize(appId, appKey, serverUrl, new KiiUnityClientFactoryImpl(), new KiiAsyncUnityClientFactoryImpl()); KiiAnalytics.Initialize(appId, appKey, serverUrl, this.GetDeviceID(), new KiiUnityClientFactoryImpl(), new KiiAsyncUnityClientFactoryImpl()); this.AppID = appId; this.AppKey = appKey; this.ServerUrl = serverUrl; }
/// <summary> /// Switchs the app. /// </summary> /// <param name="appId">AppID.</param> /// <param name="appKey">AppKey.</param> /// <param name="site">Site.</param> /// <remarks> /// This method is for internal use only. Do not use it from your application. /// </remarks> public void SwitchApp(string appId, string appKey, Kii.Site site) { Kii.Initialize(appId, appKey, site, new KiiUnityClientFactoryImpl(), new KiiAsyncUnityClientFactoryImpl()); KiiAnalytics.Initialize(appId, appKey, ToAnalyticsSite(site), this.GetDeviceID(), new KiiUnityClientFactoryImpl(), new KiiAsyncUnityClientFactoryImpl()); this.AppID = appId; this.AppKey = appKey; this.Site = site; this.ServerUrl = null; }
/// <summary> /// You will generate event when the specified condition in the <see cref="KiiCorp.Cloud.ABTesting.KiiExperiment"/> has achieved. /// ex.) User has signed up, view the message, purchase item, etc. /// You need to call <see cref="KiiAnalytics.Upload(KiiEvent)"/> method of KiiAnalyticsSDK to send the event to Kii Analytics Cloud. /// </summary> /// <remarks> /// </remarks> /// <returns>KiiEvent represents event.</returns> /// <param name="conversionEvent">ConversionEvent to specify which conversionEvent has achieved.</param> public KiiEvent EventForConversion(ConversionEvent conversionEvent) { if (conversionEvent == null) { throw new ArgumentException("conversionEvent is null"); } if (mParentExperimentStatus != KiiExperimentStatus.RUNNING) { return(new KiiEvent.NullKiiEvent()); } KiiEvent e = KiiAnalytics.NewEvent(this.mParentExperimentID); e ["variationName"] = this.mName; e ["conversionEvent"] = conversionEvent.Name; e ["version"] = this.mParentExperimentVersion; return(e); }
void CalculateScore() { #if !UNITY_FLASH recordedTime = (int)GameScore.GameTime; #endif deaths = GameScore.Deaths; buzzerKills = GameScore.GetKills("KamikazeBuzzer"); spiderKills = GameScore.GetKills("EnemySpider"); mechKills = GameScore.GetKills("EnemyMech") + GameScore.GetKills("ConfusedEnemyMech"); points = (int)(buzzerKills * kBuzzerKillPrize + spiderKills * kSpiderKillPrize + mechKills * kMechKillPrize); if (deaths != 0) { points /= (int)(deaths * kDeathCostFactor); } // Sending Kii Analytics event for game over stats Debug.Log("Sending game over event..."); KiiEvent ev = KiiAnalytics.NewEvent("GameOver"); // Set key-value pairs #if !UNITY_FLASH ev["recordedTime"] = recordedTime; #endif ev["deaths"] = deaths; ev["buzzerKills"] = buzzerKills; ev["spiderKills"] = spiderKills; ev["mechKills"] = mechKills; ev["points"] = points; // Upload Event Data to Kii Cloud KiiAnalytics.Upload((Exception e) => { if (e == null) { Debug.Log("GameOverGU: Analytics game over event upload successful"); } else { Debug.Log("GameOverGUI: Unable to upload game over event to Kii Cloud: " + e.ToString()); } }, ev); }
// Send Analytics event for end of level time public static void EndOfLevel(float gameTime) { Debug.Log("Sending end of level event..."); KiiEvent ev = KiiAnalytics.NewEvent("EndOfLevel"); // Set key-value pairs ev ["user"] = user.Username; ev ["time"] = gameTime; // Upload Event Data to Kii Cloud KiiAnalytics.Upload((Exception e) => { if (e == null) { Debug.Log("GameScore: Analytics end-of-level event upload successful"); } else { Debug.Log("GameScore: Unable to upload end-of-level event to Kii Cloud: " + e.ToString()); } }, ev); }
void FetchAvgDeathTime() { if (GameConfig.ANALYTICS_RULE_ID == 0) { Debug.Log("See Assets/Readme.txt for instructions on how to replace the rule ID and use analytics"); return; } Debug.Log("Getting analytics snapshots"); // Define filters ResultCondition condition = new ResultCondition(); //condition.AddFilter("AppVersion", "9"); //condition.AddFilter("location", "UK"); //condition.GroupingKey = "gender"; //condition.GroupingKey = "UserLevel"; condition.DateRange = new DateRange(new DateTime(2014, 2, 2), DateTime.Now); try { // My id is 147, but the ID must match the analytic rule you should create on developer.kii.com this way: // Go to developer.kii.com, go to your app's console // Click on Analytics on the left side bar, then lick on "Create a new rule" // Name your rule AvgDeathTime or whichever name you like, select "Event Data" // In the function dropdown select "Avg" and in the field enter the word "time" // In the type combo select "float" // In the dimensions fields enter "time", "time" and "float" // Click on Save and activate the rule // Once active copy the ID assigned to the rule and replace the 147 below with that KiiAnalytics.GetResult(GameConfig.ANALYTICS_RULE_ID.ToString(), condition, (string ruleId, ResultCondition condition2, GroupedResult result2, Exception e) => { if (e == null) { Debug.Log("Analytics event upload successful"); IList <GroupedSnapShot> snapshots = result2.SnapShots; Debug.Log("Cycling through analytics snapshots"); foreach (GroupedSnapShot snapshot in snapshots) { Debug.Log("Found a snapshot: " + snapshot.Data); JsonOrg.JsonArray array = snapshot.Data; int j = 0; Score.avgDeath = 0; for (int i = array.Length(); i > 0; i--) { if (array.Get(i - 1).GetType() == typeof(JsonOrg.JsonNull)) { j++; } else { Score.avgDeath += (float)array.GetDouble(i - 1); } } Score.avgDeath /= (array.Length() - j); } } else { Debug.Log("Analytics snapshot fetch error: " + e.ToString()); } }); } catch (Exception e) { Debug.Log("Analytics snapshot fetch error: " + e.ToString()); } }
void OnGUI() { if (!this.loggedin) { this.email = GUI.TextField(new Rect(10, 10, 800, 100), this.email); GUI.Label(new Rect(10, 220, 500, 1000), this.message); if (GUI.Button(new Rect(10, 115, 250, 100), "Create User")) { try { KiiUser user = KiiUser.BuilderWithEmail(this.email).WithName("U" + Environment.TickCount).Build(); user.Register("pa$$sword"); this.message = "SUCCESS"; this.loggedin = true; this.message = ""; } catch (KiiCorp.Cloud.Storage.NetworkException e) { this.message = "ERROR: " + e.GetType() + "\n" + "Status=" + e.Status + "\n" + "Data=" + e.Data.ToString() + "\n" + "InnerExcepton=" + e.InnerException.GetType() + "\n" + "InnerExcepton.Message=" + e.InnerException.Message + "\n" + "InnerExcepton.Stacktrace=" + e.InnerException.StackTrace + "\n" + "Source=" + e.Source + "\n" + e.Message + "\n" + e.StackTrace; } catch (Exception e) { this.message = "ERROR: " + e.GetType() + " " + e.Message + "\n" + e.StackTrace; } } } else { GUI.Label(new Rect(10, 220, 500, 1000), message); try { if (this.experiment == null) { this.experiment = KiiExperiment.GetByID("8a835c5b-3508-41a9-9a7e-a348c513a426"); this.appliedVariation = experiment.GetAppliedVariation(experiment.Variations[0]); ConversionEvent viewCnvEvent = this.experiment.GetConversionEventByName("viewed"); KiiEvent viewKiiEvent = this.appliedVariation.EventForConversion(viewCnvEvent); KiiAnalytics.Upload(viewKiiEvent); } if (this.buttonCaption == null) { JsonObject json = this.appliedVariation.VariableSet; this.buttonCaption = json.GetString("caption"); } if (GUI.Button(new Rect(10, 10, 250, 100), this.buttonCaption)) { ConversionEvent cnvEvent = experiment.GetConversionEventByName("clicked"); KiiEvent kiiEvent = this.appliedVariation.EventForConversion(cnvEvent); KiiAnalytics.Upload(kiiEvent); Application.Quit(); } if (GUI.Button(new Rect(265, 10, 250, 100), "Cancel")) { Application.Quit(); } } catch (Exception e) { this.message = "ERROR: " + e.GetType() + " " + e.Message + "\n" + e.StackTrace; } } }
void Start() { Kii.Initialize("{APP ID}", "{APP KEY}", "http://api-jp.kii.com/api"); KiiAnalytics.Initialize("{APP ID}", "{APP KEY}", "http://api-jp.kii.com/api", GetDeviceID()); }
void OnGUI() { if (!this.loggedin) { this.email = GUI.TextField(new Rect(10, 10, 800, 100), this.email); GUI.Label(new Rect(10, 220, 500, 1000), this.message); if (GUI.Button(new Rect(10, 115, 250, 100), "Create User")) { KiiUser user = KiiUser.BuilderWithEmail(this.email).WithName("U" + Environment.TickCount).Build(); user.Register("pa$$sword", (KiiUser registeredUser, Exception e) => { if (e != null) { this.message = "ERROR: failed to register user " + e.GetType() + " " + e.Message + "\n" + e.StackTrace; } else { this.message = "SUCCESS"; this.loggedin = true; this.message = ""; } }); } } else { GUI.Label(new Rect(10, 220, 500, 1000), message); if (!this.initializedExperiment) { this.initializedExperiment = true; Debug.Log("#####Call KiiExperiment.GetByID"); KiiExperiment.GetByID("f1b00500-eae2-11e3-a0e4-22000a84b8f5", (KiiExperiment experiment, Exception e) => { Debug.Log("#####End KiiExperiment.GetByID"); if (e != null) { Debug.Log("#####Error KiiExperiment.GetByID"); this.message = "ERROR: KiiExperiment.GetByID failed!! " + e.GetType() + " " + e.Message + "\n" + e.StackTrace; return; } Debug.Log("#####Success KiiExperiment.GetByID"); this.experiment = experiment; this.appliedVariation = this.experiment.GetAppliedVariation(this.experiment.Variations[0]); ConversionEvent viewCnvEvent = this.experiment.GetConversionEventByName("viewed"); KiiEvent viewKiiEvent = this.appliedVariation.EventForConversion(viewCnvEvent); KiiAnalytics.Upload((Exception e1) => { if (e1 != null) { this.message = "ERROR: KiiAnalytics.Upload('viewed') failed!! " + e1.GetType() + " " + e1.Message + "\n" + e1.StackTrace; } else { this.message = "Event 'viewed' is Uploaded!!"; } } , viewKiiEvent); if (this.buttonCaption == null) { JsonObject json = this.appliedVariation.VariableSet; this.buttonCaption = json.GetString("caption"); } }); } if (GUI.Button(new Rect(10, 10, 250, 100), this.buttonCaption)) { ConversionEvent cnvEvent = this.experiment.GetConversionEventByName("clicked"); KiiEvent kiiEvent = this.appliedVariation.EventForConversion(cnvEvent); KiiAnalytics.Upload((Exception e2) => { if (e2 != null) { this.message = "ERROR: KiiAnalytics.Upload(clicked) failed!! " + e2.GetType() + " " + e2.Message + "\n" + e2.StackTrace; } else { this.message = "Event 'clicked' is Uploaded!!"; } }, kiiEvent); } if (GUI.Button(new Rect(265, 10, 250, 100), "Cancel")) { Application.Quit(); } } }