If you’ve ever wanted a Google Ads Script to automatically update the daily budget from a Google Sheet, here’s one version you can use.
Creating your first Google Ads script can be intimidating especially if you’ve never written code before or don’t know javascript.
Here is a sample Google Ads script that updates the daily budget of your campaign based on values set in a Google Sheet:
function main() {
// Get the current date
var currentDate = new Date();
// Set the spreadsheet URL
var spreadsheetUrl = 'https://docs.google.com/spreadsheets/d/<spreadsheet_id>';
// Get the budget values from the Google Sheet
var budgetValues = getBudgetValues(spreadsheetUrl, currentDate);
// Get the campaign
var campaign = AdWordsApp.campaigns().withCondition('Name = "Your Campaign Name"').get().next();
// Set the daily budget
campaign.setDailyBudget(budgetValues.dailyBudget);
// Set the start and end dates for the budget
var budget = campaign.getBudget();
budget.setStartDate(budgetValues.startDate);
budget.setEndDate(budgetValues.endDate);
}
function getBudgetValues(spreadsheetUrl, currentDate) {
// Get the spreadsheet and the budget sheet
var spreadsheet = SpreadsheetApp.openByUrl(spreadsheetUrl);
var budgetSheet = spreadsheet.getSheetByName('Budget');
// Get the number of rows in the sheet
var numRows = budgetSheet.getLastRow();
// Loop through the rows to find the budget values for the current date
for (var i = 1; i <= numRows; i++) {
var startDate = budgetSheet.getRange(i, 1).getValue();
var endDate = budgetSheet.getRange(i, 2).getValue();
var dailyBudget = budgetSheet.getRange(i, 3).getValue();
// If the current date is within the start and end dates, return the budget values
if (currentDate >= startDate && currentDate <= endDate) {
return {
startDate: startDate,
endDate: endDate,
dailyBudget: dailyBudget
};
}
}
// If no budget values were found, return null
return null;
}
This script assumes that your Google Sheet has a sheet named “Budget” with the following columns:
- Start Date: The start date for the budget period.
- End Date: The end date for the budget period.
- Daily Budget: The daily budget for the budget period.
The script will look for the budget values for the current date in the “Budget” sheet and update the daily budget of the specified campaign accordingly. If no budget values are found for the current date, the script will do nothing.
To use this script, you will need to replace <spreadsheet_id>
in the spreadsheetUrl
variable with the ID of your Google Sheet, and replace “Your Campaign Name” with the name of your campaign. You can find the ID of your Google Sheet in the URL of the sheet (it is the long string of characters between /d/
and /edit
).
Hopefully this helps you manage your Google Ads campaigns easier.
Leave a Reply