Support » Plugins » [Plugin: Tweetbacks] Hack: Make TweetBacks work with GA tracking tags

  • Joost has written recently about using Google Analytics to track Twitter hits, and the same method works for links in RSS feeds using various plugins.

    The regular Tweetbacks plugin, though, won’t pick up tweets of your URLs with these tracking tags because the shortening services will give a different short URL for your article URL with tracking tags. This is my hack to get around that limitation.

    This hack relies on the fact that the tracking tags always come in the same order. This is usually true because the RSS tracking tags are added by a plugin, and the twitter tracking tags are added by a bookmarklet as described by Joost in the article. We have to assume because of limitations with the URL shortening services. For example take these two URLs:

    The utm_medium and utm_source variables are reversed, but the URLs are functionally the same. Even though these URLs are the same in function, the URL shortening services will give them different short versions. So we have to rely on the source/medium/campaign variables always coming in the same order.

    Now open the tweetbacks.php file. It’s fairly short and easy to understand because Joost has coded it well. Look for line 414 where the function yoast_get_tweetbacks starts – it should look like this:

    function yoast_get_tweetbacks() {

    The first part of the function sets up a whole bunch of stuff, but the rest of it is a bunch of code groups that look like this:

    if (!isset($shorturls['tinyurl'])) {
    $result = $snoopy->fetch("".$permalink);
    if ($result && strpos($snoopy->response_code,"200") !== false && $snoopy->results!="" && strpos($snoopy->results,"") === 0) {
    $shorturls['tinyurl'] = trim($snoopy->results);

    These groups loop through all the RL shortening services and grab the short versions of your article’s URL. You can copy each one and grab the short version of the URL with google analytics variables by altering it like this:

    if (!isset($shorturls['tinyurlrss'])) {
    $permalinkrss = $permalink . "?utm_source=rss&utm_medium=rss&utm_campaign=rss";
    $result = $snoopy->fetch("".$permalinkrss);
    if ($result && strpos($snoopy->response_code,"200") !== false && $snoopy->results!="" && strpos($snoopy->results,"") === 0) {
    $shorturls['tinyurlrss'] = trim($snoopy->results);

    All I’ve done is added this line:

    $permalinkrss = $permalink . "?utm_source=rss&utm_medium=rss&utm_campaign=rss";

    I replaced everywhere it said $permalink with $permalinkrss, this makes it look up the permalink with the tags. Then I replaced each place it had $shorturls['tinyurl'] with $shorturls['tinyurlrss'], so the results get stored in a different place to the regular tinyurl results.

    Then I did the same thing with every other piece that looked like that – copied it, and changed the copy to look up and store the GA tagged version of the short URL.

    I always shorten my twitter URLs with, so after I’d done the above stuff I took a third copy of the section and changed it so it looked up the twitter version of the google analytics tags, as well as the RSS version. The changes are just the same as above except I put “twitter” everywhere instead of “rss”. I’m the only person who adds twitter tags to my URLs, so I’m pretty sure that is the only service that I need to look up for that.

    After I’d done all of this I gingerly put my changed version up instead of the original version and, lo, it worked! It still misses some tweets here and there as I’ve reported before and uses the wrong timestamp for tweetbacks, but right away it picked up a huge bunch of tagged URLs which people had tweeted previously.

    It seems my RSS feed readers tweet articles much more frequently than casual readers, which makes sense – they’re more interested in the site long-term, generally.

    I’m pretty sure this hack is messy and not the best way to go about doing this – I’m not much of a PHP coder and I’m certainly not claiming this will solve all your google analytics + tweetbacks problems! But it’s a start, anyway.


  • The topic ‘[Plugin: Tweetbacks] Hack: Make TweetBacks work with GA tracking tags’ is closed to new replies.