Skip to content

Instantly share code, notes, and snippets.

@blafrance
Forked from kressaty/ga-twitter.js
Created November 11, 2012 04:50
Show Gist options
  • Save blafrance/4053759 to your computer and use it in GitHub Desktop.
Save blafrance/4053759 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