Giter VIP home page Giter VIP logo

pyaw-reporting's Introduction

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 28.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

pyaw-reporting's People

Contributors

dependabot[bot] avatar gmontamat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pyaw-reporting's Issues

Feature request: make it PyPi package or at least 'pip installable' from github repository

We actively use PyAwReporting as a library in our project(s) and like it! But it is a bit futile to download the files additionally to our 'pip install' workflow because 'pip install' is not possible:

$ pip install pyawreporting
Collecting pyawreporting
  Could not find a version that satisfies the requirement pyawreporting (from versions: )
No matching distribution found for pyawreporting

Also we have to patch the code in awreporting.py just a little, because we do not want to store queries in files but serve them as a parameter:

def read_query(query_file):
    
    if os.path.isfile(query_file):

        try:
            with open(query_file, 'rb') as fin:
                query = fin.read().replace('\r', '').replace('\n', ' ')
        except Exception as e:
            logger.exception("Couldn't read query file")
            sys.exit(1)

    else:

        # if it is not a file it is maybe a query string
        query = query_file

    return query

Use skipReportSummary, skipReportHeader and skipColumnHeader

There's no compatibility with these googleads parameters and by default they take the following values:

'includeZeroImpressions': 'False'
'skipColumnHeader': 'False'
'skipReportHeader': 'True'
'skipReportSummary': 'True'

Column headers are used (from the first decompressed report) to consolidate the full output but the end user may not need it since the query is known. Add the possibility to choose these settings and see them in the final report.

Help needed testing library

Hello users,

I need help testing this library since I no longer have access to an AdWords Manager Account to use it with.

To those that use this code, feel free to update the library with pip install --upgrade pyaw-reporting==0.0.5 and test it using your token, note that pyaw-reporting==0.0.4 was the last version I successfully tested with a valid token, so rollback to that version if needed.

I'll investigate if I can create a test manager account but I doubt I could replicate the volume I used to have to test large downloads. I would also appreciate any pointers on how to generate a test token.

Thanks!

get nothing from AD_PERFORMANCE_REPORT

`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)`
When I use the code below, nothing return.
But when I try to get the CAMPAIGN_PERFORMANCE_REPORT for the same client with the same date range, I get some rows.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.