Our LabelManager creates labels automatically if necessary and avoids error messages when you e.g. try to apply a label to an element a second time. There are some functions for comfort and statistic as well.
Our LabelManager makes the use of labels much easier.
function main() { // ## load the LabelManager or add the code manually var url = "http://tools.internet-marketing-inside.de/adwords-scripts-label-manager.js"; eval(UrlFetchApp.fetch(url).getContentText()); // ## LabelManager caches all labels in the account to work efficient. // ## => Don't use label.remove() or AdWordsApp.createLabel() when using the LabelManager. // ## - Instead of label.remove() use LabelManager.removeFromAccount(label.getName()); // ## - Instead of AdWordsApp.createLabel("label name") use LabelManager.create("label name") // ## // ## Examples var campaignIterator = AdWordsApp.campaigns().get(); if (campaignIterator.hasNext() === true) { var campain1 = campaignIterator.next(); // ## add 2 labels to the campaign1. They are created automatically if necessary! LabelManager.apply("LabelManager first", campain1, true); LabelManager.apply("LabelManager second", campain1, true); // ## add label "LabelManager first" again with _bCheckIfAlreadyExists = true // ## => no error in log because LabelManager checks if the element has got this label already LabelManager.apply("LabelManager first", campain1, true); // ## try to remove "LabelManager dummy" again with _bCheckIfAlreadyExists = true // ## => no error in change list because LabelManager checks if the element has got this label already LabelManager.remove("LabelManager dummy", campain1, true); // ## add label "LabelManager third" and remove label "LabelManager first" LabelManager.applyOrRemove({"LabelManager third":true, "LabelManager first":false}, campain1, true); // ## some statistics (only correct if _bCheckIfAlreadyExists was always set true Logger.log("label applied: "+JSON.stringify(LabelManager.getLabelAppliedPerLabel())); Logger.log("label removed: "+JSON.stringify(LabelManager.getLabelRemovedPerLabel())); Logger.log("label applied total: "+LabelManager.getLabelAppliedTotal()+ " (requested applies: "+LabelManager.getLabelApplyRequested()+")"); Logger.log("label removed total: "+LabelManager.getLabelRemovedTotal()+ " (requested removes: "+LabelManager.getLabelRemoveRequested()+")"); // ## Output: // ## label applied: {"LabelManager first":1,"LabelManager second":1,"LabelManager third":1} // ## label removed: {"LabelManager first":1} // ## label applied total: 3 (requested applies: 4) // ## label removed total: 1 (requested removes: 1) // ## add label "LabelManager second" now with _bCheckIfAlreadyExists = false // ## => error in log because LabelManager does NOT check if the element has got this label already // ## Set _bCheckIfAlreadyExists = false only if performance is critical LabelManager.apply("LabelManager second", campain1, false); // ## get a label object LabelManager.getOrCreate("LabelManager second").setDescription("just a test"); // ## remove all created labels from account again LabelManager.removeFromAccount("LabelManager first", "LabelManager second", "LabelManager third"); } }
The LabelManager can be loaded into the Google Ads Scripts code this way
function main() { // ## load the LabelManager this way to get updates automatically var url = "http://tools.internet-marketing-inside.de/adwords-scripts-label-manager.js"; eval(UrlFetchApp.fetch(url).getContentText());
or you insert the code in your script you find at https://tools.internet-marketing-inside.de/adwords-scripts-label-manager.js.
The disadvantage of this method is that updates are not automatically installed when we update the code. Google likes to change the API, which is why scripts suddenly may not work anymore.