Skip to content

Instantly share code, notes, and snippets.

@kressaty
Created October 9, 2012 15:37
Show Gist options
  • Save kressaty/3859584 to your computer and use it in GitHub Desktop.
Save kressaty/3859584 to your computer and use it in GitHub Desktop.
Twitter Button Google Analytics
// This assumes that you have included the Tweet button via the standard Twitter JS include
// Thanks to Google for most of this code, see the Social Interaction Analytics guide at
// https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSocial
// To track a Tweet, subscribe to the tweet event (note the additional function required
// to determine the query parameter being sent to Twitter):
function trackTwitter(intent_event) {
if (intent_event) {
var opt_pagePath;
if (intent_event.target && intent_event.target.nodeName == 'IFRAME') {
opt_target = extractParamFromUri(intent_event.target.src, 'url');
}
_gaq.push(['_trackSocial', 'twitter', 'tweet', opt_pagePath]);
}
}
twttr.ready(function (twttr) {
twttr.events.bind('tweet', trackTwitter);
});
function extractParamFromUri(uri, paramName) {
if (!uri) {
return;
}
var regex = new RegExp('[\\?&#]' + paramName + '=([^&#]*)');
var params = regex.exec(uri);
if (params != null) {
return unescape(params[1]);
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment