Hello,
For one of my accounts there are multiple locations inside the account. Each location has 4 campaigns. These campaigns are named based off the location. I am wondering if there is a script to pause these 4 campaigns once the total cost for all of these campaigns combined equals a monthly budget.
For example, the Dallas location has 4 campaigns. Each of these campaigns spend a different amount of money throughout the month but once they reach a combined total budget, I want to automatically turn these 4 campaigns off.
Campaign-Dallas 1
Campaign-Dallas 2
Campaign-Dallas 3
Campaign-Dallas 4
Pause all 4 campaigns automatically once they spend a combined total of $1,000 for the month.
I do not want to use shared budgets because you can only set a daily budget for the campaigns. I want to set a monthly budget.
I do not want to use the automated rules because this only applies to the individual campaign total spend and does not combined the total for all 4 campaigns.
I was wondering if anyone has created a script for this?
Thank you,
function main() {
//update the value of monthlyBudget based on your requirement
var monthlyBudget = 1000;
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"]')
.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 4 campaigns is equal to defined monthlyBudget, pause the 4 campaigns
if (totalCost == monthlyBudget){
for (var i = 0; i < campaignsList.length; i++) {
var campaign = campaignsList[i];
Logger.log(campaign.getName())
campaign.pause();
}
}
}
function main() {
//update the value of monthlyBudget based on your requirement
var monthlyBudget = 100;
var totalCost = 0;
var campaignsList = [];
var campaignIterator = AdWordsApp.campaigns()
.withCondition('Name IN ["INSERT_CAMPAIGN_NAMES_HERE"]') //you may change this depending on your requirement
.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;
}
Logger.log("Combined Cost for this month: " +totalCost);
//if totalCost of combined 4 campaigns is equal to defined monthlyBudget, pause the 4 campaigns
if (totalCost == monthlyBudget){
for (var i = 0; i < campaignsList.length; i++) {
var campaign = campaignsList[i];
Logger.log(campaign.getName())
campaign.pause();
}
}
}
--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to a topic in the Google Groups "AdWords Scripts Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/adwords-scripts/UF_YditdtL8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adwords-scripts+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/fce09bc0-8713-4839-a776-2c8c8caffce6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
if (totalCost == monthlyBudget){ //line 27
if (totalCost >= monthlyBudget){ //line 27
var campaignIterator = AdWordsApp.campaigns()
var campaignIterator = AdWordsApp.videoCampaigns()
function main() {
/***
This script will auto-pause campaigns when the account exceeds its monthly budget.
No more daily monitoring of account budgets!
***/
Logger.log("**********************");
Logger.log("Running BUDGET - Pause All Campaigns script...");
Logger.log("**********************");
// THIS AMOUNT WILL VARY CLIENT BY CLIENT
// MAKE SURE IT IS CORRECT
var monthlyBudget = 500;
var totalCost = 0;
var campaignsList = [];
var campaignIterator = AdWordsApp.campaigns().get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
// Save each current campaign into an array for ease of iteration
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;
}
Logger.log("Account cost for this month: " + totalCost);
// If totalCost of combined campaigns exceeds defined monthlyBudget, pause all!
if (totalCost >= monthlyBudget){
Logger.log("Monthly budget met or exceeded. Auto-pausing all campaigns!");
for (var i = 0; i < campaignsList.length; i++) {
var campaign = campaignsList[i];
Logger.log("Pausing campaign: " + campaign.getName());
campaign.pause();
}
} else {
Logger.log("Total monthly cost currently under budget.");
Logger.log("Monthly Cost: " + totalCost);
Logger.log("Monthly Budget: " + monthlyBudget);
}
}
Also if I want it to be label = x
Thanks
var date = new Date();
var day = date.getDay();
//Sunday = 0, Monday = 1, Tuesday = 2, etc
if(day == 0){
//rest of code
}
--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scripts+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/1a5dd642-7b91-4354-b3d4-e96d0707a34b%40googlegroups.com.
Hi Antonio,
You may use getStartDate() and getEndDate() method to get a campaign's period. With regard to budget, could you confirm what you meant by this "define Max Budget so it can pause the campaign with ID X but for the defined period."? If you meant that setting the amount of budget, you may use setAmount() method. Otherwise, so I can provide proper suggestion, could you provide further details?
Regards,
Hiroyuki
Google Ads Scripts Team
Hi Antonio,
> now, I need a variation os this do apply to campaigns that run not on a montly base, but on a start and close date.
Could you confirm if what you want to achieve is to check the spending (cost) for only the period between campaign's start date and end date? If yes, you may get start and end date by getStartDate() and getEndDate(), then use getStatsFor(dateFrom, dateTo) to stats for the specified custom date range.
> Yes, i need to set amount (fixed) for a determined timeframe.
Could you confirm if what you meant is to set amount everyday during the period between the start date and end date?
ReferenceError: "spreadsheet" is not defined. (file Code.gs, line 903) |
Hi Antonio,
Thank you for your confirmation.
You may refer to the sample script below.
Getting the total cost only for campaign period------------------------------
var campaignIterator = AdWordsApp.campaigns()
.withCondition("Name CONTAINS_IGNORE_CASE 'Tobedefined'")
.withIds([ID1,ID2, ])
.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(campaign.getStartDate(), campaign.getEndDate());
var campaignCost = stats.getCost();
totalCost += campaignCost;
}
------------------------------------------------------------------------------------------
With regard to set amount of budget, you may refer to this sample.
Please let me know if you have further clarifications.
Regards,
Hiroyuki
Google Ads Scripts Team
Hi Divya,
Could you confirm if the email address which authorized the script has access to the spreadsheet? If no, you would need to give an access to the email address.
Since this is an old thread and the topic here is about managing campaigns by total monthly budget, could you create a new thread with the details for better tracking?
Regards,
Hiroyuki
Google Ads Scripts Team