Example #1
0
    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());
        }
    }