1// Copyright 2011 Google Inc. All Rights Reserved. 2 3/** 4 * @fileoverview A simple script to automatically track Facebook and Twitter 5 * buttons using Google Analytics social tracking feature. 6 * @author [email protected] (Nick Mihailovski) 7 */ 8 9 10/** 11 * Namespace. 12 * @type {Object}. 13 */ 14var _ga = _ga || {}; 15 16 17/** 18 * Ensure global _gaq Google Anlaytics queue has be initialized. 19 * @type {Array} 20 */ 21var _gaq = _gaq || []; 22 23 24/** 25 * Helper method to track social features. This assumes all the social 26 * scripts / apis are loaded synchronously. If they are loaded async, 27 * you might need to add the nextwork specific tracking call to the 28 * a callback once the network's script has loaded. 29 * @param {string} opt_pageUrl An optional URL to associate the social 30 * tracking with a particular page. 31 * @param {string} opt_trackerName An optional name for the tracker object. 32 */ 33_ga.trackSocial = function(opt_pageUrl, opt_trackerName) { 34 _ga.trackFacebook(opt_pageUrl, opt_trackerName); 35 _ga.trackTwitter(opt_pageUrl, opt_trackerName); 36}; 37 38 39/** 40 * Tracks Facebook likes, unlikes and sends by suscribing to the Facebook 41 * JSAPI event model. Note: This will not track facebook buttons using the 42 * iFrame method. 43 * @param {string} opt_pageUrl An optional URL to associate the social 44 * tracking with a particular page. 45 * @param {string} opt_trackerName An optional name for the tracker object. 46 */ 47_ga.trackFacebook = function(opt_pageUrl, opt_trackerName) { 48 var trackerName = _ga.buildTrackerName_(opt_trackerName); 49 try { 50 if (FB && FB.Event && FB.Event.subscribe) { 51 FB.Event.subscribe('edge.create', function(targetUrl) { 52 _gaq.push([trackerName + '_trackSocial', 'facebook', 'like', 53 targetUrl, opt_pageUrl]); 54 }); 55 FB.Event.subscribe('edge.remove', function(targetUrl) { 56 _gaq.push([trackerName + '_trackSocial', 'facebook', 'unlike', 57 targetUrl, opt_pageUrl]); 58 }); 59 FB.Event.subscribe('message.send', function(targetUrl) { 60 _gaq.push([trackerName + '_trackSocial', 'facebook', 'send', 61 targetUrl, opt_pageUrl]); 62 }); 63 64/* Comment tracking from the tucknott.net blog */ 65
66 FB.Event.subscribe('comment.create', function(targetUrl) { 67 _gaq.push(['_trackSocial', 'facebook', 'comment', targetUrl]); 68 }); 69 70 FB.Event.subscribe('comment.remove', function(targetUrl) { 71 _gaq.push(['_trackSocial', 'facebook', 'uncomment', targetUrl]); 72 }); 73 74 } 75 } catch (e) {} 76}; 77 78 79 80 81/** 82 * Returns the normalized tracker name configuration parameter. 83 * @param {string} opt_trackerName An optional name for the tracker object. 84 * @return {string} If opt_trackerName is set, then the value appended with 85 * a . Otherwise an empty string. 86 * @private 87 */ 88_ga.buildTrackerName_ = function(opt_trackerName) { 89 return opt_trackerName ? opt_trackerName + '.' : ''; 90}; 91 92 93/** 94 * Tracks everytime a user clicks on a tweet button from Twitter. 95 * This subscribes to the Twitter JS API event mechanism to listen for 96 * clicks coming from this page. Details here: 97 * http://dev.twitter.com/pages/intents-events#click 98 * This method should be called once the twitter API has loaded. 99 * @param {string} opt_pageUrl An optional URL to associate the social 100 * tracking with a particular page. 101 * @param {string} opt_trackerName An optional name for the tracker object. 102 */ 103_ga.trackTwitter = function(opt_pageUrl, opt_trackerName) { 104 var trackerName = _ga.buildTrackerName_(opt_trackerName); 105 try { 106 if (twttr && twttr.events && twttr.events.bind) { 107 twttr.events.bind('tweet', function(event) { 108 if (event) { 109 var targetUrl; // Default value is undefined. 110 if (event.target && event.target.nodeName == 'IFRAME') { 111 targetUrl = _ga.extractParamFromUri_(event.target.src, 'url'); 112 } 113 _gaq.push([trackerName + '_trackSocial', 'twitter', 'tweet', 114 targetUrl, opt_pageUrl]); 115 } 116 }); 117 } 118 } catch (e) {} 119}; 120 121 122/** 123 * Extracts a query parameter value from a URI. 124 * @param {string} uri The URI from which to extract the parameter. 125 * @param {string} paramName The name of the query paramater to extract. 126 * @return {string} The un-encoded value of the query paramater. underfined 127 * if there is no URI parameter. 128 * @private 129 */ 130_ga.extractParamFromUri_ = function(uri, paramName) { 131 if (!uri) { 132 return; 133 } 134 var uri = uri.split('#')[0]; // Remove anchor. 135 var parts = uri.split('?'); // Check for query params. 136 if (parts.length == 1) { 137 return; 138 } 139 var query = decodeURI(parts[1]); 140 141 // Find url param. 142 paramName += '='; 143 var params = query.split('&'); 144 for (var i = 0, param; param = params[i]; ++i) { 145 if (param.indexOf(paramName) === 0) { 146 return unescape(param.split('=')[1]); 147 } 148 } 149 return; 150};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.