Weather Triggered Content: How to customize user’s experience

Not only weather marketing…There is no doubt that weather conditions affect consumer behaviour. The changing weather can initiate a switch in the clothes we buy, the design of our homes and the music we listen to. This has been noticed by marketers, re…


This content originally appeared on Level Up Coding - Medium and was authored by Rafał Rybnik

Not only weather marketing…

There is no doubt that weather conditions affect consumer behaviour. The changing weather can initiate a switch in the clothes we buy, the design of our homes and the music we listen to. This has been noticed by marketers, resulting in the emergence of weather marketing. Using weather data, you can control when and where your ads are shown.

Unless stated otherwise, all pictures in the article are by the author.

For example, if you want to advertise the amusement park, you better take into account the weather forecast, because consumers often consider it when they are planning such activities. By combining the capabilities of today’s advertising platforms and real-time weather APIs, a message can be embedded that is relevant to your audience’s local weather.

In this article, I’ll show you how to build a system that displays messages (such as banner ads or proposed articles) based on the local weather. We will use Python (Flask) and the Weather API.

Let the static become dynamic

Let’s assume you run an online magazine. Most of the content that appears on the homepage is selected by the editorial team and is completely static — every user sees exactly the same thing.

As the season of very capricious weather in your country is approaching, you would like to customize the portal to display content that is consistent with the weather that every user sees outside the window.

So your task is to extend the CMS with features:

  • displaying specific articles depending on the current weather in the user’s area,
  • modifying the message according to the weather conditions,
  • displaying advertising campaigns based on the weather forecast for the coming days.

Portal architecture

Your portal works on the base of the Flask framework and its built-in templating system.

Note, as web development is not the main topic of this article, we use a mockup here. However, most of the knowledge can be easily transferred to use in other frameworks and programming languages.

Repository for the article:

fischerbach/large-portal

The initial state of the project can be found in the initial branch. The whole thing is quite simple, by using Articles().get_all() method, the articles are taken from the database and inserted into the main page in order.

Due to the expected change in weather, the journalists have prepared two articles: one on sunburn and one on what to do when it rains.

It doesn’t make much sense to display an article on sunburn to users whose town is in heavy rain, right? So first we need to get information about the weather conditions where the user lives.

Weather API

Courtesy of WeatherAPI.com, I was given access to their API. They offer free plans for real-time and forecasted weather and geolocation API. For our purposes, it is important that they provide real-time weather data and IP geolocation in the same request.

Downloading the current weather for a given IP address is very simple:

http://api.weatherapi.com/v1/current.json?key=81afd70710744cc380d213803210404&q=89.64.73.93&aqi=no`

We will use this for our solution.

Getting user IP

First, we need to acquire the IP address from which the user of our portal connects to pass it to the Weather API.

Note that if you are working in a local environment, you will get 127.0.0.1 when you refresh the page. Let’s create a function for the convenience of testing:

Now we can easily test different IP addresses by specifying them in the URL parameter:

http://127.0.0.1:5000/?ip=37.47.58.114

Get local weather

To retrieve data efficiently, I sketched a simple API client:

All results are cached. As this is real-time data, we can’t store it indefinitely without having to update it, so the time to live (TTL) is short — 15 minutes. However, this will help increase performance and reduce costs. Now we are ready to consume data from the API in our application.

Article recommendations accurate to weather

Ok, so our display strategy is to modify the Articles model with an additional field that stores the conditions for displaying them:

And then comparing them with the weather conditions for the user’s location:

Of course, a few minor technical changes are needed, which are beyond the scope of this article. Refer to the repository (branch `step1`).

Let’s check how our portal looks for Los Angeles users:

http://127.0.0.1:5000/?ip=Los%20Angeles

At the time of writing, the weather there is sunny, so the concern for sun protection seems well-placed :)

Weather targeted content

Let’s now adjust the look of the site to the weather. Let the background of the main page element reflect the weather outside the window:

Two minor code changes, and here is the result:

Note that for this solution to cover all weather phenomena, you need to upload backgrounds with names corresponding to them. A list of all weather conditions from the Weather API.

Weather triggered advertising

The last change will summarise everything we have learnt so far. We were approached by an umbrella manufacturer who wants his ad to only display if it’s supposed to rain the next day.

First, we need to add the forecast download to the Weather API client:

Then we create an Ads model:

Note that using the eval function is not safe and should be done with caution. Here it is used for brevity. In a production environment, it is better to use more powerful parsers with code sanitization.

We pass the data to the template:

Voilà:

It’s going to rain in Los Angeles tomorrow, although the weather is nice today. The final version of the project can be found in the repository, in the master branch.

Takeaways

Our portal is just a toy, of course, but with a little bit of code and access to the Weather API it has taken on an amazing life of its own. If you build in additional conditions at various points in the portal, users can really feel that it is built for them.

Thank you for reading. I hope you enjoyed reading as much as I enjoyed writing this for you.

If you would like to share feedback or simply say hello, you can connect with me: https://www.linkedin.com/in/rafalrybnik/?locale=en_US

If you enjoyed reading this, you’ll probably enjoy my other articles too:

References

Flask:
https://flask.palletsprojects.com/en/1.1.x/

WeatherAPI:
https://www.weatherapi.com/

https://www.dynamicyield.com/lesson/weather-based-targeting/
https://netlabe.com/how-to-forecast-website-traffic-e8bc5f0c34c8


Weather Triggered Content: How to customize user’s experience was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Rafał Rybnik


Print Share Comment Cite Upload Translate Updates
APA

Rafał Rybnik | Sciencx (2021-04-06T16:03:38+00:00) Weather Triggered Content: How to customize user’s experience. Retrieved from https://www.scien.cx/2021/04/06/weather-triggered-content-how-to-customize-users-experience/

MLA
" » Weather Triggered Content: How to customize user’s experience." Rafał Rybnik | Sciencx - Tuesday April 6, 2021, https://www.scien.cx/2021/04/06/weather-triggered-content-how-to-customize-users-experience/
HARVARD
Rafał Rybnik | Sciencx Tuesday April 6, 2021 » Weather Triggered Content: How to customize user’s experience., viewed ,<https://www.scien.cx/2021/04/06/weather-triggered-content-how-to-customize-users-experience/>
VANCOUVER
Rafał Rybnik | Sciencx - » Weather Triggered Content: How to customize user’s experience. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/06/weather-triggered-content-how-to-customize-users-experience/
CHICAGO
" » Weather Triggered Content: How to customize user’s experience." Rafał Rybnik | Sciencx - Accessed . https://www.scien.cx/2021/04/06/weather-triggered-content-how-to-customize-users-experience/
IEEE
" » Weather Triggered Content: How to customize user’s experience." Rafał Rybnik | Sciencx [Online]. Available: https://www.scien.cx/2021/04/06/weather-triggered-content-how-to-customize-users-experience/. [Accessed: ]
rf:citation
» Weather Triggered Content: How to customize user’s experience | Rafał Rybnik | Sciencx | https://www.scien.cx/2021/04/06/weather-triggered-content-how-to-customize-users-experience/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.