DEV Community

Peter Panda
Peter Panda

Posted on

Pulling Daily Google Ads Cost by Country Into a CSV With Python

Teams advertising in several countries often need one simple daily file: date, country, campaign, cost, clicks and conversions. Finance wants it for reconciliation, growth wants it for ROAS checks. Exporting by hand every morning does not scale. The Google Ads API can produce it with one query.

Setup

You need a developer token, OAuth credentials and the customer ID of the account (or a manager account with access). Install the official client library and store credentials in a google-ads.yaml file outside your repository.

pip install google-ads
Enter fullscreen mode Exit fullscreen mode

The query

Google Ads Query Language looks like SQL. The geographic view gives cost per country.

from google.ads.googleads.client import GoogleAdsClient
import csv, datetime

client = GoogleAdsClient.load_from_storage("google-ads.yaml")
svc = client.get_service("GoogleAdsService")
CUSTOMER_ID = "1234567890"

yesterday = (datetime.date.today() - datetime.timedelta(days=1)).isoformat()
query = f"""
SELECT segments.date, geographic_view.country_criterion_id, campaign.name,
       metrics.cost_micros, metrics.clicks, metrics.conversions
FROM geographic_view
WHERE segments.date = '{yesterday}'
"""

rows = []
for batch in svc.search_stream(customer_id=CUSTOMER_ID, query=query):
    for r in batch.results:
        rows.append([r.segments.date, r.geographic_view.country_criterion_id,
                     r.campaign.name, r.metrics.cost_micros / 1_000_000,
                     r.metrics.clicks, round(r.metrics.conversions, 2)])

with open(f"gads-{yesterday}.csv", "w", newline="") as f:
    w = csv.writer(f)
    w.writerow(["date", "country_id", "campaign", "cost", "clicks", "conversions"])
    w.writerows(rows)
Enter fullscreen mode Exit fullscreen mode

Map country IDs to names

The API returns criterion IDs such as 2840 for the United States. Download the geo targets CSV from Google once and join on it, or query geo_target_constant for the IDs you see.

Currency and time zone

Cost is reported in the account currency and the account time zone. If you run accounts in different currencies, add a currency column and convert in a separate step with a fixed daily rate, so finance can audit the conversion. Never mix time zones in one file without saying so.

Schedule it

Run the script once a day with cron or a scheduled cloud function, and write the files to shared storage. Add a simple check that alerts you when yesterday's cost is zero or more than double the seven-day average; that catches broken tracking, paused billing and runaway campaigns early.

Use it

With daily cost by country in one place, you can compare ROAS per market and decide where to move budget. If you want to sanity-check targets before scaling, our ad budget and ROAS calculator turns margin and conversion rate into a break-even ROAS.

Published by PandaWM, an overseas marketing team based in Hong Kong.

Top comments (0)