Example

Let's take a look at a common use case: summarizing the performance of your account over the last 30 days by campaign, segmented by device. The query for this report will be as follows:

SELECT campaign.name, campaign.status, device, metrics.impressions, metrics.clicks,
       metrics.ctr, metrics.average_cpc, metrics.cost_micros
FROM campaign
WHERE date DURING LAST_30_DAYS

To issue this request, simply pass the Google Ads Query Language statement above to the GoogleAdsService.Search interface.

HTTP request URL

The request consists of an HTTP POST to the Google Ads API server at the following URL:

https://googleads.googleapis.com/v0/customers/{customer_id}/googleAds:search

Complete HTTP request sample

Here is a complete example showing the report definition above enclosed within an HTTP POST request.

POST /v0/customers/{customer_id}/googleAds:search HTTP/1.1
Host: googleads.googleapis.com
User-Agent: curl
Content-Type: application/json
Accept: application/json
Authorization: Bearer [Enter OAuth 2.0 access token here]
developer-token: [Enter developerToken here]

Parameters:
{
  "query" : "SELECT campaign.name, campaign.status, device, metrics.impressions, metrics.clicks,
                    metrics.ctr, metrics.average_cpc, metrics.cost_micros
            FROM campaign
            WHERE date DURING LAST_30_DAYS"
}

Code samples

Use the snippet below as a starting point. Modify the portion that iterates over all GoogleAdsRow objects as needed.

Java

private void runExample(GoogleAdsClient googleAdsClient, long customerId) {
  try (GoogleAdsServiceClient googleAdsServiceClient =
      googleAdsClient.getGoogleAdsServiceClient()) {
    String searchQuery =
        "SELECT campaign.id, "
            + "campaign.name, "
            + "ad_group.id, "
            + "ad_group.name, "
            + "ad_group_criterion.criterion_id, "
            + "ad_group_criterion.keyword.text, "
            + "ad_group_criterion.keyword.match_type, "
            + "metrics.impressions, "
            + "metrics.clicks, "
            + "metrics.cost_micros "
            + "FROM keyword_view "
            + "WHERE date DURING LAST_7_DAYS "
            + "AND campaign.advertising_channel_type = 'SEARCH' "
            + "AND ad_group.status = 'ENABLED' "
            + "AND ad_group_criterion.status IN ('ENABLED', 'PAUSED') "
            // Limits to the 50 keywords with the most impressions in the date range.
            + "ORDER BY metrics.impressions DESC "
            + "LIMIT 50";

    // Creates a request that will retrieve all keyword statistics using pages of the specified
    // page size.
    SearchGoogleAdsRequest request =
        SearchGoogleAdsRequest.newBuilder()
            .setCustomerId(Long.toString(customerId))
            .setPageSize(PAGE_SIZE)
            .setQuery(searchQuery)
            .build();
    // Issues the search request.
    SearchPagedResponse searchPagedResponse = googleAdsServiceClient.search(request);
    // Iterates over all rows in all pages and prints the requested field values for the keyword
    // in each row.
    for (GoogleAdsRow googleAdsRow : searchPagedResponse.iterateAll()) {
      Campaign campaign = googleAdsRow.getCampaign();
      AdGroup adGroup = googleAdsRow.getAdGroup();
      AdGroupCriterion adGroupCriterion = googleAdsRow.getAdGroupCriterion();
      Metrics metrics = googleAdsRow.getMetrics();

      System.out.printf(
          "Keyword text '%s' with "
              + "match type '%s' "
              + "and ID %d "
              + "in ad group '%s' "
              + "with ID %d "
              + "in campaign '%s' "
              + "with ID %d "
              + "had %d impression(s), "
              + "%d click(s), "
              + "and %d cost (in micros) "
              + "during the last 7 days.%n",
          adGroupCriterion.getKeyword().getText().getValue(),
          adGroupCriterion.getKeyword().getMatchType(),
          adGroupCriterion.getCriterionId().getValue(),
          adGroup.getName().getValue(),
          adGroup.getId().getValue(),
          campaign.getName().getValue(),
          campaign.getId().getValue(),
          metrics.getImpressions().getValue(),
          metrics.getClicks().getValue(),
          metrics.getCostMicros().getValue());
    }
  }
}

C#

public void Run(GoogleAdsClient client, long customerId) {
  // Get the GoogleAdsService.
  GoogleAdsServiceClient googleAdsService = client.GetService(Services.V0.GoogleAdsService);

  // Create the query.
  string query =
      $@"SELECT
             campaign.id,
             campaign.name,
             ad_group.id,
             ad_group.name,
             ad_group_criterion.criterion_id,
             ad_group_criterion.keyword.text,
             ad_group_criterion.keyword.match_type,
             metrics.impressions,
             metrics.clicks,
             metrics.cost_micros
         FROM keyword_view
         WHERE date DURING LAST_7_DAYS
             AND campaign.advertising_channel_type = 'SEARCH'
             AND ad_group.status = 'ENABLED'
             AND ad_group_criterion.status IN ('ENABLED','PAUSED')
         ORDER BY metrics.impressions DESC
         LIMIT 50";
  try {
    // Issue a search request.
    PagedEnumerable<SearchGoogleAdsResponse, GoogleAdsRow> result =
        googleAdsService.Search(customerId.ToString(), query);

    // Display the results.
    foreach (GoogleAdsRow criterionRow in result) {
      Console.WriteLine($"Keyword with text '{criterionRow.AdGroupCriterion.Keyword.Text}', " +
          $" match type '{criterionRow.AdGroupCriterion.Keyword.MatchType}' and " +
          $"ID {criterionRow.AdGroupCriterion.CriterionId} " +
          $"in ad group '{criterionRow.AdGroup.Name}' with " +
          $"ID {criterionRow.AdGroup.Id} " +
          $"in campaign '{criterionRow.Campaign.Name}' " +
          $"with ID {criterionRow.Campaign.Id} " +
          $"had {criterionRow.Metrics.Impressions.ToString()} impressions, " +
          $"{criterionRow.Metrics.Clicks} clicks, and " +
          $"{criterionRow.Metrics.CostMicros} cost (in micros)" +
          "during the last 7 days.");
    }
  } catch (GoogleAdsException e) {
    Console.WriteLine("Failure:");
    Console.WriteLine($"Message: {e.Message}");
    Console.WriteLine($"Failure: {e.Failure}");
    Console.WriteLine($"Request ID: {e.RequestId}");
  }
}

PHP

<?php
/**
 * Copyright 2018 Google LLC
 *
 * 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
 *
 *     https://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.
 */

namespace Google\Ads\GoogleAds\Examples\BasicOperations;

require __DIR__ . '/../../vendor/autoload.php';

use GetOpt\GetOpt;
use Google\Ads\GoogleAds\Examples\Utils\ArgumentNames;
use Google\Ads\GoogleAds\Examples\Utils\ArgumentParser;
use Google\Ads\GoogleAds\Lib\GoogleAdsClient;
use Google\Ads\GoogleAds\Lib\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\Lib\GoogleAdsException;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\V0\Errors\GoogleAdsError;
use Google\Ads\GoogleAds\V0\Services\GoogleAdsRow;
use Google\ApiCore\ApiException;

/**
 * This example gets keyword performance statistics for the 50 keywords with the most impressions
 * over the last 7 days.
 */
class GetKeywordStats
{
    const CUSTOMER_ID = 'INSERT_CUSTOMER_ID_HERE';
    const PAGE_SIZE = 50;

    public static function main()
    {
        // Either pass the required parameters for this example on the command line, or insert them
        // into the constants above.
        $options = ArgumentParser::parseCommandArguments([
            ArgumentNames::CUSTOMER_ID => GetOpt::REQUIRED_ARGUMENT
        ]);

        // Generate a refreshable OAuth2 credential for authentication.
        $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();

        // Construct a Google Ads client configured from a properties file and the
        // OAuth2 credentials above.
        $googleAdsClient = (new GoogleAdsClientBuilder())->fromFile()
            ->withOAuth2Credential($oAuth2Credential)
            ->build();

        try {
            self::runExample(
                $googleAdsClient,
                $options[ArgumentNames::CUSTOMER_ID] ?: self::CUSTOMER_ID
            );
        } catch (GoogleAdsException $googleAdsException) {
            printf(
                "Request with ID '%s' has failed.%sGoogle Ads failure details:%s",
                $googleAdsException->getRequestId(),
                PHP_EOL,
                PHP_EOL
            );
            foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) {
                /** @var GoogleAdsError $error */
                printf(
                    "\t%s: %s%s",
                    $error->getErrorCode()->getErrorCode(),
                    $error->getMessage(),
                    PHP_EOL
                );
            }
        } catch (ApiException $apiException) {
            printf(
                "ApiException was thrown with message '%s'.%s",
                $apiException->getMessage(),
                PHP_EOL
            );
        }
    }

    /**
     * Runs the example.
     *
     * @param GoogleAdsClient $googleAdsClient the Google Ads API client
     * @param int $customerId the client customer ID without hyphens
     */
    public static function runExample(GoogleAdsClient $googleAdsClient, $customerId)
    {
        $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
        // Creates a query that retrieves all keyword statistics.
        $query =
            "SELECT campaign.id, "
                . "campaign.name, "
                . "ad_group.id, "
                . "ad_group.name, "
                . "ad_group_criterion.criterion_id, "
                . "ad_group_criterion.keyword.text, "
                . "ad_group_criterion.keyword.match_type, "
                . "metrics.impressions, "
                . "metrics.clicks, "
                . "metrics.cost_micros "
            . "FROM keyword_view "
            . "WHERE date DURING LAST_7_DAYS "
                . "AND campaign.advertising_channel_type = 'SEARCH' "
                . "AND ad_group.status = 'ENABLED' "
                . "AND ad_group_criterion.status IN ('ENABLED', 'PAUSED') "
            // Limits to the 50 keywords with the most impressions in the date range.
            . "ORDER BY metrics.impressions DESC "
            . "LIMIT 50";

        // Issues a search request by specifying page size.
        $response =
            $googleAdsServiceClient->search($customerId, $query, ['pageSize' => self::PAGE_SIZE]);

        // Iterates over all rows in all pages and prints the requested field values for
        // the keyword in each row.
        foreach ($response->iterateAllElements() as $googleAdsRow) {
            /** @var GoogleAdsRow $googleAdsRow */
            $campaign = $googleAdsRow->getCampaign();
            $adGroup = $googleAdsRow->getAdGroup();
            $adGroupCriterion = $googleAdsRow->getAdGroupCriterion();
            $metrics = $googleAdsRow->getMetrics();
            // A mapping of enum names to values for keyword match type can be found in:
            // KeywordMatchTypeEnum_KeywordMatchType.php
            printf(
                "Keyword text '%s' with "
                . "match type %d "
                . "and ID %d "
                . "in ad group '%s' "
                . "with ID %d "
                . "in campaign '%s' "
                . "with ID %d "
                . "had %d impression(s), "
                . "%d click(s), "
                . "and %d cost (in micros) "
                . "during the last 7 days.%s",
                $adGroupCriterion->getKeyword()->getText()->getValue(),
                $adGroupCriterion->getKeyword()->getMatchType(),
                $adGroupCriterion->getCriterionId()->getValue(),
                $adGroup->getName()->getValue(),
                $adGroup->getId()->getValue(),
                $campaign->getName()->getValue(),
                $campaign->getId()->getValue(),
                $metrics->getImpressions()->getValue(),
                $metrics->getClicks()->getValue(),
                $metrics->getCostMicros()->getValue(),
                PHP_EOL
            );
        }
    }
}

GetKeywordStats::main();

Ruby

def get_keyword_stats(customer_id)
  # This will read a config file from ENV['HOME']/googleads_config.rb
  # when called without parameters.
  client = Google::Ads::Googleads::GoogleadsClient.new

  ga_service = client.service(:GoogleAds)

  # Limits to the 50 keywords with the most impressions in the date range.
  # If you wish to exclude entries with zero impressions, include a
  # predicate in the WHERE statement like 'metrics.impressions > 0'
  query = <<~QUERY
    SELECT campaign.id,
           campaign.name,
           ad_group.id,
           ad_group.name,
           ad_group_criterion.criterion_id,
           ad_group_criterion.keyword.text,
           ad_group_criterion.keyword.match_type,
           metrics.impressions,
           metrics.clicks,
           metrics.cost_micros
    FROM keyword_view
    WHERE date DURING LAST_7_DAYS
      AND campaign.advertising_channel_type = 'SEARCH'
      AND ad_group.status = 'ENABLED'
      AND ad_group_criterion.status IN ('ENABLED', 'PAUSED')
    ORDER BY metrics.impressions DESC
    LIMIT 50
  QUERY

  response = ga_service.search(customer_id, query, page_size: PAGE_SIZE)

  if response.response.results.empty?
    puts sprintf("The given query returned no entries:\n %s", query)
    return
  end

  response.each do |row|
    campaign = row.campaign
    ad_group = row.ad_group
    criterion = row.ad_group_criterion
    metrics = row.metrics

    puts sprintf("Keyword text '%s' with match type '%s' and ID %d in ad group"\
        " '%s' with ID %d in campaign '%s' with ID %d had %d impression(s), "\
        "%d click(s), and %d cost (in micros) during the last 7 days.",
        criterion.keyword.text,
        criterion.keyword.match_type,
        criterion.criterion_id,
        ad_group.name,
        ad_group.id,
        campaign.name,
        campaign.id,
        metrics.impressions,
        metrics.clicks,
        metrics.cost_micros)
  end
end

The example above will return all keywords from search campaigns with metrics over the last 7 days.

Next steps

Send feedback about...

Google Ads API Beta
Google Ads API Beta
Need help? Visit our support page.