コード例 #1
0
        /// <summary>
        /// Runs this sample.
        /// </summary>
        /// <param name="adsense">AdSense service object on which to run the requests.</param>
        /// <param name="adClientId">The ID for the ad client to be used.</param>
        /// <param name="maxReportPageSize">The maximum page size to retrieve.</param>
        public static void Run(AdsenseService adsense, string adClientId, int maxReportPageSize)
        {
            CommandLine.WriteLine("=================================================================");
            CommandLine.WriteLine("Running paginated report for ad client {0}", adClientId);
            CommandLine.WriteLine("=================================================================");

            // Prepare report.
            var startDate     = DateTime.Today.ToString(DateFormat);
            var endDate       = DateTime.Today.AddDays(-7).ToString(DateFormat);
            var reportRequest = adsense.Reports.Generate(startDate, endDate);
            var pageSize      = maxReportPageSize;
            var startIndex    = 0;

            // Specify the desired ad client using a filter, as well as other parameters.
            reportRequest.Filter = new List <string> {
                "AD_CLIENT_ID==" +
                GenerateReport.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 = getPage(reportRequest, startIndex, pageSize);

            if (reportResponse.Rows == null || reportResponse.Rows.Count == 0)
            {
                CommandLine.WriteLine("No rows returned.");
                return;
            }

            // Display headers.
            GenerateReport.displayHeaders(reportResponse.Headers);

            // Display first page of results.
            GenerateReport.displayRows(reportResponse.Rows);

            var totalRows = Math.Min(int.Parse(reportResponse.TotalMatchedRows), 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(maxReportPageSize, totalRows - startIndex);

                // Run next page of report.
                reportResponse = getPage(reportRequest, startIndex, pageSize);

                // If the report size changes in between paged requests, the result may be empty.
                if (reportResponse.Rows == null || reportResponse.Rows.Count == 0)
                {
                    break;
                }

                // Display results.
                GenerateReport.displayRows(reportResponse.Rows);
            }

            CommandLine.WriteLine();
        }