Shopencode
Shopencode

Shopencode

Twitter Intent Events with Google Analytics using _gaq.push();

Shopencode's photo
Shopencode

Published on Nov 15, 2011

1 min read

This is a quick script that i created to enable twitter intent event tracking with google analytics.

It's derived from twitters offical 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($) {

        var e = document.createElement('script'); e.type="text/javascript"; e.async = true; 
        e.src = 'http://platform.twitter.com/widgets.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(e);

        $(e).load(function() {
                function clickEventToAnalytics(intent_event) {
                        if (intent_event) {
                                var label = intent_event.region;
                                 _gaq.push(['_trackEvent', 'twitter_web_intents', intent_event.type, label]);
                        };      
                }       
                function tweetIntentToAnalytics(intent_event) {
                        if (intent_event) {
                                var label = intent_event.data.tweet_id;
                                _gaq.push(['_trackEvent', 'twitter_web_intents', intent_event.type, label]);
                        };      
                }       
                function favIntentToAnalytics(intent_event) {
                        tweetIntentToAnalytics(intent_event);
                }       
                function retweetIntentToAnalytics(intent_event) {
                        if (intent_event) {
                                var label = intent_event.data.source_tweet_id;
                                _gaq.push(['_trackEvent', 'twitter_web_intents', intent_event.type, label]);
                        };      
                }       
                function followIntentToAnalytics(intent_event) {
                        if (intent_event) {
                                var label = intent_event.data.user_id + " (" + intent_event.data.screen_name + ")";
                                _gaq.push(['_trackEvent', 'twitter_web_intents', intent_event.type, label]);
                        };      
                }       
                twttr.events.bind('click',    clickEventToAnalytics);
                twttr.events.bind('tweet',    tweetIntentToAnalytics);
                twttr.events.bind('retweet',  retweetIntentToAnalytics);
                twttr.events.bind('favorite', favIntentToAnalytics);
                twttr.events.bind('follow',   followIntentToAnalytics);
        });
});

No doubt i will revisit this in a few days when i have slept and the hype about the Twitter "Follow" buttons cools down, but for now this suffices but if anyone can recommend any improvements I would love to hear them!

 
Share this