Twitter Intent Events with Google Analytics using _gaq.push();
Update- 2012-04-27: I'v rewrote the original article and brought it up to date. Instead of using _trackEvent it now uses the appropriate _trackSocial call for google analytics.
This is a quick script that i created to enable twitter intent event tracking with google analytics.
It's derived from twitters official intents-events page but wrapped around a jQuery document ready so twitter's script doesn't slow down your page load and also makes sure the twitter javascript is loaded before you bind your own callbacks to track the intents
jQuery(document).ready(function($) {
$.getScript('//platform.twitter.com/widgets.js', function() {
function trackSocialIntent(intent_event) {
if (!intent_event) { return; }
_gaq.push(['_trackSocial', 'twitter', intent_event.type]);
}
twttr.events.bind('click', trackSocialIntent);
twttr.events.bind('tweet', trackSocialIntent);
twttr.events.bind('retweet', trackSocialIntent);
twttr.events.bind('favorite', trackSocialIntent);
twttr.events.bind('follow', trackSocialIntent);
});
});And thats all there is to it!
Update 2! - 2012-04-27: Why even use jQuery? It's a heavy framework and this vinila JS does the exact same as above but without the jQuery requirement!
(function() {
window.twttr = (function(d,s,id) {
var t, js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
js.src="//platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
}(document, "script", "twitter-wjs"));
twttr.ready(function(twttr) {
function trackSocialIntent(intent_event) {
if (!intent_event) { return; }
_gaq.push(['_trackSocial', 'twitter', intent_event.type]);
}
twttr.events.bind('click', trackSocialIntent);
twttr.events.bind('tweet', trackSocialIntent);
twttr.events.bind('retweet', trackSocialIntent);
twttr.events.bind('favorite', trackSocialIntent);
twttr.events.bind('follow', trackSocialIntent);
});
})();