Google Tag Manager activation code

11 February 2019

Using Google Tag Manager with Analytics to track Tableau visualisations

Who's looking at your Tableau Public visualisations?

Customer insight, and understanding our customers' behaviour, is an essential 'behind the scenes' activity for any organisation. This blog takes a technical detour from my usual business focus, to help people using Tableau set up their dashboards to allow tracking with Google Analytics, using Google's Tag Manager.

This post builds on an excellent blog post by Tableau Zen Master Jeffrey Shaffer's January 2017 article "Google analytics on Tableau public visualisations" by addressing changes required to use Google Tag Manager.

If you're not interested in the technical stuff and came here for some customer service talk, why not head over to my Tableau Public page and take a look at some analysis of complaints performance in the UK Public sector, UK Energy sector, New Zealand Official Information Act requests. Or you can see how UK MPs have been voting in Parliament during the Brexit debates.

If you'd like to find out more about setting up analytics for Tableau dashboards though, read on!

Updating Analytics tracking for Google Tag Manager


I followed the instructions in Jeff's post, but Google have now produced Google Tag Manager, so this blog provides an update of the steps you can take to install tracking with Google Tag Manager.  Doing so hugely simplifies the setup of Google Analytics tracking in Tableau Public dashboards, once you've got the initial setup in place.

This article is also based heavily on advice in Carlos Escalera's outstanding blog posts on removing unwanted spam and internal hits from your Google Analytics tracking.

Let me be clear, I'm no Google Tag Manager or Analytics expert, but using Carlos's blog articles I've managed to set up my own Google Analytics website tracking to filter out spam effectively and track hits on my Tableau Public dashboards, thanks to Jeff's article. It works, and this post tells you the method I used to achieve it.

How does tracking Tableau Dashboards with Google Tag Manager differ?


Google Tag Manager is intended to make it easier to manage a whole range of events on a website by configuring tags within Tag Manager, rather than having to code things into website scripts. For those of us who aren't hard core web developers, it means providing flexible tracking tools, without having to get down into the guts of scripts and coding quite as much as before.

In the context of setting up Google Analytics for Tableau visualisations, this means:

1. Configuring setup just once, to use oneTracking ID for all your Tableau dashboards
2. Tracking a new dashboard's views can all be done from within Google Analytics more simply.
3. Creating tracking for your dashboard now just involves copying one text file

Previously, to set up Google Analytics tracking, you had to provide a unique tracking ID code inserted into an HTML page on every single page you wanted to track.  What Google Tag Manager does is change this to allow you to set up many tracking options, without having to amend code on your site so much.

Basically, once you're set up to use Google Tag Manager's tracking code, every hit to your site is pointed at the Tracking Manager. You then use this interface to configure what to do when a hit arrives at your site, rather than coding it into the web page. Much more flexible and powerful!

What if I already have Google Analytics tracking set up on my website?


If you already have Google Analytics set up, there's a bit of a "but" in here. You will need to do some migration of your site over to Tag Manager, as it changes the basis of the tracking code used. You can't just leave the original code there, or you'll start getting double counting on your analytics site.

Essentially, this involves:
  1. Setting up a Google Tag Manager account for your site. You'll get a new unique 'GTM' ID number for your site (just like your old Google Analytics tracking ID code).
  2. Replace any existing Google Analytics ID code with the new GTM ID code snippets.
  3. Set up any Analytics ID codes you still want to use for website tracking as Google Tag Manager tags and trigger events.
There's a battery of articles out there on the web about migrating your sites, so I won't cover that here. Just google "migrating to google tag manager" and spend as many hours as it takes!

Let's get on with the steps you need for tracking your Tableau dashboards. This updates the steps written by Jeff Shaffer in his original article, but I'd recommend you read his original article first as it explains the fundamental concepts well.

I've adapted the steps in Jeff's original article to:
  1. Use Google Tag Manager to set up Analytics Tracking
  2. Create a single Google Analytics Tracking ID to track all my Tableau visualisations
  3. Create a single template HTML code file using Google Tag Manager's tracking code
  4. Use a copy of this template file, with a unique filename, to distinguish each dashboard
  5. Remove the need for a unique Tracking ID, code and shortened URL link for every dashboard.
Let's get started.

How do I set up Tag Manager to track Tableau Dashboards?


You'll need four things to complete this:

1. Your own website, with SSL enabled.
2. A Google Tag Manager account.
3. A template file with some HTML code (we'll set this up in a second).
4. A Google Analytics account.

Step 1. Prepare a website directory ready for tracking

• On your website server, set up a new directory that's uniquely named.  I used /analytics/

We'll be using this directory to hold individual tracking code files so we can identify individual dashboard file hits in Google Analytics. You'll use the full site pathname in Google Analytics set up.


Step 2. Get your Google Tag Manager GTM tracking code and create a template tracking file

You'll need a Google Tag Manager account ID from https://tagmanager.google.com/

If you haven't got a Google Tag Manager account set up already, create one. I've used a dummy website here, but I created the account for my live website.  This *shouldn't* cause a problem if you're going to migrate, as it won't do anything until the tracking code is inserted and you'll be all set up to migrate other stuff later.

• Create an account (or use your current website one if you're already on Google Tag Manager).


• Then create a Container using your website name.


• You'll see your workspace page which contains your site's GTM tracking number in blue. Click it to see your tracking code. This is unique to your site.



This code needs to go into every page that you'd like to track with Google Tag Manager. and part of every HTML page you want to track with Tracking Manager.

• Use a text editor to create a simple HTML template text file.
• I'm calling it "tableau-tracking-template.html"



• Save this file to your /analytics/ path. This is the master template you'll copy and rename for each dashboard you want to track via Google Analytics.  That's all the coding you need to do finished now.


Step 3. Go to Google Analytics and get a new Tracking ID

In this method, we'll be using one Tracking ID for all our Tableau Dashboards.

• Open Google Analytics and go to the Admin screen
• If you already have a website analytics account, you can select it and create a 'New Property'
  If not, you can set up a New Account (just as Jeff did in his original article)

• Set the Default URL to be the full pathname from Step 1 (e.g. https://your website/analytics/ )



• Note down the Tracking ID you're given - you'll need that next to configure Google Tag Manager.


Step 4. Configure Google Tag Manager

Now we have our Tracking ID, we can set up Google Tag Manager.  In essence, Google Tag Manager - once activated by the code on a webpage - is sitting there waiting for something to happen (a trigger event) at which point it will fire off an action (defined by a tag).

In our case, we want a hit on a Tableau Dashboard to register in Google Analytics.

In Tag Manager, first create a new variable name to hold your Tableau Tracking ID.
  • Go to 'variables' and click 'new' under 'user defined variable'

  • Call it 'Tableau Analytics Tracking ID' (or something similar)
  • Click in 'variable configuration' to set it up
  • Scroll down and pick 'Google Analytics Setting' as the variable type

  • Enter the Tracking ID you created in Step 3 and click 'Save'


This has set up your Tracking ID as a variable that can be used all over Google Tag Manager.
If you did want to create more than one Tracking ID (for example, as Jeff's original article suggested, one for Tableau Online dashboards, one for Tableau Public dashboards etc.) this is where you'll do it.

You should now be back at the dashboard with your new variable name sitting under 'user defined variables'.

Next, we need to configure our tag which will fire when the web page is opened and the GTM tracking code is activated.
  • Click 'tags' on the left side of the dashboard
  • Click 'new' to create a new tag
  • Name your tag - I called it 'Tableau Analytics Views Tag'
  • On 'tag configuration', click and select 'Google Analytics - Universal Analytics'.
  • Leave the track type as 'pageview' 
  • Under 'Google Analytics Settings' you'll see the variable name you just created (which holds the tracking ID code).  Pick this.


That's set up the tag, which says what happens when the page is viewed - it'll signal out an event to our Tableau Analytics Tracking ID.

Now we need to set up the trigger. This is what tells Google Tag Manager when to act for this tag.
  • Click under 'triggering' to set up a new trigger
  • Click '+' in the top right to create a new trigger event
  • Call the trigger 'Tableau Analytics Page Viewed'
  • Choose the trigger type of 'Page View'

We don't want this trigger to fire on every page view, as that'd mean it'll record hits when anything on the website is visited. We just want to see when Tableau dashboards are activated, so we'll qualify when the trigger is activated with the settings:
  • Select 'Some page views'
  • Change the setting in box 1 to 'Page path'
  • Change the setting in box 2 to 'matches RegEx'
  • In box 3, enter a regular expression that defines the pathname we used for our site
    • In my case this is \/analytics\/(.*)
    • If you've used a different pathname, just use that pathname
    • This is a regular expression, so you need to use a \ character before the / to ensure it's read as /analytics/
    • If you're not a regular expressions guru, this is saying that the trigger will fire whenever the page pathname contains /analytics/ anywhere.  That's why we need to use a unique directory name that doesn't appear anywhere else in your website in step 1.


  • Save this trigger and then save the Tag page as well
That's all the configuration you're going to have to do now.  What Google Tag Manager should now do is:
  1. When a file on your website with Google Tag Manager code in it is opened, it wakes up
  2. If the file has the pathname /analytics/ it will send the page information to Google Analytics, under the Tracking ID Code you provided for Tableau Analytics.
What this means, is that you can now track any Tableau Dashboard you publish with Google Analytics, just by creating a copy of that template code file you created in step 2 and giving it a new name.

How to publish a new dashboard with unique Google Analytics tracking


We've essentially moved all the configuration of Tracking IDs that Jeff's article required for each individual dashboard into a single configuration step that you do just the once.  Now, when you create a new Tableau Dashboard, there's just two steps to take.
  1. Take a copy of the template GTM code file and call it the name of your new dashboard
  2. Exactly as per Step 4 in Jeff Shaffer's original article, create a web object somewhere on your dashboard. Set the URL to be the filename you've just created (in full, with your site name)
Note - if you don't have SSL enabled on your website, I found that Tableau pops up an error message that's visible to the user. This doesn't happen with SSL websites, so make sure your website server is set up with an SSL certificate and give it the full https:///analytics/.html path to look at.

That's it.  Now, whenever the web object on your dashboard is activated, Google Analytics will register a page view hit to the tracking ID.

By using a different filename for each of your dashboards (or if you wanted to, for each page within a dashboard), you're able to distinguish which dashboard (or page within it) has been viewed by the URL name that's activated in Analytics.

This means you can set up Google Analytics filters and tools etc. to start differentiating, but it certainly reduces the admin overhead on publishing trackable Tableau Analytics dashboards to simply renaming an html file on your website.

As the Google Tag Manager code doesn't require a new code insert each time, there's no coding.  Even if you did want to set up separate tracking IDs, you can do all of that in Google Tag Manager, without any need to edit the underlying code files.  That's the main change from Jeff's original article which I'm proposing here.

What will I see in Google Analytics?


Exactly as Jeff's original article suggested, you have the full power of Google Analytics now to play with.  The key differentiator for your dashboards will be the URL filename.  Here's an example of the standard realtime display for my Tableau Public visualisations. By switching to the content view, I'm able to see which dashboard has been activated on the basis of its URL pathname.





Other steps to improve the quality of your Google Analytics reporting


One thing you'll quickly find with this method is that every time your web object is triggered (on your own machine, in development or live), the event will fire.  I'm sure there are some snazzy Google Tag Manager ways to avoid that which I'm not aware of.

There's also the question "How many of these hits are me looking at it, and how many are 'real' viewers?'.  As Tableau Public only counts overall views (including your own), this is where Google Analytics can be really powerful in helping you see who's looking at your visualisations.

This is where I should introduce Carlos Escalera's blog posts. I came across Carlos' website some time ago and he provides superb advice on how to remove all that spam from SEO clickbait bots and crawlers from your Google Analytics stats.

Part of this is being able to filter out your own IP address, so you can effectively hide your own views. This works for the Tableau Dashboard tracking above.

If, like me, your internet provider gives you a Dynamic IP address this is a bit hard to do, but Carlos' recent blog post is an excellent description of how to use Google Tag Manager to set a cookie that lets you filter out all your own views in Google Analytics.

I've tried it, it's a great primer on setting up Google Tag Manager too and you can add all the steps in his article to the configuration described above pretty easily.

In Google Analytics, set yourself up different views with the filters applied and you can easily switch between all views, your views and 'real people' views.

It's tremendously powerful and I'm well aware I've only scratched the surface.  Hopefully this blog helps others publishing Tableau dashboards to make use of this toolset more effectively.

The question it doesn't help with is "why is nobody looking at my visualisations", but if you can help me out by taking a peek at my Tableau Public visualisations, that'd be handy. After all, you made it this far.




No comments:

Post a Comment