Friday, July 17, 2020

Google Apps Script Simple Weather API Integration

Here's a simple approach to weather API integration with Google Apps Script. The objective of this script is fetch weather forecast data from a free API provider Openweathermap. The API provides you the basic weather information from historical, current and even a 30 day forecast. You need to register to this API first you to have access to the free resources. which is just enough to get you running.
Apps Script

In the script, shown the basic steps to build that approach to properly call the API and return the desired weather information you might need in your applications. For a successful run you will have below as the return values to choose from. You can create another function to fetch desired weather information.

The Script:
[20-07-18 09:37:13:462 HKT] Target OpenWeatherOrg City ID : 1850147
[20-07-18 09:37:13:511 HKT] Weather description : shower rain
[20-07-18 09:37:13:513 HKT] {main={temp_min=293.15, humidity=100.0, pressure=1012.0, feels_like=294.33, temp=293.15, temp_max=293.15}, dt=1.595035815E9, id=1850147.0, name=Tokyo, coord={lat=35.69, lon=139.69}, clouds={all=75.0}, wind={speed=3.6, deg=50.0}, cod=200.0, timezone=32400.0, sys={sunrise=1.595014718E9, id=8077.0, sunset=1.595066154E9, type=1.0, country=JP}, weather=[{icon=09d, main=Rain, id=521.0, description=shower rain}, {description=mist, icon=50d, main=Mist, id=701.0}], rain={1h=13.97}, base=stations, visibility=5000.0}
function main(){
//base url
var api_url = 'http://api.openweathermap.org/data/2.5/weather?';
//Write your openweather.org api key here
var api_key = 'XXXXXXXXXXXXXXXXXXX';//<---openweather API key here
//Get's city open weather org id
var open_weather_org_id = GETCITYOPENWEATHERMAPID('Tokyo');//<---Sample city
Logger.log('\t\tTarget OpenWeatherOrg City ID : ' + open_weather_org_id);
//Get's current city open weather data
var weather_data = CURRENTWEATHER(open_weather_org_id,api_url,api_key);
Logger.log('\t\tWeather description : ' + weather_data.weather[0].description);
Logger.log(weather_data);
}
/*
THIS IS A JSON FORMAT FOR CITIES LIST AND ITS CORRESPONDING OPENWEATHERMAP IDS.
YOU CAN FIND OTHER CITY DETATILS IN THIS SITE (http://web.archive.org/web/20180619015316/http://openweathermap.org/help/city_list.txt)
*/
function GETCITYOPENWEATHERMAPID(city){
var AdWord_Map = {
"Hokkaido":[{"Criteria_ID":20624, "Name":"Hokkaido", "Canonical_Name":"Hokkaido,Japan", "Prefecture":"Hokkaido", "Parent_ID":2392, "Country_Code":"JP", "Target_Type":"Prefecture", "Status":"Active", "Open_Weather_Map_ID":2128295}],
"Saitama":[{"Criteria_ID":20634, "Name":"Saitama Prefecture", "Canonical_Name":"Saitama Prefecture,Japan", "Prefecture":"Saitama", "Parent_ID":2392, "Country_Code":"JP", "Target_Type":"Prefecture", "Status":"Active", "Open_Weather_Map_ID":6940394}],
"Chiba":[{"Criteria_ID":20635, "Name":"Chiba Prefecture", "Canonical_Name":"Chiba Prefecture,Japan", "Prefecture":"Chiba", "Parent_ID":2392, "Country_Code":"JP", "Target_Type":"Prefecture", "Status":"Active", "Open_Weather_Map_ID":2113015}],
"Tokyo":[{"Criteria_ID":20636, "Name":"Tokyo", "Canonical_Name":"Tokyo,Japan", "Prefecture":"Tokyo", "Parent_ID":2392, "Country_Code":"JP", "Target_Type":"Prefecture", "Status":"Active", "Open_Weather_Map_ID":1850147}],
"Kanagawa":[{"Criteria_ID":20637, "Name":"Kanagawa Prefecture", "Canonical_Name":"Kanagawa Prefecture,Japan", "Prefecture":"Kanagawa", "Parent_ID":2392, "Country_Code":"JP", "Target_Type":"Prefecture", "Status":"Active", "Open_Weather_Map_ID":1848354}],
"Niigata":[{"Criteria_ID":20638, "Name":"Niigata", "Canonical_Name":"Niigata,Japan", "Prefecture":"Niigata", "Parent_ID":2392, "Country_Code":"JP", "Target_Type":"Prefecture", "Status":"Active", "Open_Weather_Map_ID":1855431}],
"Toyama":[{"Criteria_ID":20639, "Name":"Toyama Prefecture", "Canonical_Name":"Toyama Prefecture,Japan", "Prefecture":"Toyama", "Parent_ID":2392, "Country_Code":"JP", "Target_Type":"Prefecture", "Status":"Active", "Open_Weather_Map_ID":1849876}],
};
return AdWord_Map[city][0].Open_Weather_Map_ID.toString();
}
function CURRENTWEATHER(city_id,api_url,api_key){
//Combines the credentials to complete the desired api url
var api_url_id = api_url + 'id=' + city_id + '&appid=' + api_key;
//Fetch the url from api link
var response = UrlFetchApp.fetch(api_url_id.toString());
var json = response.getContentText();
var weather_data = JSON.parse(json);
return weather_data;
}

Google AdWord application

Aside from using it for a weather applications, there's a clever user for this in Google AdWords campaign strategy. Let's say you are run a campaign for a product tied up to a specific season and for that matter, generate's demand for certain weather. 

The key information from above script is the return value weather description(i.e., Sunny, Cloudy or Rainy). AdGroup names could be tailored containing the same weather description for easy script manipulation. You could iterate all your adgroups to detect those names.

There you have it. A quick and simple foundation of this automation. I have a working script that encompass the full AdWord dynamics. For Advanced Google AdWords, this will be a useful approach to optimize your campaigns and protect your budgets. Leave a comment below for your thoughts.

No comments:

Post a Comment