|
|
Linha 12: |
Linha 12: |
| })(); | | })(); |
| | | |
− | /** GA Social Tracking. Copyright 2011 Google Inc. All Rights Reserved **/ | + | // Copyright 2012 Google Inc. All Rights Reserved. |
− | /* | + | /** |
| * @fileoverview A simple script to automatically track Facebook and Twitter | | * @fileoverview A simple script to automatically track Facebook and Twitter |
| * buttons using Google Analytics social tracking feature. | | * buttons using Google Analytics social tracking feature. |
− | * @author api.nickm@google.com (Nick Mihailovski) | + | * @author api.nickm@gmail.com (Nick Mihailovski) |
| + | * @author api.petef@gmail.com (Pete Frisella) |
| */ | | */ |
− | /* | + | |
| + | /** |
| * Namespace. | | * Namespace. |
| * @type {Object}. | | * @type {Object}. |
| */ | | */ |
| var _ga = _ga || {}; | | var _ga = _ga || {}; |
− | /* | + | |
| + | /** |
| * Ensure global _gaq Google Analytics queue has been initialized. | | * Ensure global _gaq Google Analytics queue has been initialized. |
| * @type {Array} | | * @type {Array} |
| */ | | */ |
| var _gaq = _gaq || []; | | var _gaq = _gaq || []; |
− | /* | + | |
− | * Helper method to track social features. This assumes all the social | + | /** |
− | * scripts / apis are loaded synchronously. If they are loaded async, | + | * Tracks social interactions by iterating through each tracker object |
− | * you might need to add the nextwork specific tracking call to the | + | * of the page, and calling the _trackSocial method. This function |
− | * a callback once the network's script has loaded. | + | * should be pushed onto the _gaq queue. For details on parameters see |
− | * @param {string} opt_pageUrl An optional URL to associate the social | + | * http://code.google.com/apis/analytics/docs/gaJS/gaJSApiSocialTracking.html |
− | * tracking with a particular page. | + | * @param {string} network The network on which the action occurs. |
− | * @param {string} opt_trackerName An optional name for the tracker object. | + | * @param {string} socialAction The type of action that happens. |
| + | * @param {string} opt_target Optional text value that indicates the |
| + | * subject of the action. |
| + | * @param {string} opt_pagePath Optional page (by path, not full URL) |
| + | * from which the action occurred. |
| + | * @return a function that iterates over each tracker object |
| + | * and calls the _trackSocial method. |
| + | * @private |
| */ | | */ |
− | _ga.trackSocial = function(opt_pageUrl, opt_trackerName) { | + | _ga.getSocialActionTrackers_ = function( |
− | _ga.trackFacebook(opt_pageUrl, opt_trackerName); | + | network, socialAction, opt_target, opt_pagePath) { |
− | _ga.trackTwitter(opt_pageUrl, opt_trackerName);
| + | return function() { |
| + | var trackers = _gat._getTrackers(); |
| + | for (var i = 0, tracker; tracker = trackers[i]; i++) { |
| + | tracker._trackSocial(network, socialAction, opt_target, opt_pagePath); |
| + | } |
| + | }; |
| }; | | }; |
− | /* | + | |
| + | /** |
| * Tracks Facebook likes, unlikes and sends by suscribing to the Facebook | | * Tracks Facebook likes, unlikes and sends by suscribing to the Facebook |
| * JSAPI event model. Note: This will not track facebook buttons using the | | * JSAPI event model. Note: This will not track facebook buttons using the |
− | * iFrame method. | + | * iframe method. |
− | * @param {string} opt_pageUrl An optional URL to associate the social | + | * @param {string} opt_pagePath An optional URL to associate the social |
| * tracking with a particular page. | | * tracking with a particular page. |
− | * @param {string} opt_trackerName An optional name for the tracker object.
| |
| */ | | */ |
− | _ga.trackFacebook = function(opt_pageUrl, opt_trackerName) { | + | _ga.trackFacebook = function(opt_pagePath) { |
− | var trackerName = _ga.buildTrackerName_(opt_trackerName);
| + | |
| try { | | try { |
| if (FB && FB.Event && FB.Event.subscribe) { | | if (FB && FB.Event && FB.Event.subscribe) { |
− | FB.Event.subscribe('edge.create', function(targetUrl) { | + | FB.Event.subscribe('edge.create', function(opt_target) { |
− | _gaq.push([trackerName + '_trackSocial', 'facebook', 'like', | + | _gaq.push(_ga.getSocialActionTrackers_('facebook', 'like', |
− | targetUrl, opt_pageUrl]); | + | opt_target, opt_pagePath)); |
| }); | | }); |
− | FB.Event.subscribe('edge.remove', function(targetUrl) { | + | FB.Event.subscribe('edge.remove', function(opt_target) { |
− | _gaq.push([trackerName + '_trackSocial', 'facebook', 'unlike', | + | _gaq.push(_ga.getSocialActionTrackers_('facebook', 'unlike', |
− | targetUrl, opt_pageUrl]); | + | opt_target, opt_pagePath)); |
| }); | | }); |
− | FB.Event.subscribe('message.send', function(targetUrl) { | + | FB.Event.subscribe('message.send', function(opt_target) { |
− | _gaq.push([trackerName + '_trackSocial', 'facebook', 'send', | + | _gaq.push(_ga.getSocialActionTrackers_('facebook', 'send', |
− | targetUrl, opt_pageUrl]); | + | opt_target, opt_pagePath)); |
| }); | | }); |
| } | | } |
| } catch (e) {} | | } catch (e) {} |
| }; | | }; |
− | /* | + | |
− | * Returns the normalized tracker name configuration parameter. | + | /** |
− | * @param {string} opt_trackerName An optional name for the tracker object. | + | * Handles tracking for Twitter click and tweet Intent Events which occur |
− | * @return {string} If opt_trackerName is set, then the value appended with | + | * everytime a user Tweets using a Tweet Button, clicks a Tweet Button, or |
− | * a . Otherwise an empty string. | + | * clicks a Tweet Count. This method should be binded to Twitter click and |
| + | * tweet events and used as a callback function. |
| + | * Details here: http://dev.twitter.com/docs/intents/events |
| + | * @param {object} intent_event An object representing the Twitter Intent Event |
| + | * passed from the Tweet Button. |
| + | * @param {string} opt_pagePath An optional URL to associate the social |
| + | * tracking with a particular page. |
| * @private | | * @private |
| */ | | */ |
− | _ga.buildTrackerName_ = function(opt_trackerName) { | + | _ga.trackTwitterHandler_ = function(intent_event, opt_pagePath) { |
− | return opt_trackerName ? opt_trackerName + '.' : ''; | + | var opt_target; //Default value is undefined |
| + | if (intent_event && intent_event.type == 'tweet' || |
| + | intent_event.type == 'click') { |
| + | if (intent_event.target.nodeName == 'IFRAME') { |
| + | opt_target = _ga.extractParamFromUri_(intent_event.target.src, 'url'); |
| + | } |
| + | var socialAction = intent_event.type + ((intent_event.type == 'click') ? |
| + | '-' + intent_event.region : ''); //append the type of click to action |
| + | _gaq.push(_ga.getSocialActionTrackers_('twitter', socialAction, opt_target, |
| + | opt_pagePath)); |
| + | } |
| }; | | }; |
− | /* | + | |
− | * Tracks everytime a user clicks on a tweet button from Twitter. | + | /** |
− | * This subscribes to the Twitter JS API event mechanism to listen for | + | * Binds Twitter Intent Events to a callback function that will handle |
− | * clicks coming from this page. Details here:
| + | * the social tracking for Google Analytics. This function should be called |
− | * http://dev.twitter.com/pages/intents-events#click
| + | * once the Twitter widget.js file is loaded and ready. |
− | * This method should be called once the twitter API has loaded.
| + | * @param {string} opt_pagePath An optional URL to associate the social |
− | * @param {string} opt_pageUrl An optional URL to associate the social | + | |
| * tracking with a particular page. | | * tracking with a particular page. |
− | * @param {string} opt_trackerName An optional name for the tracker object.
| |
| */ | | */ |
− | _ga.trackTwitter = function(opt_pageUrl, opt_trackerName) { | + | _ga.trackTwitter = function(opt_pagePath) { |
− | var trackerName = _ga.buildTrackerName_(opt_trackerName); | + | intent_handler = function(intent_event) { |
− | try { | + | _ga.trackTwitterHandler_(intent_event, opt_pagePath); |
− | if (twttr && twttr.events && twttr.events.bind) {
| + | }; |
− | twttr.events.bind('tweet', function(event) {
| + | |
− | if (event) {
| + | //bind twitter Click and Tweet events to Twitter tracking handler |
− | var targetUrl; // Default value is undefined.
| + | twttr.events.bind('click', intent_handler); |
− | if (event.target && event.target.nodeName == 'IFRAME') {
| + | twttr.events.bind('tweet', intent_handler); |
− | targetUrl = _ga.extractParamFromUri_(event.target.src, 'url');
| + | |
− | }
| + | |
− | _gaq.push([trackerName + '_trackSocial', 'twitter', 'tweet',
| + | |
− | targetUrl, opt_pageUrl]);
| + | |
− | }
| + | |
− | });
| + | |
− | }
| + | |
− | } catch (e) {}
| + | |
| }; | | }; |
− | /* | + | |
| + | /** |
| * Extracts a query parameter value from a URI. | | * Extracts a query parameter value from a URI. |
| * @param {string} uri The URI from which to extract the parameter. | | * @param {string} uri The URI from which to extract the parameter. |
| * @param {string} paramName The name of the query paramater to extract. | | * @param {string} paramName The name of the query paramater to extract. |
− | * @return {string} The un-encoded value of the query paramater. underfined | + | * @return {string} The un-encoded value of the query paramater. undefined |
| * if there is no URI parameter. | | * if there is no URI parameter. |
| * @private | | * @private |
Linha 117: |
Linha 138: |
| return; | | return; |
| } | | } |
− | var uri = uri.split('#')[0]; // Remove anchor. | + | var regex = new RegExp('[\\?&#]' + paramName + '=([^&#]*)'); |
− | var parts = uri.split('?'); // Check for query params.
| + | var params = regex.exec(uri); |
− | if (parts.length == 1) {
| + | if (params != null) { |
− | return;
| + | return unescape(params[1]); |
− | }
| + | |
− | var query = decodeURI(parts[1]);
| + | |
− | | + | |
− | // Find url param.
| + | |
− | paramName += '=';
| + | |
− | var params = query.split('&'); | + | |
− | for (var i = 0, param; param = params[i]; ++i) { | + | |
− | if (param.indexOf(paramName) === 0) { | + | |
− | return unescape(param.split('=')[1]);
| + | |
− | }
| + | |
| } | | } |
| return; | | return; |