private string ExtractReportValueFor(string metricName, Google.Apis.AnalyticsReporting.v4.Data.Report report)
        {
            var header = report.ColumnHeader.MetricHeader.MetricHeaderEntries.FirstOrDefault(h => h.Name == metricName);
            var i      = report.ColumnHeader.MetricHeader.MetricHeaderEntries.IndexOf(header);

            return(report.Data.Totals[0].Values[i]);
        }
        /// <summary>
        /// Get a string list containing the names of the requested columns.
        /// </summary>
        /// <returns>A List(string) containing the column names of the report.</returns>
        public static List <string> GetColumns(Google.Apis.AnalyticsReporting.v4.Data.Report rep)
        {
            List <string> columns = new List <string>();

            foreach (string s in rep.ColumnHeader.Dimensions)
            {
                columns.Add(s);
            }
            foreach (MetricHeaderEntry mhe in rep.ColumnHeader.MetricHeader.MetricHeaderEntries)
            {
                columns.Add(mhe.Name);
            }

            return(columns);
        }
        private List <MetricDataPoint> ParseResults(DateTime date, Google.Apis.AnalyticsReporting.v4.Data.Report report, IEnumerable <MetricDataPoint> metricDataPoints)
        {
            var resultantDataPoints = new List <MetricDataPoint>();

            var dataPoints = metricDataPoints as IList <MetricDataPoint> ?? metricDataPoints.ToList();

            resultantDataPoints.AddRange(CreateDataPointsForMatches(dataPoints, date, MetricDataSource.GASessions, int.Parse(ExtractReportValueFor("Sessions", report))));
            resultantDataPoints.AddRange(CreateDataPointsForMatches(dataPoints, date, MetricDataSource.GAUsers, int.Parse(ExtractReportValueFor("Users", report))));
            resultantDataPoints.AddRange(CreateDataPointsForMatches(dataPoints, date, MetricDataSource.GAPageViews, int.Parse(ExtractReportValueFor("PageViews", report))));
            resultantDataPoints.AddRange(CreateDataPointsForMatches(dataPoints, date, MetricDataSource.GAUniquePageViews, int.Parse(ExtractReportValueFor("UniquePageViews", report))));
            resultantDataPoints.AddRange(CreateDataPointsForMatches(dataPoints, date, MetricDataSource.GAGoalCompletions, int.Parse(ExtractReportValueFor("GoalCompletions", report))));
            resultantDataPoints.AddRange(CreateDataPointsForMatches(dataPoints, date, MetricDataSource.GAPageViewsPerSessions, double.Parse(ExtractReportValueFor("PageViewsPerSession", report))));
            resultantDataPoints.AddRange(CreateDataPointsForMatches(dataPoints, date, MetricDataSource.GAGoalConversion, double.Parse(ExtractReportValueFor("GoalConversions", report))));

            return(resultantDataPoints);
        }
        /// <summary>
        /// Gets a list containing dictionaries each of which represents the data of one row allowing to access data by the column name.
        /// </summary>
        /// <returns>A List(Dictionary(string, string)) containing the values for each columns for each row of the report.</returns>
        public static List <Dictionary <string, string> > GetValues(Google.Apis.AnalyticsReporting.v4.Data.Report rep)
        {
            List <Dictionary <string, string> > values = new List <Dictionary <string, string> >();
            List <string> columns = GetColumns(rep);

            foreach (ReportRow row in rep.Data.Rows)
            {
                values.Add(new Dictionary <string, string>());
                for (int i = 0; i < row.Dimensions.Count; i++)
                {
                    values[values.Count - 1].Add(columns[i], row.Dimensions[i]);
                }
                for (int i = 0; i < row.Metrics[0].Values.Count; i++)
                {
                    values[values.Count - 1].Add(columns[i + row.Dimensions.Count], row.Metrics[0].Values[i]);
                }
            }

            return(values);
        }
Exemple #5
0
        private static AnalyticsReport ConvertResponse(Google.Apis.AnalyticsReporting.v4.Data.Report report)
        {
            AnalyticsReport result = new AnalyticsReport();

            if (report.Data.RowCount > 0)
            {
                foreach (ReportRow rr in report.Data.Rows)
                {
                    AnalyticsRow row = new AnalyticsRow();
                    foreach (string d in rr.Dimensions)
                    {
                        row.Dimensions.Add(d);
                    }
                    foreach (DateRangeValues m in rr.Metrics)
                    {
                        row.Metrics.Add(Convert.ToInt32(m.Values[0]));
                    }
                    result.Add(row);
                }
            }
            return(result);
        }