The analytics issue with Twitter

January 19th, 2009 – 71 Comments

Twitter is great for traffic, but just how great it is, well, you’ll never really know. Why not? You’ll ask. Well, the only traffic you’ll see coming from Twitter is traffic that comes through Twitters web interface. And most people I know don’t use the web interface, they use desktop and / or phone clients. Because these clients aren’t browsers, they won’t pass along a referrer, and thus register as “direct traffic”.

I won’t even try to estimate what percentage of Twitter usage goes through the API, although I’ve heard people say it’s up to 80 or even 90%, although Twitter Stats seems to report it as around 50%, and as 60%. So you’re getting a lot of clicks that look like “direct traffic” in your Analytics, but are in fact, not direct traffic. These people will behave differently than normal people who come to your site directly.

In an effort to improve the reliability of my Google Analytics stats a bit, I’ve decided to start adding Google Analytics campaign, medium and source variables to all my own tweets in which I promote my own posts. I know from, the URL shortening service I tend to use, that my tweeted links get up to 300 clicks each, and I’d love to track the behavior of those people on my site.

So I’ve made a small “in between” script that I can use in a bookmarklet. What it does is simple:

  1. it grabs the URL and title from the request
  2. it adds the UTM variables ?utm_campaign=twitter&utm_medium=twitter&utm_source=twitter
  3. it does a request to through its API to create a shorturl
  4. it then redirects to with the status set to that shorturl

Download the script here, and then modify this bookmarklet to use it:


Set to the domain name and path where you uploaded it.

This should allow you to create shorturls with the campaign tags in it, and thus track those tweeted links in your own analytics!

71 Responses to The analytics issue with Twitter

  1. Shrinagesh
    By Shrinagesh on 27 November, 2009

    I’ve been looking for a solution to properly track twitter traffic and stumbled on this post from google. Though I am a frequent reader here, I somehow missed this post. I will check it out to see how this works for us. Thanks Joost

  2. Ashoka
    By Ashoka on 21 November, 2009

    Great information as alway, I am going to try this on my site because it’s almost impossible to track twitter traffic as you have so correctly pointed out.
    Thanks again.

  3. Paul Patrick Dale
    By Paul Patrick Dale on 20 October, 2009

    Thank you Joost! I only just arrived at a point where I can use something like this.
    You’re generous to share such great tips and tricks.

  4. Cricket Maniac
    By Cricket Maniac on 8 September, 2009

    I thought there would be some tracking for those visitors who are visiting my twitter profile

  5. J. K. Taylor (OneTouch Massage)
    By J. K. Taylor (OneTouch Massage) on 25 August, 2009

    Thanks, Joost! I was baffled as to why I didn’t have more Twitter referrals showing up in Google Analytics. Even my cligs analytics showed more than Google did. You always have the answer for those little things that bug me.

  6. India
    By India on 12 June, 2009

    Nice bit of info. :-)

  7. Tad Reeves
    By Tad Reeves on 13 May, 2009

    Now that’s a pretty slick idea.

  8. Craig
    By Craig on 5 May, 2009

    FWIW: I wrote a plugin for Mint that helps you track just these kinds of campaigns in realtime:

  9. Cpatain Fancypants from CDU the band

    Hi, the script looks good as the shortening URL I use says hundreds of hits to my site, but the site is telling me it is only finding b/w 10 15 a day. As a beginner, where do I place the script to make this all work?

  10. David Whitehouse
    By David Whitehouse on 28 April, 2009

    I would say you would be better off specifying:
    utm_medium = referral
    utm_source =
    utm_campaign = Tweets

    That way you can lump all the referrals together appropriately, lump all the referrals together and just analyse it based on “Tweets”

  11. Andrew
    By Andrew on 13 April, 2009

    Here is how to do it with a simple bookmarklet:

  12. Nikolai
    By Nikolai on 8 April, 2009

    Joost, this sounds like a cool hack. I just have one question:

    If people tweet a direct link to your site, without using the bookmarklet underneath the post, Google Analytics will still display that as direct traffic, right?

    If so, is there a way to fix that as well?


  13. Jon Henshaw
    By Jon Henshaw on 19 March, 2009

    Joost, this is now supported in the URL link shortener

  14. Gary
    By Gary on 13 March, 2009

    Note, if your using the latest google analytics you don’t need to set those variables at all. Instead create your own, for example ?track_id=5 and maintain a database to lookup and set the tracking info yourself following the conventions here

    You can even go ahead and set a cookie to track browsers you have already run the code on and only run it once per browser.

  15. jon perry
    By jon perry on 11 March, 2009

    It’s a basic question and I hope I didn’t overlook it, but to be clear, I am saving the text file as glig.php in the webroot directory. Where am I putting the javacode? In the theme index page?



  16. oneighturbo
    By oneighturbo on 28 January, 2009

    just came across this site, thanks joost. great stuff and this will be a great addition to our twitter unknowns!

  17. php
    By php on 26 January, 2009

    funny that you gave the code to the google dudes. hope they use it. :-) heeheee

  18. Brian St. Pierre
    By Brian St. Pierre on 23 January, 2009

    Joost, thanks for a great solution to twitter referrer stats!

    As implemented, a visitor may end up bookmarking (delicious, digg, etc) the URL with the campaign stuff in it. This would mess up your stats. To avoid this, include this script below the Google Analytics script. It modifies the browser history. The reason the RegExp is complicated is becase, as Joe points out above, there may be other parameters in the URL that you want to preserve. Disclaimer: I haven’t widely tested this but it does work in FF2 (linux), FF3 (win32), IE7 with a handful of URL variations. Also, my javascript is weak so there may be a better way to implement this.

    // Show a clean URL if this came from a tweet/ URL.
    var campaignRe = new RegExp(/([?&])utm_campaign=twitter&utm_medium=twitter&utm_source=twitter(&?)/);
    var loc = new String(window.location);
    var match = campaignRe.exec(loc);
    if(match != null) {
    // Remove the crufty URL from browser history.
    if(match[1] == "?" && match[2] == "&") {
    // Change & to ?.
    var cleanLoc = loc.replace(campaignRe, "?");
    } else if(match[1] == "&" && match[2] == "&") {
    // Preserve &.
    var cleanLoc = loc.replace(campaignRe, "&");
    } else {
    // Just drop the params.
    var cleanLoc = loc.replace(campaignRe, "");

    @Gif Dump: does this solution change your opinion about “bad SEO written all over it”, or is there still some problem I’m not seeing? Can you elaborate about what you mean? The crufty URL is cloaked by so I don’t see the issue.

  19. JoSe
    By JoSe on 23 January, 2009

    Sorry for sucha a noob question, but where do I place the bookmarklet ?

  20. Karri
    By Karri on 21 January, 2009

    The websites I manage use OneStat, is there a way to track Twitter usage on that platform?

  21. Gif Dump
    By Gif Dump on 21 January, 2009

    Easier way is to just send out your link with the tag ?twitter – so

    Same process but lessens the code soup you’re adding at the end – easily will be recognizable in your analytics as traffic from twitter. But both ways have bad SEO written all over them.

  22. Brad
    By Brad on 20 January, 2009

    For an interesting look at follower counts I set up a website that I planned to promote solely through Twitter,, a daily music thing. Admittedly there are a few things popular twitter users do that I don’t: I’m not big on conversation (though I respond and try my best I don’t strike up convos) and my content is very much “you can look once and determine if you care to click”… But I’m up to 630 followers now and I get about 20-30 actual visits a day. I have very little (almost 0) search engine traffic even after unblocking them in my robots.txt. Since this a post about analytics and if Twitter generates real traffic I figured I’d share my “high follower count” story.

    Followers != Clicks.

  23. Alex
    By Alex on 20 January, 2009

    Very interesting insight, will try that out! Thank you very much!

  24. Ricky Buchanan
    By Ricky Buchanan on 20 January, 2009

    Do these parameter additions not mess somehow with the tweetbacks code? I’m already having trouble with it – don’t want to court more trouble!

    Also, how do you get your RSS tracking into the RSS feed? Both seems to be very useful techniques.

  25. Aaron Peters
    By Aaron Peters on 20 January, 2009

    Very nice, txs Joost!

  26. Paul
    By Paul on 20 January, 2009

    Great idea! Twitter traffic can (and should) be measured properly with the right approach.

  27. Joe Dolson
    By Joe Dolson on 20 January, 2009

    You may want to make a minor revision to this code – you’ll need to check whether the URL already has parameters.

    One simple option:

    if ( strpos( $url,"?" ) === FALSE) {
        $url .= "?";
    } else {
        $url .= "&";
    $url .= "utm_campaign=twitter&utm_medium=twitter&utm_source=twitter";
    • Joost de Valk
      By Joost de Valk on 20 January, 2009

      Thx a lot Joe, adapted it!

  28. Joe Spencer
    By Joe Spencer on 19 January, 2009

    Great solution for tracking tweet’d links, I´ll pass it on.

  29. review plugin wordpress
    By review plugin wordpress on 19 January, 2009

    great site. thanks for the info.

  30. Steve
    By Steve on 19 January, 2009

    Pretty straight forward campaign tracking for GA – I like the automated solution however.

    I guess the flaw is when people link to your campaign tracking URL or bookmark it. The source is then no longer accuate. This could be solved in the case of people posting links using advanced segmentation etc. but do you have any ideas how to solve the bookmark situation?


  31. Adrian Eden
    By Adrian Eden on 19 January, 2009

    Very powerful indeed, nice post, its good to see what people are looking at and where they came from.

  32. Anthony Parkes
    By Anthony Parkes on 19 January, 2009

    So you can actually track what those specific people do on your site? wow, that’s pretty powerful… hat’s off Joost

Check out our must read articles about Analytics