Using Google Spreadsheets to combine Twitter and Google Analytics data to find your top content distributors

When you share a link on Twitter there are a number of services, like, which allow you to track the impact of the url in terms of the number of clicks it attracts from other users. At the same time there are a number of ways to monitor people sharing links to your site, the most basic being using a Twitter search like this one for Using these search results you could start extracting the follower information from the person tweeting, workout potential reach and so on, but wouldn’t you like to know, as with your own account, how many visits someone else’s tweet generated to your site? Fortunately there is a way to do this and in this post I give you two tools to help you do it and look at why this information might be useful, but lets first look at how it is possible.

Referral Traffic

In August 2011 Twitter started automatically wrapping links over 19 characters in it’s own shortening service, later in October started wrapping all links in When you start navigating around the web at each page you visit the server generally knows which page you came from (exceptions include direct traffic and when coming from https://). When you click on a link the site can track where you came from, known as referral traffic (interestingly it looks like Twitter also bypass your url shortener of choice by following the redirects until it reaches a final destination and use that link in the redirect). So when you click on a link posted on Twitter I can detect that, that’s where you came from. Furthermore, each time a person tweets a link it gets wrapped in a unique url even if that url has been shortened before with the exception being new style retweets. This means that when someone clicks on a link tweeted by someone I can trace it back to a single person.
Lets see how this works in practice. When I fire up my Google Analytics account and look at referral traffic I can see it’s dominated by sources. Drilling down into the data I can actually see how many visits each link generated.
Referral source Referral path
Searching Twitter for say allows me to trace it back to this tweet:

So we can say this tweet and it subsequent 5 retweets generated 42 visits to my blog. At this point you might be saying but this tweet above has a link, there’s no reference to It may say but underneath the hyperlink is
Tweet html source
For some this isn’t news. In fact, Tom Critchlow was writing about how Twitter’s link shortening service is game changing – here’s why way back in August 2011. His post probably has a better explanation of what is happening and also includes a bookmarklet (appears broken after recent Analytics overhaul) which takes the referral path from your Google Analytics report and searches it on Twitter to find out which persons tweet is sending you the traffic.
I thought this was a neat idea but wanted to get the full impact of seeing the visit count associated with a named person. I tried a couple of ways to inject the data using a bookmarklet with no joy so turned to Google Spreadsheets (and in particular Google Apps Script) to marry data from Twitter with Google Analytics. So I give you:

Method 1: Quick 7 day search

*** Twitter/Google Analytics Top Distributor Sheet v1.0 ***
[File > Make a copy for you own version]

With this Google Spreadsheet I can authenticate with my Google Analytics account which then allows me to extract data using the Google Analytics Core Reporting API. I then pass each link to the Twitter Search API to find out who tweeted it first. This is all wrapped in a custom formula getGATwitterRef(startDate, endDate, optional numberOfResults) which generates a table of results like this:
Twitter/Google Analytics Top Distributor Sheet v1.0
So big thanks go to Brian E. Bennett (@bennettscience) for generating 16 visits, Alberto Cottica (@alberto_cottica), @futuresoup and others for also generating traffic. But who generated the 14 visits? The reason this row is blank is while the link is still generating traffic to my site the link was first tweeted over 7 days ago meaning it is outwith the capabilities of the Twitter Search API.
What a terrible shame never mind 9 out of 10 isn’t bad … ah but hold on I’ve got a Google Spreadsheet template that can archive Twitter searches (TAGS). So I give you:

Method 2: TAGS v4.0 with Google Analytics integration

*** TAGS v4.0 with Google Analytics integration ***
[File > Make a copy for you own version]
Update: 11th Sept. 2014 TAGS has moved on and this method might no longer work

So by using a search term for your domain you can start collecting ten of thousands of tweets over months and years, which you then query against your Google Analytics data.
‘What the flip’ you might be asking. Here’s the explanation. By using the same search query at the beginning of this post for all tweets containing ‘’ (and because Twitter is wrapping everything in it knows these even if they began life as a or I can build up a corpus of tweets containing links to my site. If you also look at this archive I’m building you’ll see in the column labelled ‘text’ there is not a or in sight, all the links are
So all I need to do is extract a referral path I’m interested in from my Google Analytics data and find who first tweeted it in my archive giving me the number of visits that link generated.
So now I can say thank you @BillMew for the mystery 14 visits, thank you @TweetSmarter for the 38 visits generated from your tweet last month (you’ll see some #N/A I think because my collection went offline for a bit, working sweet now)
TAGS v4.0 with Google Analytics integration


Whilst there could be a seriously creepy side to this (lets not forget people like Google have made serious bucks knowing where you go and what you share), there are a couple of reasons why I was interested in following up this concept. One was in relation to the Learning Registry/JLeRN experiment (background info here) which is trying to create a framework beyond metadata to include activity/paradata around resources. The idea is this data can be used to provide feedback and improve discoverability of resources. So potentially you’ve got some rich data to push into a node .. errm I think, maybe #haventplayedwiththespecyet
Another thought was during the OER Visualisation project I discovered that social sharing of resources appears, insert caveats, to be rare. If you could recognising and reward the people pushing content it might encourage them to distribute more (and as I highlighted in How do I ‘like’ your course? The value of Facebook recommendation there is real and measureable value in people distributing information through their networks) – the flip side to this being as soon as you start measuring something someone else will start gaming the system.
There’s also a degree of profiling you could do. If someone ends up at your resource having clicked on a linked shared by A that person may have something in common with them so you could target additional resources to them based on what A might like.
I’m sure there are others. As I’ve shared the tools to do this it’s only fair that you share your ideas in the comments.

But there is potentially more …

Data Feed Query ExplorerI will leave you with one last thought. I haven’t mentioned much about the code, which is available and open source (my bits anyway) via the Script Editor. To help construct the Google Analytics query I used the Data Feed Query Explorer. Here’s a permalink to the main query structure I used. If you open the link and hit the ‘Authenticate with Google Analytics’ button choosing one of your own analytics ids you can see what data comes back. I’ve been conservative only pulling what I need but if you click on the ‘dimensions’ box you can see I could also be pulling where the visits were coming from, time of day, and more. All potentially valuable intelligence to give you a picture of how a resource is being shared if you can unlock it of course.


Join the conversation

comment 21 comments
  • Jaspal

    Martin, what can I say…magic!
    BTW I got odd behaviour from GDocs when trying to run the scripts first time – I had to open the onOpen script in script editor and run it before GDocs ‘recognised’ any of the other scripts. All running fine now though .Thanks!

    • Martin Hawksey

      Thanks Jaspal. Surprised social media metric sites haven’t picked up on this yet. Postrank already have authorised access to my GA account, so they have access to the data they just need to glue together.
      (Haven’t worked out solution for the onOpen issue, might have to dig out documentation on the onInstall function)

  • Su Butcher

    Hi Martin,
    This is marvellous. I’ve been using your TAGS template after finding that ‘Twapperkeeper’ (awful name) was being bought out by Hootsuite and would be a paid for service, and as you know your advice has helped us sort out ArchitectMap too.
    As a complete novice to coding and scripts (though enthusiastic!) I shall take a look and see if I can make this work for me. My motivation is thanking people on twitter who help you, and helping them help you more. Finding helpful people on twitter is one of my hobbies!

  • Johan Myrberger

    Really cool stuff, thanks! Will for sure give it a try, no doubt it will be useful.
    On a related side-note; when using earlier versions of the TAGS spreadsheets, for popular tags, I run into issues when the spreadsheet gets too large. both when the “number of cells”-limit is reached (of course), but also (as it seems) when you get a large spreadsheet, but still with space left. Have you noticed this from your end? Any ideas of good workarounds?

    • Martin Hawksey

      Hi Johan, I don’t normally have a problem but I’m usually only interested in small data. On the occasion that I’ve needed to do this I download the data and clear the archive then merge the data offline (usually in excel)
      Something I keep meaning to do but have never got round to doing is pushing the data into Google Fusion Tables which is possible with Apps Script.

  • Johan Myrberger

    Hm, trying the template now…
    For some reason I get an error in the sheet TopDistributors (cells B5 and B18) stating “error: Authorization is required to perform that action.”
    The GA-authentication (and connection to specific profile) seems to have gone well though. Not sure where to start trouble-shooting, any quick hints?
    (And pouring the data into Fusion Tables would be nice for sure)

    • Martin Hawksey

      @Johan that error message is caused if there’s no GA-authentication – only thing I can think of is something went wrong internally but if you are able to select the GA profile you want to use it suggests everything is working … hmm not sure, my default fall-back is make a fresh copy and try again. If you figure what went wrong it would be useful to know

      • Martin Hawksey

        @Johan – experiencing a similar behaviour – I think it’s because the results are cached. I closed the spreadsheet and reopened later and it was all working fine

  • Johan Myrberger

    Oh well, it’s working for me as well at the moment (although I did close and reopened the spreadsheet a couple of times when I first had the issue it didn’t work, but now when I tried it works like a charm..)
    I started to think that it was due to the GA account I hooked up with was not “owned” by me, only shared with me, but apparently that was not the issue…
    Have a nice weekend!

  • Sheila’s work blog » @jisccetis - how others see us

    […] change number 1, @jisccetis is following people now. Armed now with our found new twitter and google intelligence, we have a much clearer idea of who is retweeting our posts and driving traffic to our blogs and […]

  • RSS Feed Social Share Counting Google Spreadsheet JISC CETIS MASHe

    […] and page view counts for out blog, category or project feeds. The solution was a quick add-on to Using Google Spreadsheets to combine Twitter and Google Analytics data to find your top content dist…, recycling some other bits of code I had lying around (mostly from Introducing a RSS social […]

  • Tony Hirst

    “because Twitter is wrapping everything in it knows these even if they began life as a or”
    I just did a test on this: I think urls map onto unique (user,url) pairs for original tweets/link sharing (not sure what happens if I RT someone and cut/replace an original link? I suspect my new link will be used?), so if I tweet a link to, or a bitly shorturl to, they will receive different links.
    The Twitter search searches over resolved URLs, so it will turn up a tweet containing a link shared to as well as a teet containing a shorturl that points to
    (I guess you know this – I’m just clarifying for myself…!)

    • Martin Hawksey

      yes Twitter appears to follow redirects and records the destination (which doesn’t appear to be publicly accessible via the api), the then points to the to the url submitted by the user.
      Something I hadn’t noticed until now is in the entities data returned (example below) it would be easy for a twitter client to replace a with the original link. I’m not aware of any clients doing this and I imagine if twitter found out they would be shutting their api access sharpish.

  • Martin

    for some reason the google docs files are only opened on view only mode.
    Something with permissions?

  • Martin

    I downloaded and uploded the file again to my google docs , so it is working, but I the meny items “TAGS” are not showing and there light blue button is non exist as well. be glad if you can assist.

Comments are closed.