Hub

How to View Referring Twitter Users as a Custom Dimension in Google Analytics

8 min read by Simon Thompson 13 Oct 2017

|||||||||||
t.co is the link shortening service Twitter uses to wrap all URLs within Tweets.

Update: We’ve had a few people concerned about whether this potentially breaches GA’s T&C’s on PII being pushed into Google Analytics. It seems to be a grey area, which we’re investigating, but until then please just be aware.

If you’ve ever had somebody visit your site from Twitter, chances are that you’ll be familiar with the t.co domain showing up in your Google Analytics. t.co is Twitter’s shortening service which they use to wrap outgoing links, to provide functionality such as malicious-content screening and useful analytics stats to their users.

Recently, I got wondering — wouldn’t it be nice if we could make use of these for something useful within our Analytics?

After a bit of experimenting with Twitter and writing some code, I’ve ended up with this: Custom Dimensions within Google Analytics, which allow you to segment data by the referring Tweeter just like any other Dimension.

Why would we want to do this?

  1. We can answer questions like “How much revenue was driven by @Simon_JThompson on Twitter?”
  2. We can find Twitter influencers who are actively driving traffic to our website.

This post is a guide to how you can get this set up on your website, at no cost.


Prerequisites

I’m making the assumption that you already have Google Tag Manager on your site, with Universal Analytics set up and operating correctly. If you’re not quite there yet, I recommend checking out Google’s article to get started.

I’ll jump straight into the implementation (which I’ve dubbed “Twitlytics”) and add a bit afterwards on how it works for those who are interested in the technical details, plus a few caveats to be aware of. I’ll try and keep everything as simple as possible and explain what each part does as we go.

There are five components to our setup;

  1. A Custom Dimension in Google Analytics
  2. A Trigger in Google Tag Manager
  3. A Data Layer Variable in Google Tag Manager
  4. A Universal Analytics Event Tag in Google Tag Manager
  5. A custom HTML tag in Google Tag Manager

Implementation

1. Google Analytics – Custom Dimension

To quote the Analytics Documentation — “Custom dimensions and metrics allow you to combine Analytics data with non-Analytics data”. Our custom “Referring Twitter User” dimension will allow us to combine it with other Analytics data, to see things like “Pageviews by Twitter user”.

Create a Custom Dimension in Google Analytics named “Referring Twitter User” with the scope set to “Session”.

 

Make a note of the index of your new Custom Dimension, as shown back in your Custom Dimension list.

Be sure to make note of the index – you’ll need this later!

2. Tag Manager – Trigger

This trigger will fire once the back-end code has found the referring Twitter User, and is what we’ll use to trigger the Google Analytics tag which actually sends the information to Google Analytics.

Create a Custom Event Trigger named “Twitlytics – twitlyticsDone” which fires when an event with the name “twitlyticsDone” is pushed into the dataLayer.

3. Tag Manager — Data Layer Variable

Once our code snippet has found the referring tweet, we’ll push some information about it into the dataLayer so it’s ready to be sent off to Google Analytics.

Create a new Data Layer Variable named “Twitlytics – Referring Tweeter” with the Data Layer Variable Name set to “twitlyticsTweeter”.

4. Tag Manager — Universal Analytics Event Tag

Once the back-end has found the referring tweet and pushed it to the dataLayer and fires the twitlyticsDone trigger we set up, we send an event to Google Analytics with our tweet data set as a custom dimension.

Create a new Universal Analytics Tag within Tag Manager named “Twitlytics – Track”, with the following settings:

  • Track Type: Event
  • Category: Twitlytics
  • Action: Track
  • Label: {{Twitlytics – Referring Tweeter}} (the variable we created earlier)
  • Non-Interaction Hit: True

Underneath “More Settings” (click “Enable overriding settings in this tag”) set a Custom Dimension. Set the Index as 1 & the Dimension Value as {{Twitlytics – Referring Tweeter}}.

Finally, set this tag to fire on the Twitlytics – twitlyticsDone trigger.

 

5. Tag Manager —Custom HTML Tag (Script)

This is the code which will be triggered on each page load, and will call our back-end if it detects a t.co referrer.

Create a Custom HTML Tag within Tag Manager named “Twitlytics – Lookup” which uses the “All Pages” Trigger, and add the code snippet below.

 

This calls a version of the back-end which I’ve hosted as a proof-of-concept for you to use, but with no assurances as to it’s availability long-term. If you’d like to self-host a copy, the code is freely available over on GitHub.

6. Test

To test this is working as expected, either ensure you’re in Preview Mode within Tag Manager, or Publish your changes (not recommended if you’re on a live site!).

The easiest test is to tweet a link to your site, and click it. If you watch the realtime Analytics Events, you should see the following event (with the Category of “Twitlytics”).

 

After a brief period of time, you should start being able to test the Custom Dimension itself.

And that’s it!


Technical Detail

The main reason this works is because Twitter generates a different t.co link for each unique user that shares a URL. For instance; if I share this post on Twitter, the t.co would be different than if you were to share it.

All our back-end (a small Node.JS API) is doing is querying the Twitter API to find the oldest tweet mentioning the referring t.co (excluding retweets) and giving back that data as JSON.

After that, it’s simply a case of getting that data into Google Analytics in a useful format, which comes in the shape of Custom Dimensions.

Worth noting is a huge credit to Simo Ahava, as it was what he describes in this post that was the final piece of the puzzle to get the data into Google Analytics.

Caveats

  • Twitter API Bottlenecks   The Twitter API is rate limited, so will if there’s a lot of lookups to be done, our back-end will start failing. The back-end does try and help by caching t.co URL’s it’s already looked up though.
  • No backdated data   This method won’t allow us to apply data retrospectively to Google Analytics.
  • 7 Day Limit   The Twitter search API doesn’t go back further than 7 days, so if it takes longer than 7 days for the first person to click through from a tweet, it won’t be tracked. (I’m working on a potential fix to this — but let me know if it’s a big deal for you in the meantime!).

If this has been helpful to you or you’re using Twitlytics, I’d love to hear about it! Let me know via Twitter or drop an email to simon@strategiq.co.

Let's talk strategy
Drag Read