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.
Here are the steps in the script explained:
- We don’t want to output script errors when something goes wrong, so we disable them here.
- Here we get the IP address of our visitor.
- With the Geoplugin service, we can get the corresponding geolocation info for our visitor’s IP address.
- 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.
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:
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.
We also need to configure two tags. We can do this in the Tags tab.
The first Tag will load the weather script if weather has not loaded before in this session. Click the Add Tag button and add a new Tealium Generic Tag.
Select Weather has not loaded this session
The second tag will send the weather data as custom dimensions to Google Analytics. Click the Add Tag button and add a new Google Universal Analytics tag.
Select Weather has loaded
The last thing we need to do (before we publish) is to persist the fact that the weather has run in this session in a cookie. To do this, go to the Extensions tab.
Click the Add Extension button to add a Persist Data Value extension.
Save and Publish
Click the Save/Publish button to save the configuration and publish it.
Although some technical knowledge is required, you can see in the steps above that it’s not that difficult to add this invaluable data to your Google Analytics data with Tealium. Depending on your needs, or the needs of your client, other information can be added in a very similar way. The possibilities are endless: public transportation attendance, public holidays, stock prices…