From b639c1d36d586e9681b00bb203594cccd90daa9b Mon Sep 17 00:00:00 2001 From: Aaron Kim Date: Mon, 18 Apr 2016 17:21:03 +0800 Subject: [PATCH] Create DaimtoAnaltyicsReportingV4Helper.cs This helper class migrates Core Reporting API V3 to the Analytics Reporting API V4 as suggested from following guide. I have reused overall structure of DaimtoAnaltyicsReportingHelper class. https://developers.google.com/analytics/devguides/reporting/core/v4/migration --- .../DaimtoAnaltyicsReportingV4Helper.cs | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 Google-Analytics/Daimto-Google-Analytics-Sample/Daimto-Google-Analytics-Sample/DaimtoAnaltyicsReportingV4Helper.cs diff --git a/Google-Analytics/Daimto-Google-Analytics-Sample/Daimto-Google-Analytics-Sample/DaimtoAnaltyicsReportingV4Helper.cs b/Google-Analytics/Daimto-Google-Analytics-Sample/Daimto-Google-Analytics-Sample/DaimtoAnaltyicsReportingV4Helper.cs new file mode 100644 index 00000000..cfb2e315 --- /dev/null +++ b/Google-Analytics/Daimto-Google-Analytics-Sample/Daimto-Google-Analytics-Sample/DaimtoAnaltyicsReportingV4Helper.cs @@ -0,0 +1,141 @@ +/* + * Copyright 2014 Daimto.com + * + * 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. + */ +using System; +using System.Collections.Generic; +using Google.Apis.AnalyticsReporting; +using Google.Apis.AnalyticsReporting.v4; +using Google.Apis.AnalyticsReporting.v4.Data; +using System.Linq; +using System.Diagnostics; + + +namespace Daimto.Google.Sample.Analytics +{ + public class DaimtoAnaltyicsReportingV4Helper + { + /// + /// You query the Core Reporting API v4 for Google Analytics report data. + /// Documentation: https://developers.google.com/analytics/devguides/reporting/core/v4 + /// + /// Dimension and metric reference : https://developers.google.com/analytics/devguides/reporting/core/dimsmets + /// + /// Valid Authenticated ReportingServicve + /// The unique table ID of the form XXXX, where XXXX is the Analytics view (profile) ID for which the query will retrieve the data. + /// Start date for fetching Analytics data. Requests can specify a start date formatted as YYYY-MM-DD, or as a relative date (e.g., today, yesterday, or NdaysAgo where N is a positive integer). + /// End date for fetching Analytics data. Request can specify an end date formatted as YYYY-MM-DD, or as a relative date (e.g., today, yesterday, or NdaysAgo where N is a positive integer). + /// A list of comma-separated metrics, such as ga:sessions,ga:bounces. + /// + public static IList Get(AnalyticsReportingService service, string profileId, string startDate, string endDate, string metrics, string dimensions) + { + // Create the DateRanges object. + IList dateRangeList = new List(); + IList startDateList = new List(); + IList endDateList = new List(); + startDateList = startDate.Split(',').ToList(); + endDateList = endDate.Split(',').ToList(); + + for (int i = 0; i < startDateList.Count; i++ ) + { + DateRange dr = new DateRange(); + dr.StartDate = startDateList[i]; + dr.EndDate = endDateList[i]; + dateRangeList.Add(dr); + } + + // Create the Metircs object. + IList metricsList = new List(); + foreach( string metricString in metrics.Split(',').ToList()) + { + Metric m = new Metric(); + m.Expression = metricString; + metricsList.Add(m); + } + + // Create the Dimensions object. + IList dimensionsList = new List(); + foreach( string dimensionString in dimensions.Split(',').ToList() ) + { + Dimension d = new Dimension(); + d.Name = dimensionString; + dimensionsList.Add(d); + } + + // Create the ReportRequest object + ReportRequest request = new ReportRequest(); + // Assign values + request.ViewId = profileId; + request.DateRanges = dateRangeList; + request.Metrics = metricsList; + request.Dimensions = dimensionsList; + + GetReportsRequest getReport = new GetReportsRequest(); + getReport.ReportRequests = new List(); + getReport.ReportRequests.Add(request); + + GetReportsResponse getResponse = service.Reports.BatchGet(getReport).Execute(); + + PrintResults(getResponse.Reports); + + return getResponse.Reports; + } + + /// + /// This prints + /// + /// + private static void PrintResults(IList reports) + { + foreach(Report report in reports) + { + ColumnHeader header = report.ColumnHeader; + IList dimensionHeaders = new List(); + dimensionHeaders = header.Dimensions; + + IList metricHeaders = new List(); + metricHeaders = header.MetricHeader.MetricHeaderEntries; + + IList rows = new List(); + rows = report.Data.Rows; + + foreach(ReportRow row in rows) + { + IList dimensions = new List(); + dimensions = row.Dimensions; + + IList metrics = new List(); + metrics = row.Metrics; + + for (int i=0 ; i < dimensionHeaders.Count && i < dimensions.Count ; i++) + { + Debug.WriteLine(dimensionHeaders[i] + ": " + dimensions[i]); + } + + for (int j = 0; j < metrics.Count; j++) + { + DateRangeValues values = metrics[j]; + for (int k = 0; k < values.Values.Count ; k++) + { + Debug.WriteLine("\t" + metricHeaders[k].Name + ": " + values.Values[k]); + } + } + } + } + } + + + } +} +