This Python 3 code snippet demonstrates the usage of the Google Ads API (formerly known as AdWords API), to generate and retrieve a report. The code can be modified to retrieve all kinds of reports (in the example, a report of type CLICK_PERFORMANCE_REPORT is requested as CSV).

 

Prerequisites

  • App Setup in the Google Developer Console with Google Ads API enabled
  • Google Ads API key (to get from your MCC under TOOLS & SETTINGS > SETUP > API Center)
  • Generate a refresh token using the generate_refresh_token.py script from the Google Ads API examples
  • Copy the googleAds.yaml example file from the Google Ads API Examples to your script directory and insert your client-id, client-secret, API key and refresh token
  • Install the Google Ads Python Library (pip install googleads)

 

Code Snippet


#!/usr/bin/python3
# -*- coding: UTF-8 -*-

from googleads import adwords
from googleads import errors
import time
import datetime
import os
import sys

if __name__ == '__main__':

  ## initialize google adwords client object
  adwords_client = adwords.AdWordsClient.LoadFromStorage("googleAds.yaml")

  ## set your customer-ID
  adwords_client.SetClientCustomerId('123-456-7890')

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

  ## get CLICK_PERFORMANCE report for yesterday as an example
  report_date = datetime.datetime.now()-datetime.timedelta(days=1)
  report_date_string = report_date.strftime("%Y%m%d")

  ## build the report query
  report_query = (adwords.ReportQueryBuilder()
      .Select('GclId','CampaignName', 'AdGroupName', 'CriteriaParameters')
      .From('CLICK_PERFORMANCE_REPORT')
      .During(start_date=report_date_string,end_date=report_date_string)
      .Build())

  ## download the report as CSV into string
  csv_report = report_downloader.DownloadReportWithAwql(
      report_query, 'CSV', skip_report_header=True,
      skip_column_header=True, skip_report_summary=True,
      include_zero_impressions=False)

   ## do whatever you want with the generated report 


 

This website is making use of cookies for website analysis. Data is collected anonymously and solely for the purpose of improving the website. Do you agree to the use of cross-session cookies?