Skip to content

Instantly share code, notes, and snippets.

@derekmartinla
Last active June 2, 2017 14:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save derekmartinla/b719840542406322bb27 to your computer and use it in GitHub Desktop.
Save derekmartinla/b719840542406322bb27 to your computer and use it in GitHub Desktop.
AdWords Countdown Ads Updater Script
/***************************************************************************************
* AdWords Countdown Ad Updater -- Find stale countdown ads and replace them with
* Ads that are updated with new dates.
* Version 1.0
* Created By: Derek Martin
* DerekMartinLA.com or MixedMarketingArtist.com
****************************************************************************************/
var DESCRIPTION2_TEXT = "Sale Ends In"
function main() {
// Set your campaign criteria here
var campIter = AdWordsApp.campaigns().withCondition("Status=ENABLED").withCondition("Name does_not_contain Remarketing").get();
while (campIter.hasNext()) {
var camp = campIter.next();
var agIter = camp.adGroups().withCondition("Status = ENABLED").get();
while (agIter.hasNext()) {
var ag = agIter.next();
var adsIter = ag.ads().withCondition("Status = ENABLED").get();
while (adsIter.hasNext()) {
ad = adsIter.next();
var headline,d1,d2,displayUrl, destUrl;
headline = ad.getHeadline();
d1 = ad.getDescription1();
d2 = ad.getDescription2();
displayUrl = ad.getDisplayUrl();
destUrl = ad.getDestinationUrl();
var regex = /\{=COUNTDOWN\(\"(\d{4})\/(\d{1,2})\/(\d{1,2})/
if (d2.match(regex)) {
match = d2.match(regex);
info(match);
var the_date = new Date(match[1], match[2]-1,match[3])
var today = new Date;
info(today);
if (daysBetween(today, the_date) < 0) {
ad.pause();
var newDate = new Date();
newDate.setDate(newDate.getDate() + 5); // add 5 days
var formattedDate = Utilities.formatDate(newDate, "PST", "yyyy/MM/dd");
var newDest2 = DESCRIPTION2_TEXT + " {\=COUNTDOWN(\"" + formattedDate +" 23:59:00\")}."
status = ag.newTextAdBuilder().withHeadline(headline).withDescription1(d1).withDescription2(newDest2).withDisplayUrl(displayUrl).withDestinationUrl(destUrl).build();
}
}
}
}
}
}
function daysBetween( date1, date2 ) {
//Get 1 day in milliseconds
var one_day=1000*60*60*24;
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = date2_ms - date1_ms;
// Convert back to days and return
return Math.round(difference_ms/one_day);
}
/* HELPER FUNCTIONS */
function warn(msg) {
Logger.log('WARNING: '+msg);
}
function info(msg) {
Logger.log(msg);
}
@nastyaivanova
Copy link

Hello! I tried to use your script but there is one bug and I can't fix it. There is outdated function in Line 53 withDestinationUrl https://developers.google.com/adwords/scripts/docs/sunsets?hl
I tried to change this function to withFinalUrl(finalUrl) also changed this one finalUrl = ad.urls.getFinalUrl() in line 35. But adwords show me this error "TypeError: Cannot find function getFinalUrl in object function () {...}. (line 31)"
Can you help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment