广告组错误的AdWords倒计时脚本

时间:2013-02-15 13:11:33

标签: google-adwords

我正在尝试从https://developers.google.com/adwords/scripts/docs/tutorials/countdown-sale

实施倒计时脚本

该脚本适用于单个广告组,但在尝试多个广告组时,我不断收到以下错误:

TypeError:无法调用未定义的方法“setAdParam”。 (第9行)

我的完整脚本是:

// Date to use to find out how many days are remaining.
var END_DATE = new Date('February 24, 2013');
// Change this to the Ad Group you set up with text ads with AdParams.
var AD_GROUP_NAMES = ['AdGroup1', AdGroup2'];

function main() {
  var timeLeft = calculateTimeLeftUntil(END_DATE);
  var adGroups = getAdGroups(AD_GROUP_NAMES);

  while (adGroups.hasNext()) {
  var adGroup = adGroups.next();
  var keywords = adGroup.keywords().get();
    // We want to update {param1} to use our calculated days and {param2} for hours.
    keywords.setAdParam(1, timeLeft['days']); // HERE COMES THE ERROR
    keywords.setAdParam(2, timeLeft['hours']); 
  }
}

var DAY_IN_MILLISECONDS = 1000*60*60*24;

function calculateTimeLeftUntil(end) {
  var current = new Date();
  var timeLeft = {};
  var daysFloat = (end - current) / (DAY_IN_MILLISECONDS);
  timeLeft['days'] = Math.floor(daysFloat);
  timeLeft['hours'] = Math.floor(24 * (daysFloat - timeLeft['days']));
  return timeLeft;
}

function getAdGroups(names) {
  var predicateValues = "['" + names.join("','") + "']";
  Logger.log(predicateValues);
  return AdWordsApp.adGroups()
      .withCondition('Name IN ' + predicateValues)
      .withLimit(names.length)
      .get();
}

1 个答案:

答案 0 :(得分:0)

问题在于您尝试将广告参数设置为关键字迭代器,而不是单个关键字。

而不是这个

var keywords = adGroup.keywords().get();
// We want to update {param1} to use our calculated days and {param2} for hours.
keywords.setAdParam(1, timeLeft['days']); // HERE COMES THE ERROR

试试这个:

var keywords = adGroup.keywords().get();
while (keywords.hasNext()) {
  var keyword = keywords.next();
  // and now
  keyword.setAdParam(...);
}