コード例 #1
0
        /// <summary>
        /// Converts Google.Analytics.DataFeed into a JsonDataTable
        /// </summary>
        /// <param name="jsKey">a unique key for the javascript variable.  variable is created as var {jsKey}_JsonTable = {};</param>
        /// <returns></returns>
        public string ResultsAsJsonTable(string jsKey)
        {
            string x = queryResults.ToString();

            if (queryResults.Entries.Count == 0)
            {
                return("{}");
            }

            JsonTable tab = new JsonTable(jsKey);
            int       idx = 0;

            foreach (DataEntry entry in queryResults.Entries)
            {
                JsonRow row = new JsonRow();
                foreach (Dimension dimension in entry.Dimensions)
                {
                    if (idx == 0)
                    {
                        tab.Columns.Add(new JsonColumn()
                        {
                            ID = dimension.Name, Label = getLabelFriendlyName(dimension.Name), Type = JsonType.STRING
                        });
                    }
                    JsonCell cell = new JsonCell();
                    cell.Type = JsonType.STRING;
                    int valAsNumDate;
                    if (int.TryParse(dimension.Value, out valAsNumDate))
                    {
                        string year  = dimension.Value.Substring(0, 4);
                        string month = dimension.Value.Substring(4, 2);
                        string day   = dimension.Value.Substring(6);
                        cell.Value = month + '/' + day + '/' + year;
                    }
                    else
                    {
                        cell.Value = dimension.Value;
                    }
                    row.Cells.Add(cell);
                }
                foreach (Metric metric in entry.Metrics)
                {
                    if (idx == 0)
                    {
                        tab.Columns.Add(new JsonColumn()
                        {
                            ID = metric.Name, Label = getLabelFriendlyName(metric.Name), Type = JsonType.NUMBER
                        });
                    }
                    JsonCell cell = new JsonCell();
                    cell.Type  = JsonType.NUMBER;
                    cell.Value = metric.Value;
                    row.Cells.Add(cell);
                }

                idx++;
                tab.Rows.Add(row);
            }

            return(tab.ToString());
        }