Pause AdGroups With No Active ADs
/*********************************************
* Pause AdGroups With No Active ADs
* Version 1.1
* Changelog v1.1
* - Updated for speed and added comments
* Created By: Russ Savage
* FreeAdWordsScripts.com
**********************************************/
function main() {
// Let's start by getting all of the active AdGroups
var agIter = AdWordsApp.adGroups()
.withCondition('CampaignStatus = ENABLED')
.withCondition('Status = ENABLED')
.get();
// It is faster to store them and process them all at once later
var toPause = [];
// Then we will go through each one
while(agIter.hasNext()) {
var ag = agIter.next();
//get all the ad copies that are enabled
var adIter = ag.ads()
.withCondition("Status = ENABLED")
.get();
//If .hasNext() is true, there is at least 1 ad copy in the AdGroup
var hasAd = adIter.hasNext();
if(!hasAd) {
toPause.push(ag);
}
}
// Now we process them all at once to take advantage of batch processing
for(var i in toPause) {
toPause[i].pause();
}
}
* Pause AdGroups With No Active ADs
* Version 1.1
* Changelog v1.1
* - Updated for speed and added comments
* Created By: Russ Savage
* FreeAdWordsScripts.com
**********************************************/
function main() {
// Let's start by getting all of the active AdGroups
var agIter = AdWordsApp.adGroups()
.withCondition('CampaignStatus = ENABLED')
.withCondition('Status = ENABLED')
.get();
// It is faster to store them and process them all at once later
var toPause = [];
// Then we will go through each one
while(agIter.hasNext()) {
var ag = agIter.next();
//get all the ad copies that are enabled
var adIter = ag.ads()
.withCondition("Status = ENABLED")
.get();
//If .hasNext() is true, there is at least 1 ad copy in the AdGroup
var hasAd = adIter.hasNext();
if(!hasAd) {
toPause.push(ag);
}
}
// Now we process them all at once to take advantage of batch processing
for(var i in toPause) {
toPause[i].pause();
}
}
Monthly Budget Cut off
function main() {
//update the value of monthlyBudget based on your requirement
var monthlyBudget = ####;
var totalCost = 0;
var campaignsList = [];
var campaignIterator = AdWordsApp.campaigns()
.withCondition('Name IN ["INSERT_CAMPAIGN_NAME_HERE", "INSERT_CAMPAIGN_NAME_HERE", "INSERT_CAMPAIGN_NAME_HERE", "INSERT_CAMPAIGN_NAME_HERE"]') //add 1 or more campaigns that are needed to be included the check
.get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
//save in campaignsList the list of campaigns object.
campaignsList.push(campaign);
//use THIS_MONTH to get data for all days in the current month
var stats = campaign.getStatsFor('THIS_MONTH');
var campaignCost = stats.getCost();
totalCost += campaignCost;
}
//if totalCost of combined campaigns is equal to defined monthlyBudget, pause the listed campaigns
if (totalCost > monthlyBudget){
for (var i = 0; i < campaignsList.length; i++) {
var campaign = campaignsList[i];
Logger.log(campaign.getName())
campaign.pause();
}
}
}
//update the value of monthlyBudget based on your requirement
var monthlyBudget = ####;
var totalCost = 0;
var campaignsList = [];
var campaignIterator = AdWordsApp.campaigns()
.withCondition('Name IN ["INSERT_CAMPAIGN_NAME_HERE", "INSERT_CAMPAIGN_NAME_HERE", "INSERT_CAMPAIGN_NAME_HERE", "INSERT_CAMPAIGN_NAME_HERE"]') //add 1 or more campaigns that are needed to be included the check
.get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
//save in campaignsList the list of campaigns object.
campaignsList.push(campaign);
//use THIS_MONTH to get data for all days in the current month
var stats = campaign.getStatsFor('THIS_MONTH');
var campaignCost = stats.getCost();
totalCost += campaignCost;
}
//if totalCost of combined campaigns is equal to defined monthlyBudget, pause the listed campaigns
if (totalCost > monthlyBudget){
for (var i = 0; i < campaignsList.length; i++) {
var campaign = campaignsList[i];
Logger.log(campaign.getName())
campaign.pause();
}
}
}