How can I embed a custom ROLLING date range in a Google Ads API Query?
I am utilizing the Google ads API to pull campaign data and I am having an issue where I would like to use a fixed date range in my query, but it seems to be unavailable as an option in the documentation.
Here is my query:
GAquery = """ SELECT segments.date, segments.device, campaign.name, metrics.clicks, metrics.conversions, metrics.conversions_value, metrics.cost_micros, metrics.impressions FROM campaign WHERE segments.date DURING LAST_30_DAYS ORDER BY metrics.clicks DESC""" # Issues a search request using streaming. response = ga_service.search_stream(customer_id=customer_id, query=GAquery)
I am looking to get the LAST_60_DAYS instead of 30, but changing the LAST_30_DAYS to LAST_60_DAYS errors out. Has anyone found a way to code a rolling date range that is not a preset option in the system or are we stuck with only the preset options?
Thanks so much for your help. :)
Simply create the variable names yourself and insert them into the query.
from datetime import datetime, timedelta sixty_days_ago = datetime.now().date() + timedelta(days=60) now = datetime.now().date()Then in your query:
GAquery = f""" SELECT segments.date, segments.device, campaign.name, metrics.clicks, metrics.conversions, metrics.conversions_value, metrics.cost_micros, metrics.impressions FROM campaign WHERE segments.date BETWEEN {sixty_days_ago} AND {now} ORDER BY metrics.clicks DESC"""The predefined date ranges you linked to are the only ones that exist.
If you need a different time range than the available options, you're going to have to calculate the start and end dates yourself and use a custom date range with segments.date BETWEEN '<START_DATE>' AND '<END_DATE>'.