Add weather information to your Google Analytics with Tealium | All Categories

In this article I’ll explain how to add weather information to your Google Analytics data using Tealium. This article is rather technical, so if words like JavaScript, JSON, PHP and server scare you, this article might not be the right article for you.

Collecting the weather data

First we need to collect the weather data for the current location of the visitor. We can do this with a simple PHP script on our server that gets the current location of the visitor based on the visitor’s IP address and then looks up the weather information for that location. We’ll upload this script to our server and name it weather_script.php.

tealium weather data 1

Here are the steps in the script explained:

  1. Our script will be outputting JavaScript, so we send the correct header info to the browser.
  2. We don’t want to output script errors when something goes wrong, so we disable them here.
  3. Here we get the IP address of our visitor.
  4. With the Geoplugin service, we can get the corresponding geolocation info for our visitor’s IP address.
  5. When we have the geolocation data, we can get the actual weather info for that location. Note that you need to register for a free API key in order to use the Open Weather Map service.
  6. Here we output the weather data to the browser in a JavaScript call to utag.link so it’ll end up in our Data Layer.

This PHP script, when called from a browser, will output a JavaScript function call similar to the following:

Tealium weather data 2

Data Layer

When our script is working, we need to set up our Data Layer. Log in to Tealium IQ and go to Data Layer. Add the following variables:

Tealium weather data 3

Load Rules

After we’ve set up the Data Layer, it’s time to set up the Load Rules. To do this, click the Load Rules  tab.

The first Load Rule we need, will fire whenever the weather script has not yet loaded in the current session. We can check this by checking if our weather_done cookie does not equal weather_done.

 

Tags:

Contacteer Ons

Semetis | Scheldestraat 122, 1080 Brussel - België

welcome@semetis.com

Volg Ons

Cookie Policy

This website uses cookies that are necessary to its functioning and required to achieve the purposes illustrated in the privacy policy. By accepting this OR scrolling this page OR continuing to browse, you agree to our privacy policy.