AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

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

Script to enable & pause campaigns based on Weather

J M
Follower ✭ ☆ ☆
# 1
J M
Follower ✭ ☆ ☆

Looking for help to create a script that simply enables & disables display campaigns based on weather. I know there's a guide for bid modifiers (https://developers.google.com/adwords/scripts/docs/solutions/weather-based-campaign-management) but I can't seem to manipulate the code to just enable & disable DISPLAY campaigns. If anyone (JON!!) could help me out in creating one - I'd VERY much appreciate it. I've always found help here. Thanks again!

1 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author J M
September 2015

Re: Script to enable & pause campaigns based on Weather

J M
Follower ✭ ☆ ☆
# 2
J M
Follower ✭ ☆ ☆
any help out there?

View solution in original post

Marked as Best Answer.
Solution
Accepted by topic author J M
September 2015

Re: Script to enable & pause campaigns based on Weather

J M
Follower ✭ ☆ ☆
# 2
J M
Follower ✭ ☆ ☆
any help out there?

Re: Script to enable & pause campaigns based on Weather

Top Contributor
# 3
Top Contributor

Hi @J M sorry I missed this post earlier, I do try to catch the script ones.  Make sure you call me out using the "@" feature in future and I'll get a notification via email...

 

I built my own script for weather bidding as I found the Google example overly complex for my needs.  My client had only one location so there was no need for a spreadsheet and the script is a lot simpler.  Here's the important part of the code:

 

// Simple weather script
// (c) Jon Gritton 2015

var APIKEY = "your api key";
var LOCATION = "your location code";

function main() {
getWeather();
}

function getWeather() {
var url = Utilities.formatString(
'http://api.openweathermap.org/data/2.5/weather?APPID=%s&id=%s',
encodeURIComponent(APIKEY),
encodeURIComponent(LOCATION));

var response = UrlFetchApp.fetch(url);
if (response.getResponseCode() != 200) {
throw Utilities.formatString(
'Error returned by API: %s, Location searched: %s.',
response.getContentText(), LOCATION);
}

var result = JSON.parse(response.getContentText());
var precipitation = 0;
if (result.rain && result.rain['3h']) {
precipitation = result.rain['3h'];
}

var clouds = result.clouds.all;
var wSpeed = result.wind.speed;
var temp = result.main.temp_max - 273;

Logger.log("Clouds: " + clouds + ", Wind Speed: " + wSpeed + ", Rain: " + precipitation + ", Temp: " + temp);

// at this point you have all the weather data you need to make a decision.
// below here you can then just write code to enable or pause a Campaign based upon this data

// insert your code here

}

 

So, get your own API key (there are instructions for this on the sample script site) and location code (again, instructions for this on the example site) then the code above will retrieve the weather and load the variables, precipitation, clouds, wSpeed temp.  What I did was use a simple formula and conditional statements to decide what to do based upon these variables and you may want to do the same.  So, for example, if rain > 0 and temp < 30, or if the wind is above a certain speed, etc.  Once you have a variable that represents whether you want to pause or enable the Campaign, you can use a simple selection to do this, e.g.

 

var campState = (precipitation > 0)? true : false; //will be true if it's raining, false if not

var theCamp = AdWordsApp.campaigns()

  .withCondition("Name = 'your campaign name'")

  .get().next();

 

if (campState) {

  theCamp.pause();

}

else {

  theCamp.enable();

}

 

Does this help?


Jon

 

 

 

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

Re: Script to enable & pause campaigns based on Weather

J M
Follower ✭ ☆ ☆
# 4
J M
Follower ✭ ☆ ☆
You are a legend - such appreciation for your help ! But I'm running into an error when previewing.

TypeError: Cannot read property "all" from undefined. (line 30)

Generated from this line: var clouds = result.clouds.all;

Re: Script to enable & pause campaigns based on Weather

J M
Follower ✭ ☆ ☆
# 5
J M
Follower ✭ ☆ ☆
FYI I ended up figuring out the fix, thanks for the help!

Script to enable &amp; pause campaigns based on Weather

Visitor ✭ ✭ ✭
# 6
Visitor ✭ ✭ ✭

Hi Jon

 

Thanks for sharing your knowledge here. Additionally I was wondering whether the script can be modified to check weather data in advance an change the CampState based on it. 

 

So specifically I want a campaign to start 2-3 days before it starts to rain and end the day it's sunny or not raining anymore. How would I achieve that?