Urgent.News

What's breaking now, across thousands of outlets.

Tech

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…

Advertisers operating in multiple countries often require a daily Google Ads cost report. This file contains essential data such as the date, country, campaign, cost, clicks, and conversions. While manual exporting is impractical, the Google Ads API can provide this information with a single query.

To access the data, you need a developer token, OAuth credentials, and the customer ID of the account. Install the official client library and store credentials securely in a google-ads.yaml file outside the repository.

The Google Ads Query Language functions similarly to SQL. The geographic view allows retrieval of cost per country. Using the GoogleAdsClient, load the configuration from the google-ads.yaml file and access the GoogleAdsService. Define the CUSTOMER_ID as 1234567890 and calculate yesterday's date using the datetime module.

Construct the query to select date, country_criterion_id, campaign name, cost, clicks, and conversions from the geographic_view where the date matches yesterday. Initialize an empty list to store the rows of data. Iterate through the search_stream results, appending each row to the list. Write the data to a CSV file named gads-YYYY-MM-DD.csv using the csv writer.

To differentiate countries, download the geo targets CSV once and join on it or query geo_target_constant for the IDs encountered. Remember, cost is reported in the account's currency and time zone. If multiple accounts use different currencies, convert to a single currency for consistency. Ensure the file includes a currency column for finance audits.

Schedule the script to run daily using cron or a cloud function, storing the CSV files in shared storage. Implement a simple check to alert if the previous day's cost is zero or exceeds double the seven-day average. This helps detect tracking issues, paused billing, or unexpected campaign performance.

By consolidating daily cost data by country, you can analyze return on ad spend (ROAS) per market and allocate budget accordingly. The script also provides a foundation for sanity-checking targets before scaling operations. PandaWM, an overseas marketing team based in Hong Kong, utilizes this approach to streamline their advertising analytics.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Monday 2 November →