Created
November 13, 2015 18:13
-
-
Save russorat/41bac361994c04a27849 to your computer and use it in GitHub Desktop.
This file contains 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
/*********** | |
* 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 = kw_iter.next(); | |
var labels = kw.labels().get(); | |
var processed_label = false; | |
while(labels.hasNext()) { | |
var label = labels.next(); | |
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 | |
changes_to_make.kw_to_delete.push(kw); | |
} else { | |
// Otherwise, drop the count by 1 day | |
daysLeft--; | |
changes_to_make.labels_to_delete.push(label); | |
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!'); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment