Pliss Burl
Pliss Burl

Reputation: 13

Get the "cost" google adward api with python

I am trying to get the cost of an account in google adward using their API.

What I want is the total cost of this accont so I thaught of using the Account Performance Report report type and it Cost metric.

Here what I tried :

adwords_client = adwords.AdWordsClient.LoadFromStorage(
        "googleads.yaml")

adwords_client.SetClientCustomerId('000-000-0000') 

report_downloader = adwords_client.GetReportDownloader(version='v201806')

# Create report definition.
report_definition = {
      'reportName': 'trying',
      'dateRangeType': 'TODAY',
      'reportType': 'ACCOUNT_PERFORMANCE_REPORT',
      'downloadFormat': 'XML',
      'selector': {
          'fields': [
                     'Cost'],
      },
  }

  print (report_downloader.DownloadReportAsStringWithAwql(
      report_definition, 'XML', skip_report_header=False, skip_column_header=False,
      skip_report_summary=False, include_zero_impressions=True))

This did not work. I get :

AdWordsReportBadRequestError: Type: QueryError.MISSING_SELECT_CLAUSE
Trigger: None
Field Path: None

From that, I understood I had to build a query with a select, so I tried this :

report_query = (adwords.ReportQueryBuilder()
      .Select('Cost')
      .From('ACCOUNT_PERFORMANCE_REPORT')
      .During('TODAY')
      .Build())

  print (report_downloader.DownloadReportAsStringWithAwql(
      report_query, 'XML', skip_report_header=False, skip_column_header=False,
      skip_report_summary=False, include_zero_impressions=True))

And I get this :

<?xml version='1.0' encoding='UTF-8' standalone='yes'?><report><report-name name='ACCOUNT_PERFORMANCE_REPORT'/><date-range date='Jul 16, 2018'/><table><columns><column name='cost' display='Cost'/></columns><row cost='270020000'/></table></report>

As you can see the cost value is very hight and did not correspond to the actual true value. I very new to the API, do you know what am doing wrong or how can I acheve my goal ?

Upvotes: 1

Views: 204

Answers (1)

dorian
dorian

Reputation: 6292

The Adwords API reports currency values as "micros", meaning you'll have to divide these metrics by 1'000'000.

Upvotes: 1

Related Questions