AdWords
5.6K members online now
5.6K members online now
Use AdWords conversion tracking and reporting to measure your results. Have a question about Google Analytics? Post it here, on the Google Analytics Community!
Guide Me
star_border
Reply

AdWords Countdown Script for AdGroups Error

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi,

 

I'm trying to implement the countdown script from https://developers.google.com/adwords/scripts/docs/tutorials/countdown-sale

 

The script works for single Ad Group but when trying to Multiple ad groups part of I keep getting following error:

TypeError: Cannot call method "setAdParam" of undefined. (line 9)

 

My full script is:

// 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();
}

 

Anybody?

 

Thanks!

- Mark

 

1 Expert replyverified_user
Marked as Best Answer.
Solution
Accepted by topic author Mark_Pz
September 2015

Re: AdWords Countdown Script for AdGroups Error

[ Edited ]
Top Contributor
# 2
Top Contributor

Hi Mark and welcome to the forum.

 

You're trying to address a set of Keywords when the setAdParam only works on individual elements.  You need to loop with a hasNext() for the keywords as well.

 

Try this for your main function:

 

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();

    while (keywords.hasNext()) {

      keyword=keywords.next();

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

  }
}

 

I haven't tested this so I can't gurantee it'll work, but I'm pretty sure that's the issue.

 

Jon

 

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: AdWords Countdown Script for AdGroups Error

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
Thank you! That solved it.

Re: AdWords Countdown Script for AdGroups Error

Top Contributor Alumni
# 4
Top Contributor Alumni

Hi 

Perfect answer from Jon,

I would also suggest that you should look for timezone diffrence while adjusting countdown. 

I am at GMT +5:50 and when i try to run countdown i always get 13 hrs time less than expected. 

To fix this I used following code 

Spoiler

var DAY_IN_MILLISECONDS = 1000*60*60*24;
var Set_time = 1000*60*60*13;

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

 

 

Hope this helps

 

Regards, Nik
LinkedIn  |   @nikhilparachure | My Blog

Re: AdWords Countdown Script for AdGroups Error

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭
Hi Jon

The update provided removes the error from the line which is great, but when I view the ads with the updated script running, it is only giving days and hours in my first ad group ad copy, the others only show the fall back "Only a few days and hours left!" it is not referncing the days and hours? any thoughts?

// Date to use to find out how many days are remaining.
var END_DATE = new Date('July 16, 2014');
// Change this to the Ad Group you set up with text ads with AdParams.
var AD_GROUP_NAMES = ['adgroup 1', 'adgroup 2', 'adgroup 3'];

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();
while (keywords.hasNext()) {
keyword=keywords.next();
// We want to update {param1} to use our calculated days and {param2} for hours.
keyword.setAdParam(1, timeLeft['days']);
keyword.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();
}


thanks in advance

Carl.