/// <summary> /// Get report name from config file by any metric /// </summary> /// <param name="metric"></param> /// <returns></returns> private string GetReportNameByMetric(string metric) { var config = ReportConfiguration.GetConfig(); foreach (var item in config.Reports) { var report = item as Report; if (report != null && report.Metrics.Contains(metric)) { return(report.Name); } } return(string.Empty); }
/// <summary> /// Get all reports configured in App.config /// </summary> /// <returns></returns> public GetReportsResponse GetReport(string viewId) { var combinedReportResponse = new GetReportsResponse { Reports = new List <GAReport>() }; try { Logger.Info("Processing View Id: " + viewId); var config = ReportConfiguration.GetConfig(); foreach (var item in config.Reports) { if (item is Report report) { var stopwatch = new Stopwatch(); Logger.Info("Started fetching report: " + report.Name); // Create the Metrics and dimensions object based on configuration. var metrics = report.Metrics.Split(',').Select(m => new Metric { Expression = m }).ToList(); var dimensions = report.Dimensions.Split(',').Select(d => new Dimension { Name = d }).ToList(); var reportRequest = new ReportRequest { DateRanges = GetDateRangeFromConfiguration(config), Metrics = metrics, Dimensions = dimensions, ViewId = viewId.Trim(), SamplingLevel = "LARGE", //https://developers.google.com/analytics/devguides/reporting/core/v4/basics#sampling PageSize = report.RecordCount > 0? report.RecordCount : 10000 //The Analytics Core Reporting API returns a maximum of 10,000 rows per request, no matter how many you ask for. https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet }; //Sorting as per report configuration if (!string.IsNullOrEmpty(report.OrderBy)) { reportRequest.OrderBys = report.OrderBy.Split(',').Select(o => new OrderBy { FieldName = o.Split('-')[0], SortOrder = o.Split('-')[1] }).ToList(); } stopwatch.Start(); var reportsResponse = analyticsReportingServiceInstance.Reports.BatchGet(new GetReportsRequest { ReportRequests = new List <ReportRequest> { reportRequest } }).Execute(); if (reportsResponse != null) { ((List <GAReport>)combinedReportResponse.Reports).AddRange(reportsResponse.Reports); while (reportsResponse.Reports[0].NextPageToken != null) { reportRequest.PageToken = reportsResponse.Reports[0].NextPageToken; reportsResponse = analyticsReportingServiceInstance.Reports.BatchGet(new GetReportsRequest { ReportRequests = new List <ReportRequest> { reportRequest } }).Execute(); ((List <GAReport>)combinedReportResponse.Reports).AddRange(reportsResponse.Reports); } stopwatch.Stop(); } Logger.Info("Finished fetching report: " + report.Name); Logger.Info(string.Format("Time elapsed: {0:hh\\:mm\\:ss}", stopwatch.Elapsed)); } } } catch (Exception ex) { Logger.Error("Error in fetching reports: " + ex); } return(combinedReportResponse); }