Пример #1
0
        // [END navigate_search_result_pages_caching_tokens]

        /// <summary>
        /// Updates the cache of page tokens based on a page that was retrieved.
        /// </summary>
        /// <param name="pageTokens">The cache of page tokens to update.</param>
        /// <param name="response">The response that was retrieved.</param>
        /// <param name="pageNumber">The number of the page that was retrieved.</param>
        /// <returns></returns>
        private static void CacheNextPageToken(Dictionary <int, string> pageTokens,
                                               SearchGoogleAdsResponse response, int pageNumber)
        {
            if (pageNumber == 0)
            {
                pageTokens[0] = "";
                return;
            }
            if (string.IsNullOrEmpty(response.NextPageToken) || pageTokens.ContainsKey(pageNumber))
            {
                return;
            }
            // Updates the cache with the next page token if it is not set yet.
            pageTokens[pageNumber] = response.NextPageToken;
            Console.WriteLine($"Cached token for page #{pageNumber + 1}.");
        }
Пример #2
0
        // [START navigate_search_result_pages_caching_tokens]
        /// <summary>
        /// Fetches and prints the results of a page of a search using a cache of page tokens.
        /// </summary>
        /// <param name="googleAdsService">The Google Ads API Service client.</param>
        /// <param name="request">The request.</param>
        /// <param name="pageNumber">The number of the page to fetch and print results for.</param>
        /// <param name="pageTokens">The cache of page tokens to use and update.</param>
        /// <returns></returns>
        private static void FetchAndPrintPageResults(GoogleAdsServiceClient googleAdsService,
                                                     SearchGoogleAdsRequest request, int pageNumber, Dictionary <int, string> pageTokens)
        {
            int currentPageNumber = pageNumber;

            // There is no need to fetch the pages we already know the page tokens for.
            if (pageTokens.ContainsKey(pageNumber - 1))
            {
                Console.WriteLine("The token of the requested page was cached, we will use it " +
                                  "to get the results.");
                currentPageNumber = pageNumber;
            }
            else
            {
                Console.WriteLine("The token of the requested page was never cached, we will " +
                                  $"use the closest page we know the token for (page #{pageNumber}) and " +
                                  $"sequentially get pages from there.");
                currentPageNumber = pageNumber;
                while (!pageTokens.ContainsKey(currentPageNumber))
                {
                    currentPageNumber--;
                }
            }

            SearchGoogleAdsResponse response = null;

            // Fetches next pages in sequence and caches their tokens until the requested page
            // results are returned.
            while (currentPageNumber <= pageNumber)
            {
                // Fetches the next page.
                Console.WriteLine($"Fetching page #{currentPageNumber}...");
                request.PageToken = pageTokens[currentPageNumber - 1];
                response          = googleAdsService.Search(request)
                                    .AsRawResponses().First();
                CacheNextPageToken(pageTokens, response, currentPageNumber);
                currentPageNumber++;
            }

            // Prints the results of the requested page.
            Console.WriteLine($"Printing results found for the page #{pageNumber}");
            foreach (GoogleAdsRow row in response.Results)
            {
                Campaign c = row.Campaign;
                Console.WriteLine($" - Campaign with ID {c.Id} and name '{c.Name}'");
            }
        }
Пример #3
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The customer ID for which the call is made.</param>
        public void Run(GoogleAdsClient client, long customerId)
        {
            // Get the GoogleAdsServiceClient.
            GoogleAdsServiceClient googleAdsService =
                client.GetService(Services.V10.GoogleAdsService);

            // The cache of page tokens. The first page's token is always an empty string.
            Dictionary <int, string> pageTokens = new Dictionary <int, string>();

            CacheNextPageToken(pageTokens, null, 0);

            Console.WriteLine("---0. Fetch page #1 to get metadata");

            // Creates a query that retrieves the campaigns.
            string query = $"SELECT campaign.id, campaign.name FROM campaign ORDER BY " +
                           $"campaign.name LIMIT {RESULTS_LIMIT}";

            // Issues a paginated search request.
            SearchGoogleAdsRequest request = new SearchGoogleAdsRequest()
            {
                Query      = query,
                CustomerId = customerId.ToString(),
                // Sets the number of results to return per page.
                PageSize = PAGE_SIZE,
                // Requests to return the total results count. This is necessary to determine
                // how many pages of results there are.
                ReturnTotalResultsCount = true
            };

            try
            {
                SearchGoogleAdsResponse response = googleAdsService.Search(request)
                                                   .AsRawResponses().First();
                CacheNextPageToken(pageTokens, response, 1);

                // Determines the total number of results and prints it.
                // The total results count does not take into consideration the LIMIT clause of
                // the query so we need to find the minimal value between the limit and the total
                // results count.
                long totalNumberOfResults = Math.Min(RESULTS_LIMIT, response.TotalResultsCount);
                Console.WriteLine($"Total number of campaigns found: {totalNumberOfResults}.");

                // Determines the total number of pages and prints it.
                int totalNumberOfPages =
                    (int)Math.Ceiling((double)totalNumberOfResults / PAGE_SIZE);
                Console.WriteLine($"Total number of pages: {totalNumberOfPages}.");
                if (totalNumberOfResults == 0)
                {
                    Console.WriteLine("Could not find any campaigns.");
                    return;
                }

                // Demonstrates how the logic works when iterating pages forward. We select a
                // page that is in the middle of the result set so that only a subset of the page
                // tokens will be cached.
                int middlePageNumber = (int)Math.Ceiling((double)totalNumberOfPages / 2);
                Console.WriteLine($"--- 1. Print results of the page #{middlePageNumber}");
                FetchAndPrintPageResults(googleAdsService, request, middlePageNumber, pageTokens);

                // Demonstrates how the logic works when iterating pages backward with some page
                // tokens that are not already cached.
                Console.WriteLine("--- 2. Print results from the last page to the first");
                for (int i = totalNumberOfPages; i > 0; i--)
                {
                    Console.WriteLine($"--- Printing results for page #{i}");
                    FetchAndPrintPageResults(googleAdsService, request, i, pageTokens);
                }
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }