/* Name: Auto Scheduler Description: This script allows you to pause or enable four different adwords entities: campaigns, ads, adgroups or keywords based on a schedule you define. It utilizes the current day of the month to decide whether your entity should be paused or enabled. It will respect if you pause the entities manually or if other scripts have paused your entities. The script use labels to tag what you'd like to pause or enable on a schedule. Written by Harris Lim, LeadCamp, Inc Version: 1.0.0 Url: http://leadcamp.com/corporate/adwords-scripts/ */ /* Do not change this section. Entities we support in this script.*/ var ADS = 0; var KEYWORDS = 1; var CAMPAIGNS = 2; var ADGROUPS = 3; /* end of do not chance this section */ /* change logIt to true or false if you want to log the progress of the script */ var logIt = true; function main(){ /* entittiesToProcess indicates which entity the script will process, this script supports 4 entities. */ var entitiesToProcess = [ADS,KEYWORDS,CAMPAIGNS,ADGROUPS]; /* labelToPause indicates which entity to pause, be sure to add this label to your adwords account */ var labelToPause = "AutoScheduler"; /* labelToApplyIfPaused indicates which is currently paused by this script, be sure to add this label to your adwords account */ var labelToApplyIfPaused = "AutoSchedulerPause"; /* sheetName indicates which sheet we are going to get schedule from */ var sheetName = "monthlyschedule"; /* this is the spreadsheet were we'll configure our schedule. */ var ssUrl = "[GoogleDriveSpreadsheetURLHere]"; /* do not change anything under this line, unless you know what you are doing */ var sheet = SpreadsheetApp.openByUrl(ssUrl).getSheetByName(sheetName); var pauseNow = sheet.getRange(sheet.getRange("B2").getCell(1,1).getValue()).getCell(new Date().getDate(),2).getValue()=='Off'; logIt?Logger.log("PauseNow = " + pauseNow):a=1; var labelIterator = ''; if(pauseNow){ labelIterator = AdWordsApp.labels() .withCondition('Name = "' + labelToPause + '"') .get(); }else{ labelIterator = AdWordsApp.labels() .withCondition('Name = "' + labelToApplyIfPaused + '"') .get(); } logIt?Logger.log("labelIterator = " + labelIterator.totalNumEntities()):a=1; while (labelIterator.hasNext()) { var label = labelIterator.next(); var itr = ''; for(entity in entitiesToProcess){ itr = extractEntityItr(entitiesToProcess[entity],label); while (itr.hasNext()) { var itrEntity = itr.next(); if(pauseNow){ pauseEntity(itrEntity,labelToApplyIfPaused); }else{ enableEntity(itrEntity,labelToApplyIfPaused); } } } } } function extractEntityItr(entityType, objLabel){ switch(entityType){ case ADS: logIt?Logger.log("Processing Ads..."):a=1; return objLabel.ads().get(); break; case KEYWORDS: logIt?Logger.log("Processing Keywords..."):a=1; return objLabel.keywords().get(); break; case CAMPAIGNS: logIt?Logger.log("Processing Campaigns..."):a=1; return objLabel.campaigns().get(); break; case ADGROUPS: logIt?Logger.log("Processing Adgroups..."):a=1; return objLabel.adGroups().get(); break; } } function pauseEntity(obj, labelStr){ if(obj.isEnabled()){ if(logIt){ var helpTxt = ''; if(obj.getEntityType() == 'Ad'){ helpTxt = obj.getId() + ":" + obj.getHeadline(); }else if(obj.getEntityType() == 'Keyword'){ helpTxt = obj.getText(); }else{ helpTxt = obj.getName(); } Logger.log("Pausing and applying label to " + obj.getEntityType() + " " + helpTxt); } obj.pause(); obj.applyLabel(labelStr); } } function enableEntity(obj, labelStr){ if(obj.isPaused()){ if(logIt){ var helpTxt = ''; if(obj.getEntityType() == 'Ad'){ helpTxt = obj.getId() + ":" + obj.getHeadline(); }else if(obj.getEntityType() == 'Keyword'){ helpTxt = obj.getText(); }else{ helpTxt = obj.getName(); } Logger.log("Enabling and removing label from " + obj.getEntityType() + " " + helpTxt); } obj.enable(); obj.removeLabel(labelStr); } } //logIt?Logger.log():a=1;