How to get all campaigns details with google adwords api?

user3911183 picture user3911183 · Nov 11, 2015 · Viewed 10.6k times · Source

i can get the list of campaigns using the google adwords api ( with a test account ) , i would like to get all the details for each campaigns ( impressions , clicks , budget , cost , cpc , ... ) using the api , how to do that ? Trying with this :

  // Get the service, which loads the required classes.
  $campaignService = $user->GetService('CampaignService', ADWORDS_VERSION);

  // Create selector.
  $selector = new Selector();
  $selector->fields = array('Id', 'Name','Impressions', 'Clicks');
  $selector->ordering[] = new OrderBy('Name', 'ASCENDING');

  // Create paging controls.
  $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE);

  do {
    // Make the get request.
    $page = $campaignService->get($selector);

    // Display results.
    if (isset($page->entries)) {
      foreach ($page->entries as $campaign) {
        printf("Campaign with name '%s' and ID '%s' and Impressions %s was found.\n",
            $campaign->name, $campaign->id,$campaign->impressions);
      }
    } else {
      print "No campaigns were found.\n";
    }

    // Advance the paging index.
    $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE;
  } while ($page->totalNumEntries > $selector->paging->startIndex);

But getting this error :

An error has occurred: [SelectorError.INVALID_FIELD_NAME @ serviceSelector; trigger:'Impressions', SelectorError.INVALID
_FIELD_NAME @ serviceSelector; trigger:'Clicks']

Thanks.

Answer

tector picture tector · Nov 26, 2015

For performance data like impressions, clicks and conversions you have to use the ReportingService. (You can not query that informations with the CampaignService) With the ReportingService you have to use the CAMPAIGN_PERFORMANCE_REPORT.

https://developers.google.com/adwords/api/docs/appendix/reports/campaign-performance-report

I recommend to use AWQL for queries instead because it is very similar to SQL. So if you are familiar with SQL it is very easy to understand.

https://developers.google.com/adwords/api/docs/guides/awql

Example for PHP (CriteriaReport): https://github.com/googleads/googleads-php-lib/blob/master/examples/AdWords/v201509/Reporting/DownloadCriteriaReportWithAwql.php