Universal Analytics: Weather As A Custom Dimension


There is a new version of this post for GTM V2 here.

[Last updated June 2014] I’ve fallen in love with Universal Analytics and Google Tag Manager. Together they form an incredibly powerful tool for marketing professionals. In most cases, I no longer need to post recommendations to my client for yet another page template revision, since with the tag manager in place, I can just add custom code via the admin panel. Add to that the power of Universal Analytics with its ultra-sensitive Measurement Protocol, and the ability to craft custom dimensions and metrics, and voila! I’m in a happy place.

In this post, I take you through a short JavaScript dev journey of utilizing weather data as a custom dimension in your site’s Analytics. You only need to have Universal Analytics and Google Tag Manager installed on your site.


X


The Simmer Newsletter

Subscribe to the Simmer newsletter to get the latest news and content from Simo Ahava into your email inbox!

The end result

This is what you get:

As you can see, I’ve created a custom report which shows visits, pageviews and conversion rates (Pages / Visit > 2) for various weather conditions. This report unequivocally, with a plain-as-day-without-a-doubt display of causality, proves that when it rains, people are more likely to convert. I should do more targeting to British audiences…

Create a new tag in GTM

  • Name the tag “Weather API”
  • Choose Custom HTML Tag as the tag type
  • Enter the following code into the HTML field (Note! You need to load jQuery for this to work! See here for more information):
script>
  var lat = geoplugin_latitude();
  var lon = geoplugin_longitude();
  var weather = "";
  var weatherAPI = "http://api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+lon;

  $.ajax({
    type : "POST",
    dataType : "jsonp",
    url : weatherAPI+"&units=metric&callback=?",
    async : true,
    success : function(data) {
      weather = data.weather[0].main;
      dataLayer.push({"weather": weather});
    },
    error : function(errorData) {
      console.log("Error while getting weather data :: "+errorData.status);
    },
    complete : function() {
      dataLayer.push({"event": "weatherDone"});
    }
  });
script>
  • Add rule to fire tag on every page ({{url}} matches RegEx .*)

Here’s what’s going on.

First, a couple of external JavaScript functions are called to retrieve the longitude and latitude of the visitor by using their IP addresses. I’m using the free geoPlugin service. You can load it on your site by adding

We will be happy to hear your thoughts

Leave a reply

Som2ny Network
Logo
Compare items
  • Total (0)
Compare
0