By: Joseph Martinez

Does your client have sales or deals that are only available for a limited time? Let the world know with the countdown script. AdWords’ countdown script is a call to action that lets the user know how much time they have left to get in on that offer. Here’s an example…

adwords countdown script

You can see that any user who finds this Harley-Davidson dealership’s ad has 10 days and 12 hours left to get their boots at 15% off. Can you think of a few clients who could use this? Of course you can so here’s how to set it up.

1. Log into AdWords and Find the Scripts Section

Once in AdWords, click on the Automate button, then choose Create and manage scripts option.

create adwords scripts

2. Create and Edit the Script

There will be a red + SCRIPT button that you can’t miss. After you click it: name your script, authorize your script and remove the small bit of code they already have in place. Then add the following code below…

// Date to use to find out how many days are remaining.
var END_DATE = new Date('December 21, 2012');
// Change this to the Ad Group you set up with text ads with AdParams.
var AD_GROUP_NAME = 'Ad Group';

function main() {
  var timeLeft = calculateTimeLeftUntil(END_DATE);
  var adGroup = getAdGroup(AD_GROUP_NAME);
  var keywords = adGroup.keywords().get();
  while (keywords.hasNext()) {
    var 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 getAdGroup(name) {
  var adGroupIterator = AdWordsApp.adGroups()
      .withCondition('Name = "' + name + '"')
      .withLimit(1)
      .get();
  if (adGroupIterator.hasNext()) {
    return adGroupIterator.next();
  }
}

The two items in blue are the main components you’ll need to change. The first is the date on which your sale ENDS. AdWords needs to know which date to countdown to. Second is the ad group on which the sale will apply. You can add multiple ad groups if you want, but you’ll have to use a different script shown below.

// Date to use to find out how many days are remaining.
var END_DATE = new Date('December 21, 2012');
// Change this to the Ad Group you set up with text ads with AdParams.
var AD_GROUP_NAMES = ['Ad Group 1', 'Ad Group 2', 'Ad Group3'];
var DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;

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']);
 }
}

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']));

 // Prevent countdown to negative time period.
 if (timeLeft['days'] < 0) {
 timeLeft['days'] = 0;
 }
 if (timeLeft['hours'] < 0) {
 timeLeft['hours'] = 0;
 }

 return timeLeft;
}

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

Be sure to Run the Script to save it. You do get options to preview it first or run without previewing. I highly recommend previewing the script to get full conformation that it is working properly.

3. Run Your Script and Set a Schedule

The beauty of the countdown script is that it automatically updates itself, but if you don’t create this schedule your time will be frozen from the time the script was activated. Make sure you run the script and schedule how often you want it to update.

adwords script scheduling

4. Create Ads that Reference the Script

I recreated the ad we originally saw in this blog. Here’s what it should look like…

countdown script ad

The two parameters you see in the second description line reference the two keyword.setAdParam lines you saw in the source code in step two. (Note: The default text referenced in each parameter can be different, but this default reference should work.)

And that’s it. Enable your ads and use the ad preview tool to confirm the script is working just right. Last, make sure the landing page you’re sending users to confirms the sale; otherwise your efforts might go wasted.