
One of the questions we are often asked is how we track various social media sites users and their interaction with sites the answer is in a variety of ways but here is just one of the techniques we use with Google Analytics.
Please note: through out this article we use the new ga.js script rather then urchin if your still using the old code their is not much to change
Campaigns
Google Analytics offers a way to track marketing campaigns by giving them unique markers, these are normally part of a URL for example
example.com?utm_campaign=mycampaign &utm_medium=banner&utm_source=otherdomain.com
In the above example we have a global campaign the type of link it was and where it came from, this is all well and good but it requires that we create a tracking URL to submit to our social media which is not likely to happen in an “organic” submission so we need to come up with an alternate solution.
Embedding Campaign details in page
Google Analytics is pretty flexible and so allows us to embed campaign tracking information directly into a page rather then a link.
<script type="text/javascript">
var pageTracker = _gat._getTracker("YOURCODE");
pageTracker._setCampNameKey("mycampaign");
pageTracker._setCampMediumKey("banner");
pageTracker._setCampSourceKey("otherdomain");
pageTracker._setCampNOKey('ga_nooverride');
pageTracker._initData();
pageTracker._trackPageview();
</script>
This is great for following users arriving on landing pages for instance, but in our use we want to add this campaign information only when they arrive from specific sites and so we are going to need to capture the referral information.
Social Referrals
Using PHP we can get referral information about how the user arrived on the page using HTTP_REFERER this interrogates packets sent by the browser for referral information. This technique is not hugely accurate users can manipulate such information and some browsers don’t send any referral information at all. But for what we are doing which is taking snapshots of social media users it will do the job perfectly. Once we have where they come from we need to compare it to our list of social media sites and then add our tracking details as needed.
/* Social Media Campaign organiser
For more info: http://blog.venture-skills.co.uk/2007/12/22/social-media-tracking-ga/
By Tim Nash */
/*Gather Referer info and find domain*/
$url = $_SERVER['HTTP_REFERER']; // Get referer string - remember not accurate!
$domain = parse_url($url, PHP_URL_HOST); // Set domain as the Host of the referring URL
/*Select social media sites to track from*/
$social = array("stumbleupon.com","digg.com","mixx.com","sphinn.com");//Set Social sites array
/*Set campaign if a social media site referal*/
if (array_search($domain,$social)) { //search the array
echo "
pageTracker._setCampNameKey('social_media');
pageTracker._setCampMediumKey('".$domain."');
pageTracker._setCampSourceKey('".$url."');
pageTracker._setCampNOKey('ga_nooverride');
"; //add campaign details
}
?>
So in the above example code we have created a campaign called social media, the medium is set as the domain of the social media site and the source the exact url. You could also add an ID if you have several pages which are part of the same linkbait campaign.
Complete code
var gaJsHost = (("https:" == document.location.protocol) ?
"https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost +
"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
var pageTracker = _gat._getTracker("YOURCODE");
<?php
/* Social Media Campaign organiser
For more info: http://blog.venture-skills.co.uk/2007/12/22/social-media-tracking-ga/
By Tim Nash */
/*Gather Referer info and find domain*/
$url = $_SERVER['HTTP_REFERER']; // Get referer string - remember not accurate!
$domain = parse_url($url, PHP_URL_HOST); // Set domain as the Host of the referring URL
/*Select social media sites to track from*/
$social = array("stumbleupon.com","digg.com","mixx.com","sphinn.com");//Set Social sites array
/*Set campaign if a social media site referal*/
if (array_search($domain,$social)) { //search the array
echo "
pageTracker._setCampNameKey('social_media');
pageTracker._setCampMediumKey('".$domain."');
pageTracker._setCampSourceKey('".$url."');
pageTracker._setCampNOKey('ga_nooverride');
"; //add campaign details
}
?>
pageTracker._initData();
pageTracker._trackPageview();
Understanding Reports
Rather then explaining how to access your campaign reports I’m simply going to point you to the expert at Epikone who have written a fantastic series on using campaigns (though never to track social media site users) and it would be silly to replicate their work to a lower standard.
What else can you do
The above code is pretty simple to modify, to tag users with a cookie, to redirect certain user groups and with a couple of minor changes can be used with clickheat maps or any other analytics program. One option is to uniquely identify users across your analytical scripts but be careful to declare such activities in your privacy policy and your P3P document.
Subscribe to The Venture Skills Blog by Email
All our Posts are audio subscribed for more information see here, and to access the podcast feed here
This blog is moving soon, make sure you move with us by using our Feedburner RSS feed, if you have used the autodiscovery button in your browser you may need to swap feeds, simply delete the old feed and add, http://feeds.feedburner.com/VentureSkills For a more detailed explanation on feeds and recieving our content in various formats click here

Subscribe to our Odiogo Audio Feed



December 24, 2007 at 10:35 am
Nice!
December 27, 2007 at 2:41 pm
Excellent idea, very clever! One question, this line here:
$social = array(”stumbleupon.com”,”digg.com”,”mixx.com”,”sphinn.com”);
Do you simply add whichever social sites you want, the ones that you want to define as part of the campaign meaning you can basically put whatever websites you would like to?
Thanks and well done!
Jesse
December 27, 2007 at 3:05 pm
Great post and thanks for the detailed information, will definitely look into this one further
December 28, 2007 at 11:46 am
Cheers Glen, the code is pretty easy to understand and once you start you will find more and more things to do with campaigns
December 28, 2007 at 12:48 pm
@jesse sorry for the delay you were caught in the spam filter
Yes just edit the line to include the domains you wish to track users from, I included a few to get you started.
December 28, 2007 at 1:50 pm
Hey Tim,
Nice information man, I will be putting it to some good use soon!
December 28, 2007 at 6:48 pm
[...] How to track Social Media Users with Google Analytics “One of the questions we are often asked is how we track various social media sites users and their interaction with sites the answer is in a variety of ways but here is just one of the techniques we use with Google Analytics.” [...]
December 28, 2007 at 9:43 pm
[...] Venture Skills shares a social media tracking hack for Google Analytics. [...]
December 30, 2007 at 6:22 am
[...] How to track Social Media Users with Google Analytics “Google Analytics offers a way to track marketing campaigns by giving them unique markers, these are normally part of a URL for example example.com?utm_campaign=mycampaign &utm_medium=banner&utm_source=otherdomain.com” (tags: Google analytics socialmedia statistics) Book Mark it-> del.icio.us | Reddit | Slashdot | Digg | Facebook | Technorati | Google | StumbleUpon | Window Live | Tailrank | Furl | Netscape | Yahoo | BlinkList Sphere: Related Content [...]
January 5, 2008 at 11:12 pm
Hey there -
Thanks so much for sharing this code. Just got it working and couldn’t be happier.
I made a few changes though and wanted to share.
First, I think of the campaign variables a bit differently, and set them as follows…
pageTracker._setCampNameKey(’”.$url.”‘);
pageTracker._setCampMediumKey(’socialmedia’);
pageTracker._setCampSourceKey(’”.$domain.”‘);
Basically, I’m treating “socialmedia” as the medium and the domain as the source, which maps a little more closely to the definitions in the help docs…
“Use utm_source to identify a search engine, newsletter name, or other source. Use utm_medium to identify a medium such as email or cost-per- click. ”
I also had to test on $_SERVER['HTTP_REFERER'] with the following…
if(isset($_SERVER['HTTP_REFERER']))
… as my hosting environment throws an error if it isn’t set.
I’m also stripping off the www from the referrer if appropriate so I don’t have to include both www and non-www versions of the domains in the array…
if (substr($domain, 0, 4) == “www.”){
$domain = substr_replace($domain, ”, 0, 4);
}
Hope that’s useful or interesting to someone.
Again, thanks for sharing.
TL
P.S. Here’s the whole shebang…
if(isset($_SERVER['HTTP_REFERER'])){
$url = $_SERVER['HTTP_REFERER']; // Get referer string – remember not accurate!
$domain = parse_url($url, PHP_URL_HOST); // Set domain as the Host of the referring URL
if (substr($domain, 0, 4) == “www.”){
$domain = substr_replace($domain, ”, 0, 4);
}
/*Select social media sites to track from*/
$social = array(”stumbleupon.com”,”digg.com”,”mixx.com”,”sphinn.com”,”del.icio.us”);//Set Social sites array
/*Set campaign if a social media site referal*/
if (array_search($domain,$social)) { //search the array
echo ”
pageTracker._setCampNameKey(’”.$url.”‘);
pageTracker._setCampMediumKey(’socialmedia’);
pageTracker._setCampSourceKey(’”.$domain.”‘);
pageTracker._setCampNOKey(’ga_nooverride’);
“; //add campaign details
}
}
January 13, 2008 at 1:31 pm
[...] SuccessHReview a CCK example part 1 – ContemplateCCK & Views the ultimate combination – part 2How to track Social Media Users with Google AnalyticsCCK & Views the ultimate combination – part 3Running an ISO as Virtual CD’s in [...]
May 12, 2008 at 10:02 pm
Thanks for the code, I didn’t realize you could do this? We recently ran a campaign in StumbleUpon.com and I would like to exclude this traffic from my original website profile within Google Analytics. Would this be possible using similiar code?