AdWords
2.1K members online now
2.1K members online now
Dive into advanced features like Remarketing, Flexible Bid Strategies, AdWords Editor, and AdWords Scripts
Guide Me
star_border
Reply

Shared Budget Script - Using Labels

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi All, 

 
I am looking for a script that will allow me to manage shared budgets by selecting campaigns using labels.
(as the only way in Adwords is to manually select each campaign when creating a shared budget)
 
the theory would be to cycle through all campaigns, find there label, then assign a daily shared budget based upon its label
 
eg: 
Label = Level One - has a £100 p/d shared budget
Label = Level Two - has a £60 p/d shared budget
etc... (up to a max of 10 label groups)
 
 
it would also be great if i could set the start/end of month days and a total monthly budget within the script too so it doesn't go over, and manage it through google sheets.
 
Hope someone can help with a script or a decent chunk i can start to work on (however my coding is minimal so the more help the better please) 
 
 
Thanks Smiley Very Happy
1 Expert replyverified_user
Marked as Best Answer.
Solution
Accepted by topic author Richard H
September 2015

Re: Shared Budget Script - Using Labels

Top Contributor
# 2
Top Contributor

Hi @Richard H I have just the thing (see end of message)!

 

You'll need to create a Label called FlexBudget which should be applied to all the Campaigns you want to be affected by the script.  You then need to create Labels for each Campaign that are named as a percentage of their share of the budget.  So, if you had two Campaigns and wanted one to have a 60% share, you'd Label that Campaign 60 and the other 40.  No % signs, no spaces, just the number.  Obviously, you'll need to make sure all these Labels add up to 100 or you'll get over or underspend.

 

The target budget for the month is at the top.  Note that this is the amount the script will try to spend this month, so if you start running it now (26th) it'll try and spend the whole amount in the next 5 days so if you start the script mid-month you'll need to work out what the spend should be in the remaining days, then set it back to the full amount on the 1st, after that you can leave it to run.

 

The script not only apportions budgets based upon the Labels but changes the budgets in order to hit the desired amount so if there's an underspend one day, the budgets will increase the next and so on.

 

If you're using any other Labels on your Campaigns, be sure to add them into this line (should be line 50 when pasted into the script window).  

 

.withCondition("Name NOT_IN ['FlexBudget']")

 

Other Labels need to be separated by commas and in single quotes, e.g.

 

.withCondition("Name NOT_IN ['FlexBudget','LabelA','LabelB']")

 

Full script below my name.  Please ensure you PREVIEW the script before running and/or scheduling it.  If it's OK, you should schedule it to run daily around 1am.

 

Jon

 

 

/* Modified Flexible Budget Script © Jon Gritton 2015 */

var TOTAL_BUDGET = 375;
var d = new Date(Utilities.formatDate(new Date(), AdWordsApp.currentAccount().getTimeZone(), "MMM dd,yyyy HH:mm:ss"));
var thisMonth=d.getMonth();
var thisYear=d.getFullYear();
// DAYS_SO_FAR is -1 because "today" hasn't happened yet
var DAYS_SO_FAR = d.getDate() - 1;
var TOTAL_DAYS = daysInMonth(thisMonth + 1, thisYear);
var DAYS_REMAINING = TOTAL_DAYS - DAYS_SO_FAR;


var START_DATE = new Date(thisYear, thisMonth, 1);
var END_DATE = new Date(thisYear, thisMonth, TOTAL_DAYS);

function main() {
setNewBudget(calculateBudgetEvenly, TOTAL_BUDGET, START_DATE, END_DATE);
}

function setNewBudget(budgetFunction, totalBudget, start, end) {
var costSoFar = 0;
var campaignsToSet = [];
var campaigns = AdWordsApp.campaigns()
//affect only live Campaigns
.withCondition("Status = ENABLED")
.withCondition("LabelNames CONTAINS_ANY ['FlexBudget']")
.get();
while(campaigns.hasNext()) {
var campaign=campaigns.next();
var thisCost = campaign.getStatsFor(dateToString(start), dateToString(end)).getCost();
costSoFar += thisCost;
campLabels=campaign.labels()
//get only the labels for percentages
.withCondition('Name IN ["FlexBudget"]')
.get();
if(campLabels.hasNext()) {
//this campaign is a Flexbudget
campaignsToSet.push(campaign);
Logger.log("CampaignToSet: " + campaign.getName());
}
}
var totalCalcd = 0;
Logger.log("CostSoFar: " + costSoFar);
totalBudget -= costSoFar;
Logger.log("Total available for Flex Campaigns for the rest of the month: " + totalBudget);
for(var i=0;i<campaignsToSet.length;i++) {
var thisCampaign=campaignsToSet[i];
cLabels=thisCampaign.labels()
// Ensure only the "percentage" Label is selected - you may need to add others if you use them
.withCondition("Name NOT_IN ['FlexBudget']")
.get();
var percentage = cLabels.next().getName();
//set the new budgets
var newBudget = budgetFunction(totalBudget, percentage);
newBudget=(newBudget>0)? newBudget : 1;
thisCampaign.getBudget().setAmount(newBudget);
Logger.log("Campaign Set: " + thisCampaign.getName() + " Budget: " + newBudget);
}
}

function calculateBudgetEvenly(totalBudget, percentage) {
//set budget based upon percentage Label
var budgetRemaining = totalBudget*(percentage / 100);

if (DAYS_REMAINING <= 0) {
return budgetRemaining;
} else {
return budgetRemaining / DAYS_REMAINING;
}
}

function daysInMonth(month, year) {
//returns number of days in a given month of a given year
return new Date(year, month, 0).getDate();
}

function dateToString(date) {
return date.getFullYear() + zeroPad(date.getMonth() + 1) + zeroPad(date.getDate());
}

function zeroPad(n) {
if (n < 10) {
return '0' + n;
} else {
return '' + n;
}
}

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Shared Budget Script - Using Labels

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

Morning @Jon_Gritton,

 

Once again, thank you, this looks like a great solution.. 

 

Couple of questions..

 

1, Does this work on ALL campaign types (Search/PLA/Display etc..)

as it only outputted Search in the log when i did a quick test. 

 

2, Is there a way I can associate our current labels (levels) to the percentages (instead of creating a bunch of new labels) - Our campaign Labels change quite often depending on performance, and our manager isn't too happy about a mass of labels.. plus, it would make it easier for our Juniors to understand.

 

eg: 

Level One = 50%

Level Two = 20%

Level Three = 15%

Level Four = 10%

Level Five = 5%

 

Kind regards,

Rich.

Re: Shared Budget Script - Using Labels

Top Contributor
# 4
Top Contributor

Hi @Richard H to answer your questions:

 

The script as is will work for both Search and Display Campaigns.  For Shopping Campaigns it just needs a tiny change to the Campaign selector line so that instead of 

 

var campaigns = AdWordsApp.campaigns()

 

it's

 

var campaigns = AdWordsApp.shoppingCampaigns()

 

I currently run two separate scripts to handle the two types, but in theory you could create one single script that looped twice and changed the selector on each loop.

 

It would be possible to use Labels such as "Level One", etc., and there's a couple of ways you could do it.  If your level Labels could be named without spaces (e.g. "LevelOne"), you could define these levels at the start of the script:

 

var LevelOne = 50;

var LevelTwo = 20;

 

and so on, then, once the script has extracted the Label that isn't "FlexBudget", that Label name will itself be the variable name so the only other change you need to make is to evaluate the variable.  The line:

 

var percentage = cLabels.next().getName();

 

would need to be:

 

var percentage = eval(cLabels.next().getName());

 

and you're done!  That's probably how I'd go (in truth, that's a more robust method to script anyway, since not using eval relies upon the script being quite "loose" in its definitions!).  Alternatively, if you must have spaces, you'll need a method to convert the label name into an appropriate percentage and you could do this via a select/case statement.

 

Hope this helps.

 

Jon

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Shared Budget Script - Using Labels

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭
Hello,

I followed all the instructions, but when the script runs, the new given value for each campaign is 1$.
I have three campaigns, 2 with a shared budget (each of them is labeled with the label "40" and one campaign with an individual budget, labeled with "20").
Is there a bug in the code?

Re: Shared Budget Script - Using Labels

Top Contributor
# 6
Top Contributor

Hi @Roman B a bug in my code?  Never (well, almost never).  Smiley Happy


Can you post the entire code you're actually using please?  There could be a number of reasons for the failure and it's important I (and others) can see the full code you're using.


For the percentage Labels, you are using just numbers, yes?  So the entire Label is 40 or 20, without quotes or any other text?  Have you excluded any other Labels you're using at the Campaign level?

 

Jon

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Shared Budget Script - Using Labels

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭
Hi Jon_Gritton,

The code is pasted below, as it's used.
Of course, only numbers in labels. But, I use some other labels for those campaigns, for other scripts. Should I exclude them in your script? If yes, where?

Re: Shared Budget Script - Using Labels

Top Contributor
# 8
Top Contributor

Hi @Roman B looks like you forgot to paste the code, don't worry though, I'm pretty sure it's the other labels causing the problem.

 

Look for the line like this in the code:

.withCondition("Name NOT_IN ['FlexBudget']")

If you have other labels on your Campaigns, you'll need to include all of them in this condition.  Separate the names by commas, each name in single quotes, as so:

.withCondition("Name NOT_IN ['FlexBudget','Label1','anotherLabel','one more']")

This is necessary so that the script will pick up only the percentage label.  Without these exclusions it'll pick up text labels and that's what causes the failure.

 

Jon

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Shared Budget Script - Using Labels

Visitor ✭ ✭ ✭
# 9
Visitor ✭ ✭ ✭
Hello Jon,

Thank you, I've already figured it out with the labels, now it works perfect for me. The only change I'll do to it would be the time frames, for example, there are campaigns that have to run for a week.
Thank you for your help!

Re: Shared Budget Script - Using Labels

Visitor ✭ ✭ ✭
# 10
Visitor ✭ ✭ ✭
Jon,
This has been a very useful script. and I truly appreciate you making it available. We've run into an issue that I am not sure how to solve. We've started running Video campaigns alongside our search and display campaigns. So we want to be able to split the budgets between all three of those. Do you have any advice to modify the scripts to work with video campaigns? It seems to just ignore them at the moment.

Thank you