Created
April 8, 2012 13:11
-
-
Save baado/2337240 to your computer and use it in GitHub Desktop.
Tracking Twitter Widget Action with Google Analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ga = _ga || {}; | |
var _gaq = _gaq || []; | |
_ga.trackSocial = function(opt_pageUrl, opt_trackerName) { | |
twttr.ready(function (twttr) { | |
_ga.trackTwitter(opt_pageUrl, opt_trackerName); | |
}); | |
}; | |
_ga.trackTwitter = function(opt_pageUrl, opt_trackerName) { | |
var trackerName = _ga.buildTrackerName_(opt_trackerName); | |
try { | |
if (twttr && twttr.events && twttr.events.bind) { | |
var twttrFunc = function(event) { | |
if (event) { | |
var targetUrl; // Default value is undefined. | |
if (event.target && event.target.nodeName == 'IFRAME') { | |
targetUrl = _ga.extractParamFromUri_(event.target.src, 'url'); | |
} | |
_gaq.push([trackerName + '_trackSocial', 'twitter', event.type, | |
targetUrl, opt_pageUrl]); | |
} | |
}; | |
// tweet | |
twttr.events.bind('tweet', twttrFunc); | |
twttr.events.bind('click', twttrFunc); | |
twttr.events.bind('retweet', twttrFunc); | |
twttr.events.bind('favorite', twttrFunc); | |
twttr.events.bind('follow', twttrFunc); | |
} | |
} catch (e) {} | |
}; | |
_ga.extractParamFromUri_ = function(uri, paramName) { | |
if (!uri) { | |
return; | |
} | |
var regex = new RegExp('[\\?&#]' + paramName + '=([^&#]*)'); | |
var params = regex.exec(uri); | |
if (params != null) { | |
return unescape(params[1]); | |
} | |
return; | |
}; | |
$(function(){ | |
_ga.trackSocial(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment