Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/***********
* This is the function that will run in each account. We
* can leverage all the functions we wrote earlier to make
* this as short as possible.
***********/
function findKeywordsToDelete(optionalInput) {
// We are sending over a set of configs from the main
// function. Parse that config, check to see if there is
// an override for this account, or use the default.
var all_configs = JSON.parse(optionalInput);
var cust_id = AdWordsApp.currentAccount().getCustomerId();
var config_options = (all_configs[cust_id]) ? all_configs[cust_id] : all_configs['default'];
// Create our labels
createLabelsIfNeeded(config_options.days_in_limbo);
// Find the keywords old enough to be considered and then
// the keywords that we consider duds.
var keywords_old_enough = getKeywordsActiveDaysAgo(config_options.days_ago);
var duds = getDuds(config_options,keywords_old_enough);
// Let's get our object ready to store the changes we need to make
var changes_to_make = {
kw_to_delete: [],
labels_to_delete: [],
labels_to_add: []
};
while(duds.length > 0) {
// withIds can only handle 10000 records at a time.
// This will chop the duds array into 10000 record chunks.
// If you don't do this, you will get a strange error when you
// try iterating through the list.
var duds_chunk = duds.splice(0,10000);
findChangesToMake(duds_chunk,changes_to_make,config_options.days_in_limbo);
// Similar to before. If we are running short on time, leave early to
// allow the script to process the changes.
if(AdWordsApp.getExecutionInfo().getRemainingTime() < 120) {
Logger.log('Leaving early!');
break;
}
}
// Apply all the changes we made
applyChanges(changes_to_make);
// Generate some summary data so that we have something
// to collect for the email.
var summaryData = generateSummaryData(changes_to_make);
return JSON.stringify(summaryData)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment