Stop Empty AdGroups without Ads or Keywords

Easy Script for Stop AdGroups. Find empty active adgroups in active campaigns and stop them.

/* 
 * Stop Empty AdGroups without Ads or Keywords
 * Source: https://www.en.advertisercommunity.com/t5/Basics-for-New-Advertisers/How-to-Pause-Empty-Ad-Groups/td-p/452772        
 */

function main() {
    var adGroupIterator = AdWordsApp.adGroups()
        .withCondition("CampaignStatus = 'ENABLED'")
        .withCondition("Status = 'ENABLED'")
        .get();

    while (adGroupIterator.hasNext()) {
        var thisGroup = adGroupIterator.next();
        var numAds = thisGroup.ads()
            .withCondition("Status = 'ENABLED'")
            .get().totalNumEntities();
            
        var numKeywords = thisGroup.keywords()
            .withCondition("Status = 'ENABLED'")
            .get().totalNumEntities();
            
        if (numAds == 0 || numKeywords == 0) {
            thisGroup.pause();
        }
    }
}

Function createLabelIfNeeded

/* 
 * Function createLabelIfNeeded
 * Source: http://www.freeadwordsscripts.com/
 */

function main() { 
  createLabelIfNeeded("Test", "#ffffff");
}
function createLabelIfNeeded(name,color) {
  if(!AdWordsApp.labels().withCondition("Name = '"+name+"'").get().hasNext()) {
    Logger.log('Creating label: "'+name+'"');
    AdWordsApp.createLabel(name,"",color);
  } else {
    Logger.log('Label: "'+name+'" already exists.');
  }
}

Function myLog

Instead of Logger.log you can use in test mode custom help function myLog(). After test Google Ads Script you need to only change value of variable writeLog.

/* 
 * Function myLog
 * Source: https://gscripts.eu/mylog/
 */

var writeLog = 1; // if not test mode, use 0 or something else
function main() { 
  myLog("Lorem Ipsum", writeLog);
}
function myLog(text, number){
   if (number == 1) {
    Logger.log(text);
   }
}

Remove Unused Labels in Shared Library

/* 
 * Remove Unused Labels in Shared Library
 * 
 * Source: https://groups.google.com/d/msg/adwords-scripts/pD8MXs1FZFM/uzrBr0iQCQAJ
 */

function main() {
    var labelIterator = AdWordsApp.labels()
        .withCondition("CampaignsCount = '0'")
        .withCondition("AdGroupsCount = '0'")
        .withCondition("KeywordsCount = '0'")
        .withCondition("AdsCount = '0'")
        .get();
    while (labelIterator.hasNext()) {
        var label = labelIterator.next();
        var labelName = label.getName();
        label.remove();
        Logger.log("Label " + labelName + " removed");
    }
}