In an earlier article, I spoke about setting up a tool in Google Cloud Platform to automate your campaigns. Now, what if you don't use Google Cloud, or it's too much work just to automate a few campaigns?
Do you know you can use Ads Script to do the same?
Just a recap, if you need the basic of ads script, you can check it out in this article: Ads Script Beginner’s Guide: Setup and run your first script.
The code
First part: Create the function to get weather condition
Relatively straight forward, just need to call weather from a weather API provider. Example to use: https://openweathermap.org/current
Sign up for their API and call the API end point with the key, longitude and latitude of the location.
function getWeatherForecast(lat,lon,APIkey) {
// Use an external weather API to get forecast data
// Replace with your actual API call and data parsing
var weatherApiUrl = 'https://api.openweathermap.org/data/2.5/weather?lat=' + lat +'&lon=' + lon +'&appid='+ APIkey;
var response = UrlFetchApp.fetch(weatherApiUrl);
var jsonData = JSON.parse(response.getContentText());
// Extract relevant weather information
// Could be different depending on your weather API
var condition = jsonData.weather.main;
// Add more data extraction as needed (temperature, etc.)
return { condition: condition };
}
2nd part: Create function to turn off the campaigns if conditions are met
Just a function to take in campaign ID and pause/enable it.
function campaignStatusUpdate(campaignId, newState) {
var campaign = AdsApp.campaigns().withIds([campaignId]).get().next();
// pause or enable campaign based on new campaign state
if (newState = "on") {
campaign.enable();
}
if (newState = "off") {
campaign.pause();
}
}
Lastly: To tie things up in main function
To tie everything together in your main function.
function main() {
// Replace with your campaign ID and location
var campaignId = 'YOUR_CAMPAIGN_ID';
var lat = '31';
var lon = '74';
// get API key from your weather API provider
var APIkey = "123456";
// Get weather forecast
var weatherData = getWeatherForecast(lat,lon,APIkey);
// Adjust budget based on weather
if (weatherData.condition === 'Rain') {
campaignStatusUpdate(campaignId, "off"); // switch off campaign
} else if (weatherData.condition === 'Clear') {
campaignStatusUpdate(campaignId, "on"); // Great weather, let's go and power your business with Google Ads
}
// Add more conditions as needed
}Conclusion
There you had it! A pretty simple script to run on your Google Ads account to automate some campaign management tasks.
Full code:
function main() {
// Replace with your campaign ID and location
var campaignId = 'YOUR_CAMPAIGN_ID';
var lat = '31';
var lon = '74';
// get API key from your weather API provider
var APIkey = "123456";
// Get weather forecast
var weatherData = getWeatherForecast(lat,lon,APIkey);
// Adjust budget based on weather
if (weatherData.condition === 'Rain') {
campaignStatusUpdate(campaignId, "off"); // switch off campaign
} else if (weatherData.condition === 'Clear') {
campaignStatusUpdate(campaignId, "on"); // Great weather, let's go and power your business with Google Ads
}
// Add more conditions as needed
}
function getWeatherForecast(lat,lon,APIkey) {
// Use an external weather API to get forecast data
// Replace with your actual API call and data parsing
var weatherApiUrl = 'https://api.openweathermap.org/data/2.5/weather?lat=' + lat +'&lon=' + lon +'&appid='+ APIkey;
var response = UrlFetchApp.fetch(weatherApiUrl);
var jsonData = JSON.parse(response.getContentText());
// Extract relevant weather information
// Could be different depending on your weather API
var condition = jsonData.weather.main;
// Add more data extraction as needed (temperature, etc.)
return { condition: condition };
}
function campaignStatusUpdate(campaignId, newState) {
var campaign = AdsApp.campaigns().withIds([campaignId]).get().next();
// pause or enable campaign based on new campaign state
if (newState = "on") {
campaign.enable();
}
if (newState = "off") {
campaign.pause();
}
}
A shameless plug here. I am an advertising professional with a mission to teach others about marketing technology.
If you enjoyed this article, please do consider buying me a coffee here!
Cheers friends!
