Skip to main content

AdWords API Reporting in Python

Project description

Google Ads Reporting in Python

PyPI PyPI - Downloads PyPI - Python Version PyPI - License

An AdWords API large scale reporting tool written in Python. Reports are downloaded as plaintext files but connectivity with a database engine such as MySQL, PostgreSQL, or MongoDB can be implemented upon request.

Overview

pyaw-reporting is an open-source Python package suitable for large scale Google Ads reports. Output reports are comma-separated values (CSV) plaintext files. By default, the package uses 10 threads to download reports simultaneously from different accounts. The number of threads used can be modified using the -n parameter. It has been successfully tested using +100 threads making it useful for heavy load AdWords Manager Accounts.

Supported AdWords API version

The latest API version supported by this package is v201809 with googleads 27.0.0. Older versions of the API are not supported, nor the newer Google Ads API.

Quick Start

Prerequisites

You will need Python 3.6+; this package installs googleads as a dependency. Note that that support for Python 2.7 ended in 2019. Using a virtual environment is recommended to avoid running the package with sudo.

An access token YAML file with the corresponding AdWords credentials is also necessary. By default, the package will look for ~/googleads.yaml unless a different path is passed. The optional parameter client_customer_id must be included in this YAML file, you should enter your AdWords Manager Account id (formerly known as MCC id). This way, all accounts linked to the Manager Account will be retrieved. The sample file example.yaml shows how your token should look like.

AWQL queries could be either passed in the command line with the -a/--awql parameter or stored in a plaintext file and passed via the -q/--query parameter. The module includes a sample query which will retrieve clicks and impressions per ad for the last 7 days. Refer to Report Types and The AdWords Query Language (AWQL) for more information about these queries.

Installation

$ pip install pyaw-reporting

Usage

Command line

usage: awreporting [-h] (-a AWQL | -q QUERY_FILE) [-t TOKEN] [-o OUTPUT]
                   [-n NUM_THREAD] [-v]

AwReporting - Large scale AdWords reporting tool in Python

optional arguments:
  -h, --help                              show this help message and exit
  -t TOKEN, --token TOKEN                 specify AdWords YAML token path
  -o OUTPUT, --output OUTPUT              define output file name
  -n NUM_THREAD, --num-thread NUM_THREAD  set number of threads
  -v, --verbose                           display activity

required arguments:
  -a AWQL, --awql AWQL                    pass AWQL query
  -q QUERY_FILE, --query-file QUERY_FILE  ...or use a query file

For example:

$ awreporting -t example.yaml -a \
  "SELECT ExternalCustomerId, CampaignId, AdGroupId, Id, Date, Clicks, Impressions \
  FROM AD_PERFORMANCE_REPORT WHERE Impressions > 0 DURING LAST_7_DAYS" \
  -o adperformance.csv -n 100

Or, with a query file:

$ awreporting -t example.yaml -q query.txt -o adperformance.csv -n 100

If you experience problems downloading reports, check the resulting logs in awreporting.log or use the -v/--verbose parameter to verify if something is wrong with your token or AWQL query.

Code

If you prefer embedding this package in your own code, you could do like so:

from awreporting import get_report

query = (
    "SELECT ExternalCustomerId, CampaignId, AdGroupId, Id, Date, Clicks, Impressions "
    "FROM AD_PERFORMANCE_REPORT "
    "WHERE Impressions > 0 "
    "DURING LAST_7_DAYS"
)
get_report('example.yaml', query, 'adperformance.csv', 100)

About the YAML token

The example token file provided example.yaml is not valid. Refer to this guide if you are using the AdWords API for the first time.

Disclaimer

This is neither an official AdWords API repository nor a clone of AwReporting. Consider using AwReporting if you are a Java developer. This framework no longer supports Python 2.7; only Python 3.6 or greater are compatible since googleads requires it.

Troubleshooting

We recommend that you try the app with a few number of threads first (the default is 10) and increase the amount accordingly. The Google Ads server may complain when many API calls are made at the same time, but those exceptions are handled by the app. We have successfully generated huge report files using 200 threads.

When using this tool it might be necessary to enable a DNS cache in your system, such as nscd. This should eliminate DNS lookup problems when repeatedly calling the AdWords API server. For example, if you find many URLError: <urlopen error [Errno -2] Name or service not known> in your logs, enable the DNS cache.

In some Linux systems nscd is not enabled by default, but it can be started with:

# systemctl start nscd

Useful links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyaw-reporting-0.0.6.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyaw_reporting-0.0.6-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file pyaw-reporting-0.0.6.tar.gz.

File metadata

  • Download URL: pyaw-reporting-0.0.6.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for pyaw-reporting-0.0.6.tar.gz
Algorithm Hash digest
SHA256 7c47c987618e3176c2460dc4331f5a2e780fbf56c58b3120fb1ce54548f09e00
MD5 768c4f8b4de71908f4701bb61bf5fb84
BLAKE2b-256 bab124b99f2dba25d72873ce5529416c3a344876e561346c11c547e6c34c2d43

See more details on using hashes here.

File details

Details for the file pyaw_reporting-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: pyaw_reporting-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for pyaw_reporting-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d78c58e8593799308f24386f494d200f22ad2e024911553282c0a0495a07da6a
MD5 451185e4e1eaa6b3ffac75c8d4ecc0b5
BLAKE2b-256 0e43e736c182134973882075a8b1ebf563cb155768b96295fb20e1ade74e494c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page