Search code examples
google-ads-apigoogle-query-language

ROLLING date range in a Google Ads API Query


let startDate = '2020-01-01';
let endDate = new Date().toISOString().slice(0, 10).toString();

//
"WHERE segments.date BETWEEN '2020-01-01' AND 'endDate' "

Returns this error:

Exception: Call to GoogleAdsService.Search failed: Condition 'segments.date BETWEEN '2020-01-01' and 'endDate'' is invalid: BETWEEN operator must have exactly two values that are both numbers or both date strings in 'YYYY-MM-DD' format.

How can I set endDate to YESTERDAY?


Solution

  • Google Ads Query Language accepts dates both with and without quotes. If you use the ISO-like date format with hyphens, quotes are mandatory, though.

    Assuming that this is JavaScript, you can just do

    "WHERE segments.date BETWEEN '2020-01-01' AND '" + endDate + "'"
    

    Or using template literals (available from ES6 on, note the enclosing backticks):

    `WHERE segments.date BETWEEN '2020-01-01' AND '${endDate}'`