Filtering Keywords/Ad Groups/Campaigns by time (e.g. "LAST HOUR") instead of date (e.g. "YESTERDAY")

18 views
Skip to first unread message

Viral Mistry

unread,
May 11, 2018, 4:47:06 PM5/11/18
to AdWords Scripts Forum
This code snippet from Google creates a list of the top ten keywords with the high impressions for the previous day:

function main() {
  var keywords = AdWordsApp.keywords()
      .orderBy("Impressions DESC")
      .forDateRange("YESTERDAY")
      .withLimit(10)
      .get();

  Logger.log("10 keywords with most impressions yesterday");
  while (keywords.hasNext()) {
    var keyword = keywords.next();
    Logger.log(keyword.getText() + ": " +
        keyword.getStatsFor("YESTERDAY").getImpressions());
  }
}

If you're trying to do the same thing but for an hourly interval (e.g. last hour) then how would you do about this? It seems like .forDateRange only accepts predefined date ranges like "LAST_14_DAYS"

Figured you might be able to create a date object and plug that in but it didn't work:

var today = new Date();
var hourago = new Date(today.getTime() - (1000*60*60));

function main() {
  var keywords = AdWordsApp.keywords()
      .orderBy("Impressions DESC")
      .forDateRange(hourago);
}

Any advice?

Hiroyuki Miyauchi (AdWords Scripts Team)

unread,
May 14, 2018, 2:05:19 AM5/14/18
to AdWords Scripts Forum
Hello Viral,

For the KeywordSelector, your understanding is correct. Supported values are only predefined date ranges. However, for the Campaigns and Ad Groups, you may filter by HourOfDay which is available for Campaign Performance Report and Adgroup Performance Report. Please note that this is only for the reports, not for CampaignSelector and AdGroupSelector.

Please let me know if you have further clarifications.

Regards,
Hiroyuki
AdWords Scripts Team
Reply all
Reply to author
Forward
0 new messages