'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. :)
Solution 1:[1]
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>'.
Solution 2:[2]
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"""
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dorian |
| Solution 2 | Dave Davis |
