Library for fetching reports from Google Ads API and saving them locally / BigQuery.
Project description
Google Ads API Report Fetcher (gaarf)
Python version of Google Ads API Report Fetcher tool a.k.a. gaarf
.
Please see the full documentation in the root README.
Getting started
Prerequisites
- Python 3.8+
- pip installed
- Google Ads API enabled
google-ads.yaml
file. Learn how to create one here.
Installation and running
- create virtual environment and install the tool
python3 -m venv gaarf
source gaarf/bin/activate
pip install google-ads-api-report-fetcher
install the latest development version with
pip install -e git+https://github.com/google/ads-api-report-fetcher.git#egg=google-ads-api-report-fetcher\&subdirectory=py
Versions of the library
google-ads-api-report-fetcher[sqlalchemy]
- version with SQLalchemy supportgoogle-ads-api-report-fetcher[simulator]
- version with support for simulating query results instead of calling Google Ads API.google-ads-api-report-fetcher[full]
- full version
- Run the tool with
gaarf
command:
gaarf <queries> [options]
Documentation on available options see in the root README.md.
Using as a library
Once google-ads-api-report-fetcher
is installed you can use it as a library.
from gaarf.api_clients import GoogleAdsApiClient
from gaarf.query_executor import AdsReportFetcher, AdsQueryExecutor
from gaarf.io import reader, writer
# initialize Google Ads API client
client = GoogleAdsApiClient(path_to_config="google-ads.yaml", version="v12")
customer_ids = ['1', '2']
# Fetch report and store results in a variable
# initialize report fetcher to get reports
report_fetcher = AdsReportFetcher(client, customer_ids)
# create query text
query_text = "SELECT campaign.id AS campaign_id FROM campaign"
# Execute query and store campaigns variable
campaigns = report_fetcher.fetch(query_text)
# iterate over report
unique_campaigns = set([row.campaign_id for row in campaigns])
# convert `campaigns` to common data structures
campaigns_list = campaigns.to_list()
campaigns_df = campaigns.to_pandas()
# Execute query from file and save results to CSV
# initialize query_executor to fetch report and store them in local/remote storage
query_executor = AdsQueryExecutor(client)
# initialize writer
csv_writer = writer.CsvWriter(destination_folder="/tmp")
reader_client = reader.FileReader()
# execute query and save to csv
query_executor.execute(
query_text=query_text,
query_name="campaign",
customer_ids=customer_ids,
write_client=csv_writer)
# execute query from file and save to csv
query_path="path/to/query.sql"
query_executor.execute(
query_text=reader_client.read(query_path),
query_name=query_path,
customer_ids=customer_ids,
write_client=csv_writer)
Python specific command line flags
--optimize-performance
- accepts one of the following values:NONE
- no optimizations are donePROTOBUF
- convert Google Ads API response to protobuf before parsing (speeds up query execution 5x times but forces conversion of ENUMs to integers instead of strings)BATCH
- converts all response of Ads API to a list and then parses its content in parallelBATCH_PROTOBUF
- combinesBATCH
andPROTOBUF
approaches.
Disclaimer
This is not an officially supported Google product.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file google-ads-api-report-fetcher-1.2.0.tar.gz
.
File metadata
- Download URL: google-ads-api-report-fetcher-1.2.0.tar.gz
- Upload date:
- Size: 29.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b63a58cddfeac96bcd3383daed4b32b6c4d0f69d16baa049c2f2179a6fc52d0c |
|
MD5 | e77c94a75c775f74488c06e3cdb58264 |
|
BLAKE2b-256 | 8470a8db9268b5648ef12eff3520a907e81a464206be339937a764d7af718e9a |