Using Current Weather Data in Your Marketing Campaigns: An Example with AccuWeather
Weather data can transform marketing strategies by enabling real-time personalization. Imagine tailoring your email content based on a recipient's local weather conditions or displaying dynamic weather updates directly in an email. By integrating AccuWeather API with Salesforce Marketing Cloud (SFMC), you can achieve these capabilities efficiently.
This code in the post can be used in two approaches:
- Fetching and displaying weather data directly in email templates to personalize content dynamically.
- Running automated processes to fetch and store weather data for required locations, enabling batch updates for targeted campaigns.
Overview of the Solution
In this example, we assume you have a sendable customer Data Extension (DE) with the following attributes:
- Zipcode: The postal code of the customer.
- Location: Optional, descriptive location name (e.g., city).
- Contact ID: Unique identifier for the customer.
The script fetches weather data using the AccuWeather API and writes it to a destination DE with these attributes:
- LocationKey: A unique identifier for the location from AccuWeather. Mark this as primary key.
- LocationName: Name of the location.
- ForecastDate: The date of the forecast. Mark this as primary key.
- MaxTemperature: Maximum temperature for the day.
- MinTemperature: Minimum temperature for the day.
- WeatherCondition: A brief description of the weather condition.
Getting API Access from AccuWeather
To access the AccuWeather API, follow these steps:
- Visit the AccuWeather Developer Portal.
- Click Register or Sign In.
- Complete the registration form and verify your account through the email provided.
- Once logged in, navigate to the Apps section and create a new app.
- Choose the API products you want to access and note down the API key generated for your app.
APIs Available
AccuWeather provides a range of APIs to cater to diverse weather data needs. Here are some key APIs:
- Current Conditions API: Fetches real-time weather conditions.
- Forecast APIs: Provides daily or hourly weather forecasts.
- Alerts API: Delivers weather alerts and warnings.
- Indices API: Offers lifestyle indices such as air quality and UV index.
For a full list, visit AccuWeather APIs.
APIs Used in This Example
In this example, we use the following APIs:
- Location API: To fetch location keys based on zip codes.
- Endpoint:
/locations/v1/postalcodes/search
- Endpoint:
- 1-Day Forecast API: To retrieve daily weather forecasts.
- Endpoint:
/forecasts/v1/daily/1day/{locationKey}
Implementation Steps
Prepare Data Extensions:
- Source Data Extension: Contains customer data, including zip codes.
- Destination Data Extension: Stores fetched weather data.
Acquire API Key:
- Sign up at AccuWeather Developer Portal to get your API key.
Deploy the Script:
- Paste the script in your SFMC Automation Studio or CloudPages and schedule it for regular execution.
Final Working Code
Here’s the optimized SSJS code for automating weather data management:
<script runat="server"> Platform.Load("Core", "1.1.1"); // AccuWeather API details var apiKey = "YOUR_API_KEY"; // Replace with your API Key // Initialize the source Data Extension var sourceDE = DataExtension.Init("SOURCE_DE_EXTERNAL_KEY"); if (!sourceDE) return; // Retrieve rows from the source Data Extension var rows = sourceDE.Rows.Retrieve(); if (!rows || rows.length === 0) return; // Initialize the destination Data Extension var weatherDE = DataExtension.Init("DESTINATION_DE_EXTERNAL_KEY"); if (!weatherDE) return; // Process each row and fetch weather data for (var i = 0; i < rows.length; i++) { var zipcode = rows[i].Zipcode || "N/A"; var locationName = rows[i].Location || "N/A"; if (zipcode === "N/A") continue; // Fetch LocationKey using the Zipcode var locationUrl = "http://dataservice.accuweather.com/locations/v1/postalcodes/search?apikey=" + apiKey + "&q=" + zipcode; var locationReq = new Script.Util.HttpRequest(locationUrl); locationReq.emptyContentHandling = 0; locationReq.retries = 2; locationReq.continueOnError = true; locationReq.contentType = "application/json"; locationReq.method = "GET"; var locationResp = locationReq.send(); if (Number(locationResp.statusCode) !== 200) continue; var locationData = Platform.Function.ParseJSON(String(locationResp.content)); if (!locationData || locationData.length === 0) continue; var locationKey = locationData[0].Key; if (!locationKey) continue; // Fetch 1-day weather forecast using the LocationKey var weatherUrl = "http://dataservice.accuweather.com/forecasts/v1/daily/1day/" + locationKey + "?apikey=" + apiKey; var weatherResponse = HTTP.Get(weatherUrl); if (!weatherResponse || !weatherResponse.Content) continue; var weatherData = Platform.Function.ParseJSON(String(weatherResponse.Content)); if (!weatherData || !weatherData.DailyForecasts) continue; var forecast = weatherData.DailyForecasts[0]; var forecastDate = forecast.Date; var maxTemp = forecast.Temperature.Maximum.Value; var minTemp = forecast.Temperature.Minimum.Value; var condition = forecast.Day.IconPhrase; // Log attributes before upsert Write("Attributes to Upsert:<br>"); Write("LocationKey: " + locationKey + "<br>"); Write("LocationName: " + locationName + "<br>"); Write("ForecastDate: " + forecastDate + "<br>"); Write("MaxTemperature: " + maxTemp + "<br>"); Write("MinTemperature: " + minTemp + "<br>"); Write("WeatherCondition: " + condition + "<br>"); // Insert/Update the weather data into the destination Data Extension Platform.Function.UpsertData(weatherDE, ["LocationKey", "ForecastDate"], [locationKey, forecastDate], ["LocationName", "MaxTemperature", "MinTemperature", "WeatherCondition"], [locationName, maxTemp, minTemp, condition]); } </script>
Benefits
- Real-Time Personalization: Tailor email content dynamically based on local weather conditions.
- Automation: Reduces manual effort in fetching and updating weather data.
- Integration: Seamlessly integrates with existing Salesforce Marketing Cloud workflows.
Conclusion
Integrating AccuWeather API with SFMC provides the flexibility to personalize marketing campaigns based on weather data. Whether it’s embedding dynamic weather information in emails or automating batch updates for targeted campaigns, this solution offers an edge in delivering relevant and timely customer experiences.
Have questions? Share your thoughts in the comments below!
No comments:
Post a Comment