External Factors: Weather-Based Bid Adjustments
Wed Feb 21 2024 17:46:12 GMT+0000 (Coordinated Universal Time)
Saved by
@tchives
#javascript
function main() {
// Get your OpenWeatherMap API key (Sign up for a free account)
var apiKey = "YOUR_API_KEY";
// Target location (City or Zip Code)
var targetLocation = "Los Angeles, CA";
// Campaign Selection
var campaignName = "Summer Campaign";
// Bid adjustments based on temperature
var temperatureRanges = {
"below_70": -0.1, // Decrease bid by 10% if below 70°F
"70_to_85": 0.0, // No change between 70°F and 85°F
"above_85": 0.2 // Increase bid by 20% if above 85°F
};
// Fetch current weather data
var weatherUrl = "https://api.openweathermap.org/data/2.5/weather?q=" + targetLocation + "&appid=" + apiKey + "&units=imperial";
var weatherResponse = UrlFetchApp.fetch(weatherUrl);
var weatherData = JSON.parse(weatherResponse.getContentText());
// Extract temperature from the data
var currentTemperature = weatherData.main.temp;
// Determine the appropriate bid modifier
var bidModifier;
for (var range in temperatureRanges) {
if (currentTemperature >= range.split("_")[1]) {
bidModifier = temperatureRanges[range];
break;
}
}
// Apply the bid modifier at the campaign level
var campaign = AdsApp.campaigns().withCondition("Name = '" + campaignName + "'").get().next();
campaign.setBidModifier(1 + bidModifier);
}
content_copyCOPY
Comments