Custom Date Range

174 views
Skip to first unread message

KGuiste1

unread,
Feb 22, 2021, 2:22:57 PM2/22/21
to Google Ads Scripts Forum

Hi,

This code is coming back with an error:

var dateFrom = "20210201"
var dateTo = "20210201"

var camIter = AdsApp.campaigns().withCondition("Status = ENABLED").forDateRange(dateFrom, dateTo).get();

The daterange is not valid

I've checked the custom date range syntax and it seems like I'm doing everything right. Can someone assist me please.

Kind Regards

KGuiste1

unread,
Feb 22, 2021, 3:08:47 PM2/22/21
to Google Ads Scripts Forum
Hi,

I figured it out, dateFrom and dateTo need to be numbers, not strings in the AWQL... apparently that makes a difference

Regards

Google Ads Scripts Forum Advisor

unread,
Feb 22, 2021, 10:54:23 PM2/22/21
to adwords...@googlegroups.com

Hi there,

Thanks for raising this to us.

It's great that you've figured it out on your own. However, let me provide a more context when it comes to date ranges.

  • If you're using a selector, date format inside the forDateRange() method should be in string.
  • If you're using Reports, custom date range (FROM & TO) could be a string (FROM: "20210201" TO: "20210201" ) or a number (FROM: 20210201 TO: 20210201).

Hope this helps.

Regards,

Google Logo
Mark Kevin Albios
Google Ads Scripts Team
 

 

ref:_00D1U1174p._5004Q2CTccT:ref

KGuiste1

unread,
Feb 23, 2021, 2:52:19 AM2/23/21
to Google Ads Scripts Forum
The reason I'm writing is because the string variation wasn't working when I used a variable binding with a data in "YYYYMMDD" format. I just tried it again with single quotes and it made no difference.

It only seems to work as a string when directly entered into the report AWQL.

Maybe you all can try it from your end using the variable instead of the value and let me know if it works for you.

Thanks

Google Ads Scripts Forum Advisor

unread,
Feb 23, 2021, 4:54:58 AM2/23/21
to adwords...@googlegroups.com

Hi there,

 

Harry hare, teammate of Mark on the Google Ads Scripts Team. Allow me to assist you further.

 

Mark and I made a couple of tests, but we can't seem to replicate the issue that you have mentioned. Please see below two date range approaches that you can test on your end. If the issue still persists, please let us know and kindly provide a screenshot showing the issue so that we will be able to check this on our end.

 

 var dateFrom = "20210201"
  var dateTo = "20210201"
 
  var camIter = AdsApp.campaigns().withCondition("Status = ENABLED").forDateRange(dateFrom, dateTo).get();
  while(camIter.hasNext()){
    var campaign = camIter.next();
    Logger.log(campaign.getId() + " - " +campaign.getName())
  }
  
  Logger.log("==================");
  
  var report = AdsApp.report(
      'SELECT CampaignName, Clicks, Impressions, Cost ' +
      'FROM   CAMPAIGN_PERFORMANCE_REPORT ' +
      'WHERE  Impressions < 10 ' +
      'DURING ' +dateFrom + ','+dateTo);
 
  var rows = report.rows();
  while (rows.hasNext()) {
    var row = rows.next();
    var campaignName = row['CampaignName'];
    var clicks = row['Clicks'];
    var impressions = row['Impressions'];
    var cost = row['Cost'];
    Logger.log(campaignName + ',' + clicks + ',' + impressions + ',' + cost)

 

Let me know also if you have other questions

 

Thanks,

Google Logo
Harry Cliford Rivera
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2CTccT:ref

KGuiste1

unread,
Feb 23, 2021, 8:00:20 AM2/23/21
to Google Ads Scripts Forum
I tried the code you provided and it worked, however when I tried drawing values from a sheet, to define dateTo and dateFrom it doesn't work

e.g.

var dateFrom = ss.getRangeByName("DateFrom").getValue();
var dateTo = ss.getRangeByName("DateTo").getValue();

Logger.log(dateFrom) // -> "20200901"
Logger.log(typeof dateFrom) // -> String

Logger.log(dateTo) // ->  "20210201"
Logger.log(typeof dateTo) // ->  String

var camIter = AdsApp.campaigns().withCondition("Status = ENABLED").forDateRange(dateFrom, dateTo).get();
// -> The daterange is not valid. Please check spelling and casing. Valid values: TODAY, YESTERDAY, LAST_7_DAYS etc...

I don't know why this is happening. Why the script isn't accepting the string arguments drawn from Google sheets... it doesn't make sense to me.
It works if they're Number literals, but not strings.

Google Ads Scripts Forum Advisor

unread,
Feb 24, 2021, 12:00:30 AM2/24/21
to adwords...@googlegroups.com

Hi,

 

I can't seem to replicate the error you are encountering. Both ways should work if the date values are both type of string and if the sheets returns the values as numbers, it will only work with the report query. Please be informed that the getValue() method returns the value of the top-left cell in the range. The value may be of type Number, Boolean, Date, or String depending on the value of the cell. You may set cell format by navigating to Format > Number in the Sheets menu.

 

Kindly provide your CID and script name so that I can take a closer look on what causes the issue that you are encountering and assist you further. You may send them here or privately via the reply to author option. If this option is not available at your end, you may send it through our email (googleadsscr...@google.com) instead.

KGuiste1

unread,
Feb 24, 2021, 2:17:11 AM2/24/21
to Google Ads Scripts Forum
Hi,

Based on you results I double-checked things on my end. I made a syntax error. Instead of setting the cell values to plain text i had entered the dates into the cells with quotes around them e.g. ["20210223"].
After changing the cell types to plain text and omitting the quotes it all works fine.

Regards
Kurvin

Google Ads Scripts Forum Advisor

unread,
Feb 24, 2021, 4:45:22 AM2/24/21
to adwords...@googlegroups.com

Hello,

 

Thanks for coming back and informing us that the issue you have encountered is now resolved. Let me know if you have other questions or concerns.

Jan Hahlbrock

unread,
Feb 24, 2021, 7:27:55 AM2/24/21
to Google Ads Scripts Forum
Hello,
I have two small functions, that I'd like to share with you:

function getDateString(timePoint) {
    var timeZone = AdsApp.currentAccount().getTimeZone();
    var date = String(Utilities.formatDate(timePoint, timeZone, 'yyyyMMdd'));
    return date;
}

function getTimePointInPast(daysFromToday) {
    var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
    var now = new Date();
    var timePoint = new Date(now - daysFromToday * MILLIS_PER_DAY);
    return timePoint;
}

I use those in combination, and it lets me create DateStrings from a simple number. If for example I want the last 7 Days not including today, I use:

 var dateFrom = getDateString(getTimePointInPast(1));
var dateTo = getDateString(getTimePointInPast(7));

in my report i then use them like this:
var keyReportkurz = AdWordsApp.report("SELECT Id, AdGroupId, Cost, KeywordMatchType, Conversions, CpcBid, ConversionValue FROM KEYWORDS_PERFORMANCE_REPORT WHERE Cost > 0 AND CampaignStatus = ENABLED AND AdGroupStatus = ENABLED DURING " +  dateFrom   + "," +  dateTo   + "");

or like so:
var keywords = label.keywords().withCondition("Status = ENABLED").withCondition("CampaignStatus = ENABLED").withCondition("AdGroupStatus = ENABLED").withCondition("Cost > 0").forDateRange(""+ dateFrom  +","+ dateTo+"").get();

Hope you can make some use of this.

Google Ads Scripts Forum Advisor

unread,
Feb 25, 2021, 9:28:37 PM2/25/21
to adwords...@googlegroups.com

Hi Jan,

Thanks for sharing your script and for extending your knowledge to this Google Ads Scripts community.

Let us know if you have any questions/concerns related to Google Ads Scripts. We'll be happy to assist you.

Regards,

Google Logo
Mark Kevin Albios
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2CTccT:ref
Reply all
Reply to author
Forward
0 new messages