Google Ads API

Use Case: Fetch Google Ads Campaign Performance Metrics

💼 Business Scenario:

You want to fetch daily impressions, clicks, and cost for all active campaigns in your Google Ads account for reporting or optimization.


⚙️ Setup Prerequisites

  1. Google Ads account

  2. Google Cloud project with OAuth2 credentials

  3. Google Ads API access & developer token

  4. Install the official Python client library:

pip install google-ads

📦 Example Code (Google Ads API v15)

from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException

# Load configuration from google-ads.yaml or pass as dict
client = GoogleAdsClient.load_from_storage("google-ads.yaml")

# Replace with your Google Ads customer ID
customer_id = "INSERT_CUSTOMER_ID_HERE"

# GAQL query to fetch campaign performance
query = ("""
    SELECT
        campaign.id,
        campaign.name,
        metrics.impressions,
        metrics.clicks,
        metrics.cost_micros
    FROM campaign
    WHERE campaign.status = 'ENABLED'
    ORDER BY metrics.impressions DESC
    LIMIT 10
""")

# Issue the request
try:
    ga_service = client.get_service("GoogleAdsService")
    response = ga_service.search(customer_id=customer_id, query=query)

    # Display results
    for row in response:
        campaign = row.campaign
        metrics = row.metrics
        cost = metrics.cost_micros / 1_000_000  # convert from micros to standard currency

        print(f"Campaign ID: {campaign.id}, Name: {campaign.name}")
        print(f"Impressions: {metrics.impressions}, Clicks: {metrics.clicks}, Cost: ₹{cost:.2f}\n")

except GoogleAdsException as ex:
    print(f"Request failed: {ex}")

📘 google-ads.yaml Example Configuration

developer_token: "YOUR_DEVELOPER_TOKEN"
client_id: "YOUR_OAUTH2_CLIENT_ID"
client_secret: "YOUR_OAUTH2_CLIENT_SECRET"
refresh_token: "YOUR_REFRESH_TOKEN"
login_customer_id: "YOUR_MANAGER_ACCOUNT_ID"

🧠 What You Can Do With This API

Task
Description

Campaign reporting

Daily/weekly automated reports

Keyword performance analysis

Optimize based on high/low-performing keywords

Budget management

Adjust budgets dynamically via API

Ad creation automation

Launch new ad groups or campaigns programmatically

A/B testing automation

Rotate ad creatives and track results

Last updated

Was this helpful?