Last active
November 23, 2015 22:32
-
-
Save chipoglesby/4bf66c5272baafda2503 to your computer and use it in GitHub Desktop.
Mcc Level: Pause Google Adwords Campaigns, Ad Groups, Keywords or Ads based on label with date
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
| // Based on Russell Savage's script: http://goo.gl/Y9jEcG | |
| var ENTITY = 'Ad Group'; // Change to Ad or Keyword or Campaign | |
| var PAUSE_PREFIX = "Pause on "; | |
| var ENABLE_PREFIX = "Enable on "; | |
| function main() { | |
| var accountSelector = MccApp.accounts().withLimit(50); | |
| accountSelector.executeInParallel('processAccount', 'allFinished'); | |
| } | |
| function processAccount() { | |
| var todayStr = Utilities.formatDate(new Date(), AdWordsApp.currentAccount().getTimeZone(), "yyyy-MM-dd"); | |
| var pauseStr = PAUSE_PREFIX+todayStr; | |
| var enableStr = ENABLE_PREFIX+todayStr; | |
| Logger.log("Looking for labels: " + [pauseStr,enableStr].join(' and ') + " In Account: " + AdWordsApp.currentAccount().getName()); | |
| var labelsArray = buildLabelArray(pauseStr,enableStr); | |
| if(labelsArray.length > 0) { | |
| var labelsStr = "['" + labelsArray.join("','") + "']"; | |
| var entityIter; | |
| if(ENTITY === 'Keyword') { | |
| entityIter = AdWordsApp.keywords().withCondition("LabelNames CONTAINS_ANY "+labelsStr).get(); | |
| } else if(ENTITY === 'Ad') { | |
| entityIter = AdWordsApp.ads().withCondition("LabelNames CONTAINS_ANY "+labelsStr).get(); | |
| } else if(ENTITY === 'Campaign') { | |
| entityIter = AdWordsApp.campaigns().withCondition("LabelNames CONTAINS_ANY "+labelsStr).get(); | |
| } else if(ENTITY === 'Ad Group') { | |
| entityIter = AdWordsApp.adGroups().withCondition("LabelNames CONTAINS_ANY "+labelsStr).get(); | |
| } else { | |
| throw 'Invaid ENTITY type. Should be Campaign, Keyword or Ad. ENTITY:'+ENTITY; | |
| } | |
| while(entityIter.hasNext()) { | |
| var entity = entityIter.next(); | |
| pauseEntity(entity, pauseStr); | |
| enableEntity(entity, enableStr); | |
| } | |
| } | |
| } | |
| function buildLabelArray(pauseStr,enableStr) { | |
| var labelsArray = []; | |
| try { | |
| var labelIter = AdWordsApp.labels().withCondition("Name IN ['"+pauseStr+"','"+enableStr+"']").get(); | |
| while(labelIter.hasNext()) { | |
| labelsArray.push(labelIter.next().getName()); | |
| } | |
| return labelsArray; | |
| } catch(e) { | |
| Logger.log(e); | |
| } | |
| return []; | |
| } | |
| function pauseEntity(entity, pauseStr) { | |
| var labelIter = entity.labels().withCondition("Name = '"+pauseStr+"'").get(); | |
| if(labelIter.hasNext()) { | |
| entity.pause(); | |
| entity.removeLabel(pauseStr); | |
| } | |
| } | |
| function enableEntity(entity, enableStr) { | |
| var labelIter = entity.labels().withCondition("Name = '"+enableStr+"'").get(); | |
| if(labelIter.hasNext()) { | |
| entity.enable(); | |
| entity.removeLabel(enableStr); | |
| } | |
| } | |
| function allFinished(results) { | |
| for (var i = 0; i < results.length; i++) { | |
| var result = results[i]; | |
| if (result.getStatus() == 'ERROR') { | |
| Logger.log("-- Failed with error: '%s'.", result.getError()); | |
| } else { | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment