{"id":336,"date":"2019-03-12T00:14:12","date_gmt":"2019-03-12T00:14:12","guid":{"rendered":"https:\/\/searchadspro.com\/?p=336"},"modified":"2019-03-29T23:52:58","modified_gmt":"2019-03-29T23:52:58","slug":"google-ads-campaign-budgeting-scripts","status":"publish","type":"post","link":"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/","title":{"rendered":"Google Ads Campaign Budgeting Scripts"},"content":{"rendered":"

[et_pb_section bb_built=”1″][et_pb_row][et_pb_column type=”4_4″][et_pb_text admin_label=”Comment” _builder_version=”3.21″]<\/p>\n

Monthly Budget Enforcer Google Ads script<\/h2>\n

As a Google Ads Manager managing a dozen different Google Ads accounts for local businesses across the country. I find one of most common requirement a local business owner would like to have control over is their monthly spending. Very understandable, as a business owner myself, an unchecked monthly spending can lead us to a business disaster.<\/p>\n

So, on a monthly basis ever since I started working with local and small business owners more than a decade ago. I will always get requests from clients to stop their account as soon as their Google Ads account reaches a certain amount whether it be $500 a month or $5,000 a month. While we can always set the daily budget to an amount equal to their monthly budget divided by 30.5, and allocate the daily budget to several campaigns it’s not always clear cut.<\/p>\n

With the repeated requests from clients, I developed a budgeting script that will enforce monthly budget for an account and also turn it back on if a new month starts. While you can use Google Ads automated rules, the most granular running frequency for automated rules is daily. So if you reach your monthly budget mid day and your automated rules won’t run until 23 hours later, then most likely you’ll be over budget.\u00a0<\/p>\n

And not long after, an angry and unsatisfied client will be giving you a call. Well some are more forgiving but some will immediately fire you.<\/p>\n

This is the Google Ads Campaign Budgeting Script: Monthly Enforcer script I’ve developed. This monthly budget script will allow you to define a monthly max budget threshold and the script will turn off your campaigns automatically. It will also automatically turn the campaigns on when a new month rolls around.<\/p>\n

[\/et_pb_text][et_pb_text admin_label=”Monthly Budget Enforcer Ad Script” _builder_version=”3.21″]<\/p>\n

\/**\r\n *\r\n * NOTICE OF LICENSE\r\n *\r\n * Licensed under the LeadCamp PSL License.\r\n *\r\n * This source file is subject to the LeadCamp PSL License that is\r\n * bundled with this package in the license.txt file.\r\n *\r\n * @name       Monthly Budget Enforcer for a Campaign or Group of Campaigns.\r\n * @desc       This script allows you to set a hard stop for a campaign or group \r\n *             of campaigns if the campaign(s) have reached your budget for the month. \r\n *             Say for example, you have a campaign or a group of campaigns that you want to \r\n *             completely stop running if their total spend has already reached $1000 for the \r\n *             current month. You can use this script to pause and unpause the campaign or group \r\n *             of campaigns automatically.\r\n * @version    101.01\r\n * @author     Harris Lim\r\n * @license    LeadCamp PSL\r\n * @copyright  (c) 2008-Beyond, LeadCamp, Inc\r\n * @link       https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/\r\n *\/\r\n\/\/ this is your monthly budget.\r\nvar monthlyBudget = 1000;\r\n\/\/ assumptions only tag campaigns you want to process with the label inside labelToCheck.\r\nvar labelToCheck = 'Budget'; \/\/default is Budget label\r\n\r\nfunction main() {\r\n  runScript();\r\n}\r\nfunction runScript(){\r\n  createLabelIfNotExists(labelToCheck);\r\n  var campaigns = [];\r\n  var costThisMonth = 0;\r\n  var labelItr = AdsApp.labels().withCondition(\"Name = '\" + labelToCheck + \"'\").get();\r\n  if(labelItr.totalNumEntities()>0){\r\n    var campaignItr = labelItr.next().campaigns().get();\r\n    while(campaignItr.hasNext()){\r\n      var campaign = campaignItr.next();\r\n      campaigns.push(campaign);\r\n      costThisMonth += campaign.getStatsFor(\"THIS_MONTH\").getCost();\r\n    }\r\n    Logger.log(\"Spend this month:\" + costThisMonth);\r\n    if(costThisMonth>monthlyBudget){\r\n      for(i=0;i<campaigns.length;i++){\r\n        if(campaigns[i].isEnabled()){\r\n          campaigns[i].pause();\r\n        }\r\n      }\r\n    }else{\r\n      for(i=0;i<campaigns.length;i++){\r\n        if(!campaigns[i].isEnabled()){\r\n          campaigns[i].enable();\r\n        }\r\n      }\r\n    }\r\n  }else{\r\n    Logger.log(\"Error: The label [\" + labelToCheck + \"] not associated to any campaign.\");\r\n  }\r\n}\r\nfunction createLabelIfNotExists(labelName){\r\n  var labelIterator = AdWordsApp.labels()\r\n        .withCondition('Name CONTAINS \"' + labelName +'\"')\r\n        .get();\r\n  if(labelIterator.totalNumEntities() === 0){\r\n    createALabel(labelName);\r\n    labelIterator = AdWordsApp.labels()\r\n      .withCondition('Name CONTAINS \"' + labelName +'\"')\r\n      .get();    \r\n  }\r\n  Logger.log(labelName)\r\n  \/\/return labelIterator.next();\r\n}\r\nfunction createALabel(theLabel){\r\n  AdWordsApp.createLabel(theLabel,'This label is created by the SearchAdsPro.com Monthly Budget Script ','#F4CCCC');\r\n} \r\n<\/pre>\n

 <\/p>\n

[\/et_pb_text][et_pb_text admin_label=”Explanation” _builder_version=”3.21″]<\/p>\n

Explanation of the Campaign Budgeting Monthly Enforcer Google Ads Script:<\/h3>\n

On line 35 the script will load the label specified on line 26.\u00a0<\/p>\n

var labelItr = AdsApp.labels().withCondition(\"Name = '\" + labelToCheck + \"'\").get();<\/pre>\n

Then the script will load all the campaigns associated to the label on line 37.<\/p>\n

var campaignItr = labelItr.next().campaigns().get();<\/pre>\n

The the script will add up each campaign’s total cost for this month on line 41.<\/p>\n

costThisMonth += campaign.getStatsFor(\"THIS_MONTH\").getCost();<\/pre>\n

Then on line 44, the script will check if the total cost is greater than the budget specified on line 24.<\/p>\n

if(costThisMonth>monthlyBudget){<\/pre>\n

On line 45, If the cost exceeds the monthly budget, it will go through all enabled campaigns and pause them.<\/p>\n

for(i=0;i<campaigns.length;i++){\r\n  if(campaigns[i].isEnabled()){\r\n    campaigns[i].pause();\r\n  }\r\n}<\/pre>\n

But if the cost is below the monthly budget, the opposite will happen, the script will enable all disabled campaigns.<\/p>\n

for(i=0;i<campaigns.length;i++){\r\n  if(!campaigns[i].isEnabled()){\r\n    campaigns[i].enable();\r\n  }\r\n}<\/pre>\n

[\/et_pb_text][et_pb_text admin_label=”Steps to Use” _builder_version=”3.21″]<\/p>\n

Steps to use:<\/h3>\n

Step 1: Copy the script and paste it to your Google Ads Account Script section.<\/p>\n

Step 2: Change line 24 to your own budget – whole number only 2000, 1000, 5000 etc.<\/p>\n

\/\/ this is your monthly budget.\r\nvar monthlyBudget = 2000;\r\n\/\/ assumptions only tag campaigns you want to process with the label inside labelToCheck.\r\nvar labelToCheck = 'Budget'; \/\/default is Budget label\r\n<\/pre>\n

Step 3: Use Budget Label or Change Your Label to something meaningful and label all your campaigns that you want to turn off and on with the label at line 26.<\/p>\n

\/\/ this is your monthly budget.\r\nvar monthlyBudget = 2000;\r\n\/\/ assumptions only tag campaigns you want to process with the label inside labelToCheck.\r\nvar labelToCheck = 'Budget'; \/\/default is Budget label\r\n<\/pre>\n

 <\/p>\n

[\/et_pb_text][et_pb_text admin_label=”Testing and Preview” _builder_version=”3.21″]<\/p>\n

Testing Your Script<\/h3>\n

Step 1: Add the following code after line 29. You would a test amount that is currently below your actual spend and another amount that is above your current spend. This will allow you to test turning off the campaigns as well as turning them back on.<\/p>\n

function main() {\r\n  runScript();\r\n  monthlyBudget = 100; \/\/ a number that's below your actual spend\r\n  runScript();\r\n  monthlyBudget = 1000; \/\/ a number above your actual spend\r\n  runScript();\r\n}<\/pre>\n

Step 2: Be sure you have the campaigns associated to your label defined on line 26.<\/p>\n

[\/et_pb_text][\/et_pb_column][\/et_pb_row][et_pb_row][et_pb_column type=”2_5″][et_pb_text admin_label=”Testing and Preview” _builder_version=”3.21″]<\/p>\n

Step 3: Do a preview of your script<\/p>\n

[\/et_pb_text][\/et_pb_column][et_pb_column type=”3_5″][et_pb_image _builder_version=”3.21″ src=”https:\/\/searchadspro.com\/wp-content\/uploads\/2019\/03\/google-ads-scripts-screen-preview-button.jpg” \/][\/et_pb_column][\/et_pb_row][et_pb_row][et_pb_column type=”2_5″][et_pb_text admin_label=”Testing and Preview” _builder_version=”3.21″]<\/p>\n

Step 4: You should see your campaigns being paused and enabled verifying that your script works.<\/p>\n

 <\/p>\n

[\/et_pb_text][\/et_pb_column][et_pb_column type=”3_5″][et_pb_image _builder_version=”3.21″ src=”https:\/\/searchadspro.com\/wp-content\/uploads\/2019\/03\/google-ads-scripts-preview-screen-results.jpg” \/][\/et_pb_column][\/et_pb_row][et_pb_row][et_pb_column type=”4_4″][et_pb_text _builder_version=”3.21″]<\/p>\n

Step 5: Clean up the code you added and save your Google Ads script.<\/p>\n

[\/et_pb_text][\/et_pb_column][\/et_pb_row][et_pb_row][et_pb_column type=”4_4″][et_pb_text admin_label=”Monthly Budget Enforcer Google Ads Script Variations” _builder_version=”3.21″]<\/p>\n

Monthly Budget Enforcer Google Ads Script Variations<\/h2>\n

[\/et_pb_text][et_pb_text admin_label=”Variation 1: Monthly Budget Enforcer Google Ads Script for Groups of Campaigns” _builder_version=”3.21″]<\/p>\n

Variation 1: Monthly Budget Enforcer Google Ads Script for Groups of Campaigns<\/h3>\n

Now, what if your client is a law firm with 3 different practice areas. And each practice area has it’s own set of campaigns as well as budget? Say they want to allocate $500 towards real estate law, $750 towards personal injury and $250 towards business law. How would you use the above Google Ads Budget Management script to handle this scenario? you can’t here’s an expanded script that will allow you to do that.<\/p>\n

We would need to define a label for each practice area and also specify how much budget we are allocating to each practice area in the script. Below the monthly enforcer script will let you define group of campaigns together and enforce your monthly budget accordingly.<\/p>\n

Here’s the script:<\/p>\n

[\/et_pb_text][et_pb_text admin_label=”Group Code” _builder_version=”3.21″]<\/p>\n

\/**\r\n *\r\n * NOTICE OF LICENSE\r\n *\r\n * Licensed under the LeadCamp PSL License.\r\n *\r\n * This source file is subject to the LeadCamp PSL License that is\r\n * bundled with this package in the license.txt file.\r\n *\r\n * @name       Monthly Budget Enforcer for a Campaign or Group of Campaigns.\r\n * @desc       This script allows you to set a hard stop for a campaign or group \r\n *             of campaigns if the campaign(s) have reached your budget for the month. \r\n *             Say for example, you have a campaign or a group of campaigns that you want to \r\n *             completely stop running if their total spend has already reached $1000 for the \r\n *             current month. You can use this script to pause and unpause the campaign or group \r\n *             of campaigns automatically.\r\n * @version    101.01\r\n * @author     Harris Lim\r\n * @license    LeadCamp PSL\r\n * @copyright  (c) 2008-Beyond, LeadCamp, Inc\r\n * @link       https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/\r\n *\/\r\n\/\/ this is your monthly budget.\r\nvar GroupBudgetDefinition = [\r\n  {GroupLabel:\"Group1\", GroupBudget: 750}\r\n  ,{GroupLabel:\"Group2\", GroupBudget: 500}\r\n  ,{GroupLabel:\"Group3\", GroupBudget: 250}\r\n  ];\r\n\r\nfunction main() {\r\n  for(i=0;i<GroupBudgetDefinition.length;i++){\r\n    runScript(\r\n      GroupBudgetDefinition[i].GroupLabel,\r\n      GroupBudgetDefinition[i].GroupBudget);\r\n  }\r\n}\r\nfunction runScript(labelToCheck,monthlyBudget){\r\n  createLabelIfNotExists(labelToCheck);\r\n  var campaigns = [];\r\n  var costThisMonth = 0;\r\n  var labelItr = AdsApp.labels().withCondition(\"Name = '\" + labelToCheck + \"'\").get();\r\n  if(labelItr.totalNumEntities()>0){  \r\n    var campaignItr = labelItr.next().campaigns().get();\r\n    while(campaignItr.hasNext()){\r\n      var campaign = campaignItr.next();\r\n      campaigns.push(campaign);\r\n      costThisMonth += campaign.getStatsFor(\"THIS_MONTH\").getCost();\r\n    }\r\n    Logger.log(\"Spend this month:\" + costThisMonth);\r\n    if(costThisMonth>monthlyBudget){\r\n      for(i=0;i<campaigns.length;i++){\r\n        if(campaigns[i].isEnabled()){\r\n          campaigns[i].pause();\r\n        }\r\n      }\r\n    }else{\r\n      for(i=0;i<campaigns.length;i++){\r\n        if(!campaigns[i].isEnabled()){\r\n          campaigns[i].enable();\r\n        }\r\n      }\r\n    }\r\n  }\r\n}\r\nfunction createLabelIfNotExists(labelName){\r\n  var labelIterator = AdWordsApp.labels()\r\n        .withCondition('Name CONTAINS \"' + labelName +'\"')\r\n        .get();\r\n  if(labelIterator.totalNumEntities() === 0){\r\n    createALabel(labelName);\r\n    labelIterator = AdWordsApp.labels()\r\n      .withCondition('Name CONTAINS \"' + labelName +'\"')\r\n      .get();    \r\n  }\r\n  Logger.log(labelName)\r\n  \/\/return labelIterator.next();\r\n}\r\nfunction createALabel(theLabel){\r\n  AdWordsApp.createLabel(theLabel,'This label is created by the SearchAdsPro.com Monthly Budget Script ','#F4CCCC');\r\n} \r\n<\/pre>\n

 <\/p>\n

[\/et_pb_text][et_pb_text admin_label=”group label and group budget” _builder_version=”3.21″]<\/p>\n

Below is the code you would need to change for your purposes. On line 25, 26 and 27 you would define a Label to identify a group of campaigns and define the max budget for that group.<\/p>\n

\/\/ this is your monthly budget.\r\nvar GroupBudgetDefinition = [\r\n  {GroupLabel:\"Group1\", GroupBudget: 100}\r\n  ,{GroupLabel:\"Group2\", GroupBudget: 100}\r\n  ,{GroupLabel:\"Group3\", GroupBudget: 250}\r\n  ];<\/pre>\n

You can use the default labels or change it and you need to change the GroupBudget accordingly. And you should be good to go.<\/p>\n

 <\/p>\n

[\/et_pb_text][et_pb_text admin_label=”Variation 2″ _builder_version=”3.21″]<\/p>\n

Variation 2: Running the Monthly Enforcer Script in Your MCC Account<\/h3>\n

Alright, what if you want to run this Google Ads script in your MCC account. Is there a simple modification you can do? Yes, there is a simple one I might add.. you need to add the code below right after your the function main(). Where XXX-XXX-XXXX is your account number.<\/p>\n

var mccAccount = AdsApp.currentAccount();\r\nvar childAccounts = AdsManagerApp.accounts().withIds(['XXX-XXX-XXXX']).get();\r\nvar childAccount = childAccounts.next();\r\nAdsManagerApp.select(childAccount);<\/pre>\n

It should look like this for the original script:<\/p>\n

function main() {\r\n  var mccAccount = AdsApp.currentAccount();\r\n  var childAccounts = AdsManagerApp.accounts().withIds(['XXX-XXX-XXXX']).get();\r\n  var childAccount = childAccounts.next();\r\n  AdsManagerApp.select(childAccount);  \r\n\r\n  runScript();\r\n}<\/pre>\n

It should look like this for Variation 1:<\/p>\n

function main() {\r\n  var mccAccount = AdsApp.currentAccount();\r\n  var childAccounts = AdsManagerApp.accounts().withIds(['XXX-XXX-XXXX']).get();\r\n  var childAccount = childAccounts.next();\r\n  AdsManagerApp.select(childAccount);  \r\n\r\n  for(i=0;i<GroupBudgetDefinition.length;i++){\r\n    runScript(\r\n      GroupBudgetDefinition[i].GroupLabel,\r\n      GroupBudgetDefinition[i].GroupBudget);\r\n  }\r\n}<\/pre>\n

 <\/p>\n

 <\/p>\n

[\/et_pb_text][et_pb_text admin_label=”Variation 3 – Using Shared Budgets” _builder_version=”3.21″]<\/p>\n

Variation 3: Change the Budget without Changing this Ad Script if a Client ask you to change the current month’s budget.<\/h3>\n

This requirement came from one of my clients. They wanted to change the budget almost every month but they didn’t want to update the Monthly Budget Enforcer Google Ads script to avoid making a mistake and breaking the script this is completely a valid concern. This is because most Google Ads managers are not programmers and could easily mess up the code.\u00a0<\/p>\n

I proposed using Shared Budgets for them to enter new budget for the month whether there is a change or not. This shared budget won’t be used in any campaigns but will serve as a place holder for my modified Monthly Budget Enforcer script. Their script had a few modifications that are irrelevant to this post so I had to rewrite it without their customization.<\/p>\n

Here’s the tested Google Ads Script Monthly Budget Enforcer using Shared Budget:<\/p>\n

\/**\r\n *\r\n * NOTICE OF LICENSE\r\n *\r\n * Licensed under the LeadCamp PSL License.\r\n *\r\n * This source file is subject to the LeadCamp PSL License that is\r\n * bundled with this package in the license.txt file.\r\n *\r\n * @name       Monthly Budget Enforcer for a Campaign or Group of Campaigns.\r\n * @desc       This script allows you to set a hard stop for a campaign or group \r\n *             of campaigns if the campaign(s) have reached your budget for the month. \r\n *             Say for example, you have a campaign or a group of campaigns that you want to \r\n *             completely stop running if their total spend has already reached $1000 for the \r\n *             current month. You can use this script to pause and unpause the campaign or group \r\n *             of campaigns automatically.\r\n * @version    101.01\r\n * @author     Harris Lim\r\n * @license    LeadCamp PSL\r\n * @copyright  (c) 2008-Beyond, LeadCamp, Inc\r\n * @link       https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/\r\n *\/\r\n\/\/ this is your monthly budget.\r\nvar GroupBudgetDefinition = [\r\n  {GroupLabel:\"Group1\", GroupBudget: 750}\r\n  ,{GroupLabel:\"Group2\", GroupBudget: 500}\r\n  ,{GroupLabel:\"Group3\", GroupBudget: 250}\r\n  ];\r\n\r\nfunction main() {\r\n  for(i=0;i<GroupBudgetDefinition.length;i++){\r\n    runScript(\r\n      GroupBudgetDefinition[i].GroupLabel,\r\n      GroupBudgetDefinition[i].GroupBudget);\r\n  }\r\n}\r\nfunction runScript(labelToCheck,monthlyBudget){\r\n  createLabelIfNotExists(labelToCheck);\r\n  var campaigns = [];\r\n  var costThisMonth = 0;\r\n \r\n  var budgetSelector = AdsApp\r\n  .budgets()\r\n  .withCondition(\"BudgetName = '\" + labelToCheck + \"'\");\r\n  \r\n  var budgetIterator = budgetSelector.get();\r\n  if(budgetIterator.hasNext()){\r\n    var budget = budgetIterator.next();\r\n    monthlyBudget = budget.getAmount();\r\n  }    \r\n  \r\n  var labelItr = AdsApp.labels().withCondition(\"Name = '\" + labelToCheck + \"'\").get();\r\n  if(labelItr.totalNumEntities()>0){  \r\n    var campaignItr = labelItr.next().campaigns().get();\r\n    while(campaignItr.hasNext()){\r\n      var campaign = campaignItr.next();\r\n      campaigns.push(campaign);\r\n      costThisMonth += campaign.getStatsFor(\"THIS_MONTH\").getCost();\r\n    }\r\n    Logger.log(\"Spend this month:\" + costThisMonth);\r\n    if(costThisMonth>monthlyBudget){\r\n      for(i=0;i<campaigns.length;i++){\r\n        if(campaigns[i].isEnabled()){\r\n          campaigns[i].pause();\r\n        }\r\n      }\r\n    }else{\r\n      for(i=0;i<campaigns.length;i++){\r\n        if(!campaigns[i].isEnabled()){\r\n          campaigns[i].enable();\r\n        }\r\n      }\r\n    }\r\n  }\r\n}\r\nfunction createLabelIfNotExists(labelName){\r\n  var labelIterator = AdWordsApp.labels()\r\n        .withCondition('Name CONTAINS \"' + labelName +'\"')\r\n        .get();\r\n  if(labelIterator.totalNumEntities() === 0){\r\n    createALabel(labelName);\r\n    labelIterator = AdWordsApp.labels()\r\n      .withCondition('Name CONTAINS \"' + labelName +'\"')\r\n      .get();    \r\n  }\r\n  Logger.log(labelName)\r\n  \/\/return labelIterator.next();\r\n}\r\nfunction createALabel(theLabel){\r\n  AdWordsApp.createLabel(theLabel,'This label is created by the SearchAdsPro.com Monthly Budget Script ','#F4CCCC');\r\n} \r\n<\/pre>\n

 <\/p>\n

The above code have a few additional lines wherein it uses a Google Ads Shared Budget’s daily budget as the monthly budget limit. You need to make sure you have a shared budget named similar to your label, give it the max amount you want to spend say $1,500 or something and set that as the “Daily Budget” and you are ready to go. Be sure not use this shared budget anywhere else.<\/p>\n

These are the new lines for the above script:<\/p>\n

var budgetSelector = AdsApp\r\n.budgets()\r\n.withCondition(\"BudgetName = '\" + labelToCheck + \"'\");\r\n\r\nvar budgetIterator = budgetSelector.get();\r\nif(budgetIterator.hasNext()){\r\n  var budget = budgetIterator.next();\r\n  monthlyBudget = budget.getAmount();\r\n} <\/pre>\n

 <\/p>\n

[\/et_pb_text][et_pb_text admin_label=”Variation 4″ _builder_version=”3.21.1″ z_index_tablet=”500″]<\/p>\n

Variation 4: Supporting Shopping and Video Campaigns<\/h3>\n

What if you have shopping campaigns and video campaigns? I tested this since I have clients with shopping campaigns and noticed that the labels.campaigns() function only returns search and display campaigns. To be able to handle this correctly we cannot utilize labels as a way to reach the campaigns but instead use the campaign selectors for each kind of campaign to reach all the campaigns.<\/p>\n

This is the label code that won’t capture shopping and video campaigns.<\/p>\n

var campaignItr = labelItr.next().campaigns().get();<\/pre>\n

This is the replacement code that will handle all 4 campaign types:Search, Display, Shopping and Video campaigns.<\/p>\n

We utilize the withCondition and find campaigns with the label to get to the campaigns.<\/p>\n

var campaignItr = AdsApp\r\n\t.campaigns()\r\n\t.withCondition(\"LabelNames CONTAINS_ANY ['\" + labelToCheck + \"']\")\r\n\t.get();\r\nwhile(campaignItr.hasNext()){\r\n  var campaign = campaignItr.next();\r\n  campaigns.push(campaign);\r\n  var campaignCost = campaign.getStatsFor(\"THIS_MONTH\").getCost();\r\n  costThisMonth += campaignCost;\r\n  Logger.log(campaign.getName() + \":\" + campaignCost + \":\" + costThisMonth);\r\n}\r\nvar shoppingCampaignItr = AdsApp\r\n\t.shoppingCampaigns()\r\n.withCondition(\"LabelNames CONTAINS_ANY ['\" + labelToCheck + \"']\")\r\n.get();\r\nwhile(shoppingCampaignItr.hasNext()){\r\n  var campaign = shoppingCampaignItr.next();\r\n  campaigns.push(campaign);\r\n  var campaignCost = campaign.getStatsFor(\"THIS_MONTH\").getCost();\r\n  costThisMonth += campaignCost;\r\n  Logger.log(campaign.getName() + \":\" + campaignCost + \":\" + costThisMonth);\r\n}  \r\nvar videoCampaignItr = AdsApp\r\n\t.videoCampaigns()\r\n.withCondition(\"LabelNames CONTAINS_ANY ['\" + labelToCheck + \"']\")\r\n.get();\r\nwhile(videoCampaignItr.hasNext()){\r\n  var campaign = videoCampaignItr.next();\r\n  campaigns.push(campaign);\r\n  var campaignCost = campaign.getStatsFor(\"THIS_MONTH\").getCost();\r\n  costThisMonth += campaignCost;\r\n  Logger.log(campaign.getName() + \":\" + campaignCost + \":\" + costThisMonth);\r\n}  \r\n<\/pre>\n

So the full code for this change is below. Do note I added video campaign code in there but since I didn’t test it on an account with video campaign you will need to test this yourself.<\/p>\n

\/**\r\n *\r\n * NOTICE OF LICENSE\r\n *\r\n * Licensed under the LeadCamp PSL License.\r\n *\r\n * This source file is subject to the LeadCamp PSL License that is\r\n * bundled with this package in the license.txt file.\r\n *\r\n * @name       Monthly Budget Enforcer for a Campaign or Group of Campaigns.\r\n * @desc       This script allows you to set a hard stop for a campaign or group \r\n *             of campaigns if the campaign(s) have reached your budget for the month. \r\n *             Say for example, you have a campaign or a group of campaigns that you want to \r\n *             completely stop running if their total spend has already reached $1000 for the \r\n *             current month. You can use this script to pause and unpause the campaign or group \r\n *             of campaigns automatically.\r\n * @version    101.01\r\n * @author     Harris Lim\r\n * @license    LeadCamp PSL\r\n * @copyright  (c) 2008-Beyond, LeadCamp, Inc\r\n * @link       https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/\r\n *\/\r\n\/\/ this is your monthly budget.\r\nvar GroupBudgetDefinition = [\r\n  {GroupLabel:\"MonthlyBudget1\", GroupBudget: 4000}\r\n  ,{GroupLabel:\"MonthlyBudget2\", GroupBudget: 10}\r\n  ,{GroupLabel:\"MonthlyBudget3\", GroupBudget: 10}\r\n  ];\r\n\r\nfunction main() {\r\n  for(i=0;i<GroupBudgetDefinition.length;i++){\r\n    runScript(\r\n      GroupBudgetDefinition[i].GroupLabel,\r\n      GroupBudgetDefinition[i].GroupBudget);\r\n  }\r\n}\r\nfunction runScript(labelToCheck,monthlyBudget){\r\n  createLabelIfNotExists(labelToCheck);\r\n  var campaigns = [];\r\n  var costThisMonth = 0;\r\n \r\n  var budgetSelector = AdsApp\r\n  .budgets()\r\n  .withCondition(\"BudgetName = '\" + labelToCheck + \"'\");\r\n  \r\n  var budgetIterator = budgetSelector.get();\r\n  if(budgetIterator.hasNext()){\r\n    var budget = budgetIterator.next();\r\n    monthlyBudget = budget.getAmount();\r\n  }    \r\n\r\n  var campaignItr = AdsApp\r\n  \t.campaigns()\r\n  \t.withCondition(\"LabelNames CONTAINS_ANY ['\" + labelToCheck + \"']\")\r\n  \t.get();\r\n  while(campaignItr.hasNext()){\r\n    var campaign = campaignItr.next();\r\n    campaigns.push(campaign);\r\n    var campaignCost = campaign.getStatsFor(\"THIS_MONTH\").getCost();\r\n    costThisMonth += campaignCost;\r\n    Logger.log(campaign.getName() + \":\" + campaignCost + \":\" + costThisMonth);\r\n  }\r\n  var shoppingCampaignItr = AdsApp\r\n  \t.shoppingCampaigns()\r\n  .withCondition(\"LabelNames CONTAINS_ANY ['\" + labelToCheck + \"']\")\r\n  .get();\r\n  while(shoppingCampaignItr.hasNext()){\r\n    var campaign = shoppingCampaignItr.next();\r\n    campaigns.push(campaign);\r\n    var campaignCost = campaign.getStatsFor(\"THIS_MONTH\").getCost();\r\n    costThisMonth += campaignCost;\r\n    Logger.log(campaign.getName() + \":\" + campaignCost + \":\" + costThisMonth);\r\n  }  \r\n  var videoCampaignItr = AdsApp\r\n  \t.videoCampaigns()\r\n  .withCondition(\"LabelNames CONTAINS_ANY ['\" + labelToCheck + \"']\")\r\n  .get();\r\n  while(videoCampaignItr.hasNext()){\r\n    var campaign = videoCampaignItr.next();\r\n    campaigns.push(campaign);\r\n    var campaignCost = campaign.getStatsFor(\"THIS_MONTH\").getCost();\r\n    costThisMonth += campaignCost;\r\n    Logger.log(campaign.getName() + \":\" + campaignCost + \":\" + costThisMonth);\r\n  }  \r\n\r\n  Logger.log(\"Spend this month:\" + costThisMonth);\r\n  Logger.log(\"Monthly budget:\" + monthlyBudget);\r\n  if(costThisMonth>monthlyBudget){\r\n    for(i=0;i<campaigns.length;i++){\r\n      if(campaigns[i].isEnabled()){\r\n        campaigns[i].pause();\r\n      }\r\n    }\r\n  }else{\r\n    for(i=0;i<campaigns.length;i++){\r\n      if(!campaigns[i].isEnabled()){\r\n        campaigns[i].enable();\r\n      }\r\n    }\r\n  }\r\n\r\n}\r\nfunction createLabelIfNotExists(labelName){\r\n  var labelIterator = AdWordsApp.labels()\r\n        .withCondition('Name CONTAINS \"' + labelName +'\"')\r\n        .get();\r\n  if(labelIterator.totalNumEntities() === 0){\r\n    createALabel(labelName);\r\n    labelIterator = AdWordsApp.labels()\r\n      .withCondition('Name CONTAINS \"' + labelName +'\"')\r\n      .get();    \r\n  }\r\n  Logger.log(labelName)\r\n  \/\/return labelIterator.next();\r\n}\r\nfunction createALabel(theLabel){\r\n  AdWordsApp.createLabel(theLabel,'This label is created by the SearchAdsPro.com Monthly Budget Script ','#F4CCCC');\r\n} <\/pre>\n

 <\/p>\n

[\/et_pb_text][\/et_pb_column][\/et_pb_row][et_pb_row][et_pb_column type=”4_4″][et_pb_text admin_label=”MCC Multi Account Variation” _builder_version=”3.21″]<\/p>\n

MCC Multi Account Variation : Running the Variation 2 in the MCC to control multiple accounts<\/h3>\n

Key benefits are
1) Control the budget of several groups of campaigns within an account across different accounts in one centralized location. This allows you to save a lot of time managing many accounts, it’s great for Google Ads Agencies managing dozens of accounts just like myself.
2) Option to control the budget of the groups of campaign within an account across different accounts without using the centralized location. This allows you to control the budget of each group of campaign inside the account itself and still allow you to manage other accounts using a centralize location.<\/p>\n

[\/et_pb_text][\/et_pb_column][\/et_pb_row][et_pb_row][et_pb_column type=”1_3″][\/et_pb_column][et_pb_column type=”1_3″][et_pb_button _builder_version=”3.21.1″ button_text=”Request Your Copy” button_url=”https:\/\/docs.google.com\/forms\/d\/1NbaO-0EVgsqge5QsNjrlx6b6spVeSZmIupotlofQ11A\/edit” url_new_window=”on” z_index_tablet=”500″ \/][\/et_pb_column][et_pb_column type=”1_3″][\/et_pb_column][\/et_pb_row][et_pb_row][et_pb_column type=”4_4″][et_pb_text _builder_version=”3.21″]<\/p>\n

Important: When you buy a script or share this page on your social media account. You will help fund more free scripts in the future.<\/strong><\/p>\n

[\/et_pb_text][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n","protected":false},"excerpt":{"rendered":"

As a Google Ads Manager managing a dozen different Google Ads accounts for local businesses across the country. I find one of most common requirement a local business owner would like to have control over is their monthly spending. Very understandable, as a business owner myself, an unchecked monthly spending can lead us to a business disaster.<\/p>\n","protected":false},"author":1,"featured_media":484,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"

\/**\r\n *\r\n * NOTICE OF LICENSE\r\n *\r\n * Licensed under the LeadCamp PSL License.\r\n *\r\n * This source file is subject to the LeadCamp PSL License that is\r\n * bundled with this package in the license.txt file.\r\n *\r\n * @name       Monthly Budget Enforcer for a Campaign or Group of Campaigns.\r\n * @desc       This script allows you to set a hard stop for a campaign or group \r\n *             of campaigns if the campaign(s) have reached your budget for the month. \r\n *             Say for example, you have a campaign or a group of campaigns that you want to \r\n *             completely stop running if their total spend has already reached $1000 for the \r\n *             current month. You can use this script to pause and unpause the campaign or group \r\n *             of campaigns automatically.\r\n * @version    101.01\r\n * @author     Harris Lim\r\n * @license    LeadCamp PSL\r\n * @copyright  (c) 2008-Beyond, LeadCamp, INc\r\n * @link       http:\/\/leadcamp.com\/corporate\/adwords-scripts\/\r\n *\/\r\nfunction main() {\r\n  var currBudget = 1000;\r\n  var labelForGroupToCheck = \"STARTSTOP\";\r\n  var adSpend = 0;\r\n  \r\n  var currentAccount = AdWordsApp.currentAccount();\r\n  Logger.log('Customer ID: ' + currentAccount.getCustomerId() +\r\n      ', Currency Code: ' + currentAccount.getCurrencyCode() +\r\n      ', Timezone: ' + currentAccount.getTimeZone());\r\n  var stats = currentAccount.getStatsFor('THIS_MONTH');\r\n  Logger.log(stats.getCost() + ' cost, ' +\r\n    stats.getImpressions() + ' impressions this month');  \r\n\r\n    var labelIterator = AdWordsApp.labels()\r\n        .withCondition('Name = \"'+labelForGroupToCheck+'\"')\r\n        .get();    \r\n  while (labelIterator.hasNext()) {\r\n    var label = labelIterator.next();\r\n    var campaignIterator = label.campaigns().get();\r\n    \/\/Logger.log(label.getName());\r\n    while (campaignIterator.hasNext()) {\r\n      var campaign = campaignIterator.next();\r\n      adSpend += stats.getCost();\r\n      if(stats.getCost()>currBudget){\r\n        campaign.pause();\r\n        Logger.log(campaign.getName() + ' paused. ');\r\n      }else{\r\n        campaign.enable();\r\n        Logger.log(campaign.getName() + ' unpaused. ');\r\n      }\r\n    }\r\n  } \r\n}<\/pre>

\u00a0<\/p>","_et_gb_content_width":"","footnotes":""},"categories":[7,6],"tags":[5,4],"yoast_head":"\nGoogle Ads Campaign Budgeting Scripts - SearchAdsPro.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Google Ads Campaign Budgeting Scripts\" \/>\n<meta property=\"og:description\" content=\"As a Google Ads Manager managing a dozen different Google Ads accounts for local businesses across the country. I find one of most common requirement a local business owner would like to have control over is their monthly spending. Very understandable, as a business owner myself, an unchecked monthly spending can lead us to a business disaster.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/\" \/>\n<meta property=\"og:site_name\" content=\"SearchAdsPro.com\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-12T00:14:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-29T23:52:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/searchadspro.com\/wp-content\/uploads\/2019\/03\/TestIcon.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"harris\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"harris\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/\",\"url\":\"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/\",\"name\":\"Google Ads Campaign Budgeting Scripts - SearchAdsPro.com\",\"isPartOf\":{\"@id\":\"https:\/\/searchadspro.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/searchadspro.com\/wp-content\/uploads\/2019\/03\/TestIcon.jpg\",\"datePublished\":\"2019-03-12T00:14:12+00:00\",\"dateModified\":\"2019-03-29T23:52:58+00:00\",\"author\":{\"@id\":\"https:\/\/searchadspro.com\/#\/schema\/person\/31110261336bfda861babde5f5627d71\"},\"breadcrumb\":{\"@id\":\"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/#primaryimage\",\"url\":\"https:\/\/searchadspro.com\/wp-content\/uploads\/2019\/03\/TestIcon.jpg\",\"contentUrl\":\"https:\/\/searchadspro.com\/wp-content\/uploads\/2019\/03\/TestIcon.jpg\",\"width\":400,\"height\":400},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/searchadspro.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Google Ads Campaign Budgeting Scripts\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/searchadspro.com\/#website\",\"url\":\"https:\/\/searchadspro.com\/\",\"name\":\"SearchAdsPro.com\",\"description\":\"Google Ads Scripts For You!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/searchadspro.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/searchadspro.com\/#\/schema\/person\/31110261336bfda861babde5f5627d71\",\"name\":\"harris\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/searchadspro.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/eaa09eb6b56be120316e01eec6ac8113?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/eaa09eb6b56be120316e01eec6ac8113?s=96&d=mm&r=g\",\"caption\":\"harris\"},\"description\":\"Harris Lim is a Google Ads Manager since 2005 and have manage hundreds of local business Google Ads account. He currently focuses on Google Ads automation through Google Ads scripts. Find me on facebook https:\/\/www.facebook.com\/harrisyulim\",\"sameAs\":[\"https:\/\/searchadspro.com\"],\"url\":\"https:\/\/searchadspro.com\/author\/harris\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Google Ads Campaign Budgeting Scripts - SearchAdsPro.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/","og_locale":"en_US","og_type":"article","og_title":"Google Ads Campaign Budgeting Scripts","og_description":"As a Google Ads Manager managing a dozen different Google Ads accounts for local businesses across the country. I find one of most common requirement a local business owner would like to have control over is their monthly spending. Very understandable, as a business owner myself, an unchecked monthly spending can lead us to a business disaster.","og_url":"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/","og_site_name":"SearchAdsPro.com","article_published_time":"2019-03-12T00:14:12+00:00","article_modified_time":"2019-03-29T23:52:58+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/searchadspro.com\/wp-content\/uploads\/2019\/03\/TestIcon.jpg","type":"image\/jpeg"}],"author":"harris","twitter_card":"summary_large_image","twitter_misc":{"Written by":"harris","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/","url":"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/","name":"Google Ads Campaign Budgeting Scripts - SearchAdsPro.com","isPartOf":{"@id":"https:\/\/searchadspro.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/#primaryimage"},"image":{"@id":"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/#primaryimage"},"thumbnailUrl":"https:\/\/searchadspro.com\/wp-content\/uploads\/2019\/03\/TestIcon.jpg","datePublished":"2019-03-12T00:14:12+00:00","dateModified":"2019-03-29T23:52:58+00:00","author":{"@id":"https:\/\/searchadspro.com\/#\/schema\/person\/31110261336bfda861babde5f5627d71"},"breadcrumb":{"@id":"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/#primaryimage","url":"https:\/\/searchadspro.com\/wp-content\/uploads\/2019\/03\/TestIcon.jpg","contentUrl":"https:\/\/searchadspro.com\/wp-content\/uploads\/2019\/03\/TestIcon.jpg","width":400,"height":400},{"@type":"BreadcrumbList","@id":"https:\/\/searchadspro.com\/google-ads-campaign-budgeting-scripts\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/searchadspro.com\/"},{"@type":"ListItem","position":2,"name":"Google Ads Campaign Budgeting Scripts"}]},{"@type":"WebSite","@id":"https:\/\/searchadspro.com\/#website","url":"https:\/\/searchadspro.com\/","name":"SearchAdsPro.com","description":"Google Ads Scripts For You!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/searchadspro.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/searchadspro.com\/#\/schema\/person\/31110261336bfda861babde5f5627d71","name":"harris","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/searchadspro.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/eaa09eb6b56be120316e01eec6ac8113?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eaa09eb6b56be120316e01eec6ac8113?s=96&d=mm&r=g","caption":"harris"},"description":"Harris Lim is a Google Ads Manager since 2005 and have manage hundreds of local business Google Ads account. He currently focuses on Google Ads automation through Google Ads scripts. Find me on facebook https:\/\/www.facebook.com\/harrisyulim","sameAs":["https:\/\/searchadspro.com"],"url":"https:\/\/searchadspro.com\/author\/harris\/"}]}},"_links":{"self":[{"href":"https:\/\/searchadspro.com\/wp-json\/wp\/v2\/posts\/336"}],"collection":[{"href":"https:\/\/searchadspro.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/searchadspro.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/searchadspro.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/searchadspro.com\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":70,"href":"https:\/\/searchadspro.com\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":415,"href":"https:\/\/searchadspro.com\/wp-json\/wp\/v2\/posts\/336\/revisions\/415"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/searchadspro.com\/wp-json\/wp\/v2\/media\/484"}],"wp:attachment":[{"href":"https:\/\/searchadspro.com\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/searchadspro.com\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/searchadspro.com\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}