LAST_90 DAYS Adwords script report

26 views
Skip to first unread message

Sofia Aboy

unread,
Jun 26, 2024, 11:57:59 AM (7 days ago) Jun 26
to Google Ads Scripts Forum
Hello Everyone,

I am looking forward to implement a custom date range criteria for one of our adword script reports. I would need report to look at the data from last 90 days. 

These dates can't be fixed i already know i can do it by setting fixed dates in between  (eg., DURING 20240325,20240625)

But I'll schedule it to run daily so I need it to be dynamic.

Thank you

Sigurd Fabrin

unread,
Jun 27, 2024, 6:31:06 AM (6 days ago) Jun 27
to Google Ads Scripts Forum
Sure, you can do sth like the example below:

function main() {
  const daysToLookBack = 180; // number of days ago for start date. End date is yesterday
  const dates = getDates(daysToLookBack);
  const query = `
      SELECT
        campaign.name,
        metrics.impressions
      FROM
        campaign
      WHERE
        metrics.impressions > 100
        AND segments.date BETWEEN "${dates.startDate}" AND "${dates.endDate}"`;
  const response = AdsApp.search(query);
  while (response.hasNext()) {
    const row = response.next()
    console.log(`campaign: "${row.campaign.name}", ${row.metrics.impressions} impr`);
  }
}
function getDates(days) {
  var timeZone = AdsApp.currentAccount().getTimeZone();
  var format = 'yyyy-MM-dd';
  var today = new Date();
  var oneDay = 1000*60*60*24; // in milliseconds
  var xDaysAgo = Utilities.formatDate(new Date(today-(oneDay*days)),timeZone,format);
  var yesterday = Utilities.formatDate(new Date(today-(oneDay)),timeZone,format);
  console.log(`Last ${days} days i.e. start date: ${xDaysAgo} and end date: ${yesterday}\n*\n`);
  return {startDate:xDaysAgo,endDate:yesterday}                  
}



Sigurd

Google Ads Scripts Forum Advisor

unread,
Jun 27, 2024, 8:29:49 AM (6 days ago) Jun 27
to adwords...@googlegroups.com

Hi,

@Sigurd - Thanks for the brief solution.

I would suggest that you follow the code provided by Sigurd and get back to us if you still face any issues. 

This message is in relation to case "ref:!00D1U01174p.!5004Q02tJUnU:ref" (ADR-00244346)

Thanks,
 
Google Logo Google Ads Scripts Team


Reply all
Reply to author
Forward
0 new messages