Google Ad result API

182 views
Skip to first unread message

Vibeeshnan Arumugam

unread,
Feb 23, 2022, 9:45:16 AM2/23/22
to adwor...@googlegroups.com, jeyap...@zeptoh.com, ruban...@zeptoh.com
Hi Team,
     Is there any way to get Google ad results such as clicks, impressions etc... for a specific campaign using campaign ID through Google ads API?. When I search for that I got results only for a list of campaigns. I have given the code that I used below for your reference.
customerId  = 5114451929
campaignId = 16258888285 
private void runExample(GoogleAdsClient googleAdsClient, long customerId) {
 try (GoogleAdsServiceClient googleAdsServiceClient =
     googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
   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 campaign.id = 16258888285 ";
   // Constructs the SearchGoogleAdsStreamRequest.
   SearchGoogleAdsStreamRequest request =
       SearchGoogleAdsStreamRequest.newBuilder()
           .setCustomerId(Long.toString(customerId))
           .setQuery(searchQuery)
           .build();
 System.out.println("request"+request);
   // Creates and issues a search Google Ads stream request that will retrieve all of the
   // requested field values for the keyword.
 ServerStream<SearchGoogleAdsStreamResponse> stream = null;
 try {
    stream =
       googleAdsServiceClient.searchStreamCallable().call(request);
   System.out.println("stream"+stream);
 } catch (Exception e) {
e.printStackTrace();
 }
   // Iterates through the results in the stream response and prints all of the requested
   // field values for the keyword in each row.
 try {
   for (SearchGoogleAdsStreamResponse response : stream) {
  System.out.println("response -"+response);
  System.out.println("response -"+response.getResultsList());
     for (GoogleAdsRow googleAdsRow : response.getResultsList()) {
       Campaign campaign = googleAdsRow.getCampaign();
       AdGroup adGroup = googleAdsRow.getAdGroup();
       AdGroupCriterion adGroupCriterion = googleAdsRow.getAdGroupCriterion();
       Metrics metrics = googleAdsRow.getMetrics();
       System.out.printf(
               "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",
           adGroup.getName(),
           adGroup.getId(),
           campaign.getName(),
           campaign.getId(),
           metrics.getImpressions(),
           metrics.getClicks(),
           metrics.getCostMicros());
     }
   }
 } catch (Exception e) {
e.printStackTrace();
 }
 }
}
Thanks & Regards,
Vibeeshnan A.

Google Ads API Forum Advisor

unread,
Feb 24, 2022, 12:37:56 AM2/24/22
to vibe...@gmail.com, jeyap...@zeptoh.com, ruban...@zeptoh.com, adwor...@googlegroups.com

Hello Vibeeshnan,

Thank you for reaching out to us.

Please note that Google Ads API metrics (such as clicks, impressions etc) can be retrieved depending on what level/entity you're trying to get those metrics. Looking on the sample code you're provided, you're trying to get those metrics in the keyword level and filter it by the given campaign ID. I've checked on your query and wasn't able to retrieve any result. Checking on the said campaign from your account, I noticed that there is no keyword available which is the main reason why no metrics are being retrieved.

Regards,

Google Logo
Mark Kevin
Google Ads API Team
 

 

ref:_00D1U1174p._5004Q2WyM0y:ref
Reply all
Reply to author
Forward
0 new messages