Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
* Given a set of Ids, an object to store updates in, and the
* max number of days a keyword can be in limbo, this function
* iterates through your account and gathers the changes to be
* made. It also contains the logic to ignore keywords with the
* label "Save" on them. All changes are stored in the arrays within
* the object changes_to_make.
function findChangesToMake(duds,changes_to_make,max_days_in_limbo) {
// This is the label applied to "Save" a keyword
var SAVE_LABEL_TEXT = 'Save';
// This is the label format applied to keywords in limbo.
var LABEL_REGEXP = /Deleting in (\d+) days/g;
var kw_iter = AdWordsApp.keywords().withIds(duds).get();
while(kw_iter.hasNext()) {
var kw =;
var labels = kw.labels().get();
var processed_label = false;
while(labels.hasNext()) {
var label =;
var label_text = label.getName();
if(label_text == SAVE_LABEL_TEXT) {
processed_label = true;
} else if(LABEL_REGEXP.test(label_text)) {
// This means the keyword was previously in limbo
processed_label = true;
var match = label_text.match(/\d+/g);
if(match) {
// This pulls the number of days from the label
var daysLeft = parseInt(match[0]);
if(daysLeft == 1) {
// If it was the last day, delete it
} else {
// Otherwise, drop the count by 1 day
changes_to_make.labels_to_add.push({kw:kw,label:'Deleting in '+daysLeft+' days'});
} else {
throw 'Was not able to extract remaining days from label: '+label_text;
if(processed_label) { break; }
if(!processed_label) {
changes_to_make.labels_to_add.push({kw:kw,label:'Deleting in '+max_days_in_limbo+' days'});
// Sometimes these things can run long on large accounts.
// Break early if we are running out of time so we have some
// time to apply the changes. If you still run out of time, try
// increasing the value here (in seconds).
if(AdWordsApp.getExecutionInfo().getRemainingTime() < 120) {
Logger.log('Leaving early!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment