Forked from derekmartinla/gist:b719840542406322bb27
Last active
August 29, 2015 14:18
-
-
Save underdown/cedb7e52f4df655d2819 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************************************************** | |
* 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment