/// <summary>Retrieves a report, using a filter for a specified ad client.</summary> /// <param name="adClientId">The ID for the ad client to be used.</param> private void GenerateReport(string adClientId) { Console.WriteLine("================================================================="); Console.WriteLine("Running report for ad client {0}", adClientId); Console.WriteLine("================================================================="); // Prepare report. var reportRequest = service.Accounts.Reports.Generate(adSenseAccount.Name); // Specify the dates and desired ad client using a filter, as well as other parameters. reportRequest.StartDateYear = 2021; reportRequest.StartDateMonth = 3; reportRequest.StartDateDay = 1; reportRequest.EndDateYear = 2021; reportRequest.EndDateMonth = 3; reportRequest.EndDateDay = 31; reportRequest.Filters = new List <string> { "AD_CLIENT_ID==" + ReportUtils.EscapeFilterParameter(adClientId) }; reportRequest.AddMetric(MetricsEnum.PAGEVIEWS); reportRequest.AddMetric(MetricsEnum.ADREQUESTS); reportRequest.AddMetric(MetricsEnum.ADREQUESTSCOVERAGE); reportRequest.AddMetric(MetricsEnum.ADREQUESTSCTR); reportRequest.AddMetric(MetricsEnum.COSTPERCLICK); reportRequest.AddMetric(MetricsEnum.ADREQUESTSRPM); reportRequest.AddMetric(MetricsEnum.ESTIMATEDEARNINGS); reportRequest.AddDimension(DimensionsEnum.DATE); reportRequest.OrderBy = new List <string> { "+DATE" }; // Run report. var reportResponse = reportRequest.Execute(); if (!reportResponse.Rows.IsNullOrEmpty()) { ReportUtils.DisplayHeaders(reportResponse.Headers); Console.WriteLine("Showing data from {0} to {1}", reportResponse.StartDate, reportResponse.EndDate); ReportUtils.DisplayRows(reportResponse.Rows); } else { Console.WriteLine("No rows returned."); } Console.WriteLine(); }
/// <summary>Retrieves a report, using a filter for a specified ad client.</summary> /// <param name="adClientId">The ID for the ad client to be used.</param> private void GenerateReport(string adClientId) { Console.WriteLine("================================================================="); Console.WriteLine("Running report for ad client {0}", adClientId); Console.WriteLine("================================================================="); // Prepare report. var startDate = DateTime.Today.AddDays(-7); var endDate = DateTime.Today; var reportRequest = service.Accounts.Reports.Generate( adSenseAccount.Id, startDate.ToString(DateFormat), endDate.ToString(DateFormat)); // Specify the desired ad client using a filter, as well as other parameters. reportRequest.Filter = new List <string> { "AD_CLIENT_ID==" + ReportUtils.EscapeFilterParameter(adClientId) }; reportRequest.Metric = new List <string> { "PAGE_VIEWS", "AD_REQUESTS", "AD_REQUESTS_COVERAGE", "AD_REQUESTS_CTR", "COST_PER_CLICK", "AD_REQUESTS_RPM", "EARNINGS" }; reportRequest.Dimension = new List <string> { "DATE" }; reportRequest.Sort = new List <string> { "+DATE" }; // Run report. var reportResponse = reportRequest.Execute(); ReportUtils.FillGapsDates(reportResponse, startDate, endDate); if (!reportResponse.Rows.IsNullOrEmpty()) { ReportUtils.DisplayHeaders(reportResponse.Headers); Console.WriteLine("Showing data from {0} to {1}", reportResponse.StartDate, reportResponse.EndDate); ReportUtils.DisplayRows(reportResponse.Rows); } else { Console.WriteLine("No rows returned."); } Console.WriteLine(); }
/// <summary> /// Retrieves a report, using a filter for a specified saved report. /// </summary> /// <param name="savedReportId">The ID of the saved report to generate.</param> private void GenerateSavedReport(string savedReportId) { Console.WriteLine("================================================================="); Console.WriteLine("Running saved report {0}", savedReportId); Console.WriteLine("================================================================="); var savedReportRequest = service.Accounts.Reports.Saved.Generate(adSenseAccount.Id, savedReportId); AdsenseReportsGenerateResponse savedReportResponse = savedReportRequest.Execute(); // Run report. if (!savedReportResponse.Rows.IsNullOrEmpty()) { ReportUtils.DisplayHeaders(savedReportResponse.Headers); ReportUtils.DisplayRows(savedReportResponse.Rows); } else { Console.WriteLine("No rows returned."); } Console.WriteLine(); }
/// <summary> /// Retrieves a report, using a filter for a specified saved report. /// </summary> /// <param name="savedReportId">The ID of the saved report to generate.</param> private void GenerateSavedReport(string savedReportId) { Console.WriteLine("================================================================="); Console.WriteLine("Running saved report {0}", savedReportId); Console.WriteLine("================================================================="); var savedReportRequest = service.Accounts.Reports.Saved.Generate(savedReportId); savedReportRequest.DateRange = SavedDateRangeEnum.LAST7DAYS; ReportResult savedReportResponse = savedReportRequest.Execute(); // Run report. if (!savedReportResponse.Rows.IsNullOrEmpty()) { ReportUtils.DisplayHeaders(savedReportResponse.Headers); ReportUtils.DisplayRows(savedReportResponse.Rows); } else { Console.WriteLine("No rows returned."); } Console.WriteLine(); }
/// <summary> /// Retrieves a report for a specified ad client, using pagination. /// <para>Please only use pagination if your application requires it due to memory or storage constraints. /// If you need to retrieve more than 5000 rows, please check GenerateReport, as due to current /// limitations you will not be able to use paging for large reports.</para> /// </summary> /// <param name="adClientId">The ID for the ad client to be used.</param> private void GenerateReportWithPaging(string adClientId) { int rowLimit = 5000; Console.WriteLine("================================================================="); Console.WriteLine("Running paginated report for ad client {0}", adClientId); Console.WriteLine("================================================================="); // Prepare report. var startDate = DateTime.Today.AddDays(-7).ToString(DateFormat); var endDate = DateTime.Today.ToString(DateFormat); var reportRequest = service.Accounts.Reports.Generate(adSenseAccount.Id, startDate, endDate); var pageSize = maxListPageSize; var startIndex = 0; // Specify the desired ad client using a filter, as well as other parameters. reportRequest.Filter = new List <string> { "AD_CLIENT_ID==" + ReportUtils.EscapeFilterParameter(adClientId) }; reportRequest.Metric = new List <string> { "PAGE_VIEWS", "AD_REQUESTS", "AD_REQUESTS_COVERAGE", "AD_REQUESTS_CTR", "COST_PER_CLICK", "AD_REQUESTS_RPM", "EARNINGS" }; reportRequest.Dimension = new List <string> { "DATE" }; reportRequest.Sort = new List <string> { "+DATE" }; // Run first page of report. var reportResponse = ReportUtils.GetPage(reportRequest, startIndex, pageSize); if (reportResponse.Rows.IsNullOrEmpty()) { Console.WriteLine("No rows returned."); Console.WriteLine(); return; } // Display headers. ReportUtils.DisplayHeaders(reportResponse.Headers); // Display first page of results. ReportUtils.DisplayRows(reportResponse.Rows); var totalRows = Math.Min(reportResponse.TotalMatchedRows.Value, rowLimit); for (startIndex = reportResponse.Rows.Count; startIndex < totalRows; startIndex += reportResponse.Rows.Count) { // Check to see if we're going to go above the limit and get as many results as we can. pageSize = Math.Min(maxListPageSize, (int)totalRows - startIndex); // Run next page of report. reportResponse = ReportUtils.GetPage(reportRequest, startIndex, pageSize); // If the report size changes in between paged requests, the result may be empty. if (reportResponse.Rows.IsNullOrEmpty()) { break; } // Display results. ReportUtils.DisplayRows(reportResponse.Rows); } Console.WriteLine(); }