E-Mail Kontakt - Tel: 02192 / 91 89 921
    Impressum | Datenschutz

Google Ads Scripts Label-Manager

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.

 LabelManager usage examples

Example script: apply labels, remove labels, delete labels

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");
  }
}

"Changes" report

LabelManager Änderungen

"Logs" report

LabelManager Protokolle

 Notes about LabelManager usage

  • The functions apply, applyOrRemove and remove have got as third parameter _bCheckIfAlreadyExists
    true: Execution is slower but you avoid error messages if you apply a label twice or if you try to remove a label which has not been applied to an element.
    false: The only advantage is that the execution is faster. You may see error messages in the changes report of the script execution but these errors have no effect on the functionality of the script.
  • Please do not use label.remove() or AdWordsApp.createLabel() when you use our LabelManager. The LabelManager has own functions for that which are even more powerful.
  • The statistic functions only work well if you always set _bCheckIfAlreadyExists=true.

 LabelManager Script

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.

Copyright and disclaimer

This Google Ads Scripts lib can be used by anyone at no charge. Use is granted without guarantee or liability. Redistribution of this code or derived code is allowed if you refer with a link to this webpage.