Cerin
Cerin

Reputation: 64901

Retrieving earnings from Google Adsense v2 API?

How do you retrieve a report of total earnings for every month with the Google Adsense v2 report generation API?

I'm calling it like:

from googleapiclient.discovery import build
from datetime import datetime, timedelta
from myutils import get_credentials
credentials = get_credentials()
service = build('adsense', 'v2', credentials=credentials)
account_id = 'pub-############'
start_date = datetime.today() - timedelta(days=365*3) # Adsense only supports 3 years worth of data
end_date = datetime.today()
report = service.accounts().reports().generate(
    account=f'accounts/{account_id}',
    dateRange="CUSTOM",
    startDate_year=start_date.year, startDate_month=start_date.month, startDate_day=start_date.day,
    endDate_year=end_date.year, endDate_month=end_date.month, endDate_day=end_date.day,
    metrics=['TOTAL_EARNINGS', 'CLICKS', 'IMPRESSIONS'],
).execute()

But the generated report is:

{'headers': [{'name': 'TOTAL_EARNINGS', 'type': 'METRIC_CURRENCY', 'currencyCode': 'USD'}, {'name': 'CLICKS', 'type': 'METRIC_TALLY'}, {'name': 'IMPRESSIONS', 'type': 'METRIC_TALLY'}], 'startDate': {'year': 2021, 'month': 8, 'day': 14}, 'endDate': {'year': 2024, 'month': 8, 'day': 13}}

and contains no rows, even though the client account definitely has payments made in the last several months.

Why is this?

Upvotes: 0

Views: 128

Answers (0)

Related Questions