How to filter for N days (not 7 nor 30)

41 views
Skip to first unread message

Alberto Esteves Correia

unread,
Jul 28, 2023, 5:17:30 AM7/28/23
to Google Ads Scripts Forum
Hi there,

I want to get a report (by GAQL) of last 5 days. 

To do that, I have to select the specific dates because there is no standar time period like that (there is last_7_days and lats_30_days but nor last_5_days).

The problem selecting specific dates, is that would change it manually each day that I want to get this report. 

How could I do that dynamically?

Thanks!


Message has been deleted

Alberto Esteves Correia

unread,
Jul 31, 2023, 5:16:57 AM7/31/23
to Google Ads Scripts Forum
I've found this solution:

  const Ndays = 5;
  const timeZone = AdsApp.currentAccount().getTimeZone();
  const MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
 
  const today = new Date();
  const yesterday = new Date(today.getTime() - MILLIS_PER_DAY);
  const startDate =  new Date(yesterday.getTime() - Ndays*MILLIS_PER_DAY);

  const format = 'yyyy-MM-dd';

With this values, in the gaql query add this segment:

"segments.date BETWEEN '"
                  + Utilities.formatDate(startDate, timeZone, format) + "' AND '"
                  + Utilities.formatDate(yesterday, timeZone, format) + "'";



And it works! 

I hope this help to someone 

Google Ads Scripts Forum Advisor

unread,
Jul 31, 2023, 9:15:41 AM7/31/23
to adwords...@googlegroups.com
Hi Alberto,

I am from the Google Ads Scripts support team.

Thank you for updating us and sharing your found solution for this. You may also refer to our documentation (https://developers.google.com/google-ads/scripts/docs/features/dates) on Working with Dates and Times for more information on important concepts, common pitfalls, and recommended approaches when working with dates and times in Google Ads scripts.

If you have further concerns, please don't hesitate to get back to us and provide the below details. We'll be happy to assist you.
  • Google Ads account ID / CID
  • Script name
 
This message is in relation to case "ref:_00D1U1174p._5004Q2nZGLD:ref"

Thanks,
 
Google Logo Google Ads Scripts Team


Eugenia Perezmonzon

unread,
Aug 1, 2023, 1:14:12 AM8/1/23
to Alberto Esteves Correia a través de Google Ads Scripts Forum
El lun, 31 de jul. de 2023 3:15 a. m., Alberto Esteves Correia a través de Google Ads Scripts Forum <adwords...@googlegroups.com> escribió:
I've found this solution:

  const Ndays = 5;
  const timeZone = AdsApp.currentAccount().getTimeZone();
  const MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
 
  const today = new Date();
  const yesterday = new Date(today.getTime() - MILLIS_PER_DAY);
  const startDate =  new Date(yesterday.getTime() - Ndays*MILLIS_PER_DAY);


With this values, in the gaql query add this segment:

"segments.date BETWEEN '"
                  + Utilities.formatDate(startDate, timeZone, format) + "' AND '"
                  + Utilities.formatDate(yesterday, timeZone, format) + "'";



And it works! 

I hope this help to someone 

El viernes, 28 de julio de 2023 a las 11:17:30 UTC+2, Alberto Esteves Correia escribió:

--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scrip...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/b29e5e48-bdd1-4000-aa04-6f9fbded228an%40googlegroups.com.

Roy Duncan

unread,
Aug 9, 2023, 5:07:20 AM8/9/23
to Alberto Esteves Correia a través de Google Ads Scripts Forum
hello
thanks for infor

Le lun. 31 juil. 2023 à 11:15, Alberto Esteves Correia a través de Google Ads Scripts Forum <adwords...@googlegroups.com> a écrit :
I've found this solution:

  const Ndays = 5;
  const timeZone = AdsApp.currentAccount().getTimeZone();
  const MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
 
  const today = new Date();
  const yesterday = new Date(today.getTime() - MILLIS_PER_DAY);
  const startDate =  new Date(yesterday.getTime() - Ndays*MILLIS_PER_DAY);


With this values, in the gaql query add this segment:

"segments.date BETWEEN '"
                  + Utilities.formatDate(startDate, timeZone, format) + "' AND '"
                  + Utilities.formatDate(yesterday, timeZone, format) + "'";



And it works! 

I hope this help to someone 

El viernes, 28 de julio de 2023 a las 11:17:30 UTC+2, Alberto Esteves Correia escribió:

Lal singh lohra

unread,
Aug 12, 2023, 8:45:35 AM8/12/23
to Alberto Esteves Correia a través de Google Ads Scripts Forum

usjwHePzDd

Lal singh lohra

unread,
Aug 12, 2023, 8:45:37 AM8/12/23
to Alberto Esteves Correia a través de Google Ads Scripts Forum
YccAvJtVMV

Kawita kumari

unread,
Aug 12, 2023, 8:45:43 AM8/12/23
to Alberto Esteves Correia a través de Google Ads Scripts Forum

TfVi7gI3AV
Reply all
Reply to author
Forward
0 new messages