Skip to content

Instantly share code, notes, and snippets.

@stereobooster
Created November 12, 2011 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stereobooster/1361086 to your computer and use it in GitHub Desktop.
Save stereobooster/1361086 to your computer and use it in GitHub Desktop.
GA
<!-- Facebook async loading of JSAPI -->
<div id="fb-root"></div>
<script>
// Facebook async loading.
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
window.fbAsyncInit = function() {
FB.init({appId: '164838206912600', status: true, cookie: true,
xfbml: true});
_ga.trackFacebook();
};
</script>
<!-- Load Twitter JS-API asynchronously -->
<script>
(function(){
var twitterWidgets = document.createElement('script');
twitterWidgets.type = 'text/javascript';
twitterWidgets.async = true;
twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
// Setup a callback to track once the script loads.
twitterWidgets.onload = _ga.trackTwitter;
document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
})();
</script>
<script type="text/javascript" src="http://vkontakte.ru/js/api/share.js?11" charset="windows-1251"></script>
<script type="text/javascript">
VK.init({
apiId: ВАШ_API_ID,
onlyWidgets: true
});
</script>
/*
http://www.google.com/support/analytics/bin/answer.py?answer=77613
Google
Yahoo
MSN
Lycos
Ask
Altavista
Netscape
CNN
Looksmart
About
Mamma
Alltheweb
Gigablast
Voila
Virgilio
Live
Baidu
Alice
Yandex
Najdi
AOL
Club-internet
Mama
Seznam
Search
Wp
Onet
Netsprint
Google.interia
Szukacz
Yam
Pchome
*/
var _gaq=_gaq||[];
(function(_gaq){
var a='bing.com;q;images.yandex.ru;text;blogs.yandex.ru;text;video.yandex.ru;text;mail.ru;q;go.mail.ru;q;rambler.ru;words;nova.rambler.ru;query;nova.rambler.ru;words;gogo.ru;q;nigma.ru;s;search.qip.ru;query;webalta.ru;q;sm.aport.ru;r;meta.ua;q;search.bigmir.net;z;search.i.ua;q;index.online.ua;q;web20.a.ua;query;search.ukr.net;search_query;search.com.ua;q;search.ua;q;poisk.ru;text;go.km.ru;sq;liveinternet.ru;ask;gde.ru;keywords;affiliates.quintura.com;request;akavita.by;z;search.tut.by;query;all.by;query;ukr.net;search_query;quintura.ru;request;qip.ru;query;metabot.ru;st;i.ua;q;a.ua;s'.split(';'),
i;
for(i=0;i<a.length;i+=2){
_gaq.push(['_addOrganic',a[i],a[i+1]]);
}
}(_gaq));
//All this stuff should be before _gaq.push(['_trackPageview']);
// http://habrahabr.ru/company/icontext/blog/129333/
// http://habrahabr.ru/blogs/web_analytics/132639/
// http://api.yandex.ru/share/
// Copyright 2011 Google Inc. All Rights Reserved.
/**
* @fileoverview A simple script to automatically track Facebook and Twitter
* buttons using Google Analytics social tracking feature.
* @author api.nickm@google.com (Nick Mihailovski)
*/
/**
* Namespace.
* @type {Object}.
*/
var _ga = _ga || {};
/**
* Ensure global _gaq Google Anlaytics queue has be initialized.
* @type {Array}
*/
var _gaq = _gaq || [];
/**
* Helper method to track social features. This assumes all the social
* scripts / apis are loaded synchronously. If they are loaded async,
* you might need to add the nextwork specific tracking call to the
* a callback once the network's script has loaded.
* @param {string} opt_pageUrl An optional URL to associate the social
* tracking with a particular page.
* @param {string} opt_trackerName An optional name for the tracker object.
*/
_ga.trackSocial = function(opt_pageUrl, opt_trackerName) {
_ga.trackFacebook(opt_pageUrl, opt_trackerName);
_ga.trackTwitter(opt_pageUrl, opt_trackerName);
_ga.trackVkontakte(opt_pageUrl, opt_trackerName);
};
/**
* Tracks Facebook likes, unlikes and sends by suscribing to the Facebook
* JSAPI event model. Note: This will not track facebook buttons using the
* iFrame method.
* @param {string} opt_pageUrl An optional URL to associate the social
* tracking with a particular page.
* @param {string} opt_trackerName An optional name for the tracker object.
*/
_ga.trackFacebook = function(opt_pageUrl, opt_trackerName) {
var trackerName = _ga.buildTrackerName_(opt_trackerName);
try {
if (FB && FB.Event && FB.Event.subscribe) {
FB.Event.subscribe('edge.create', function(targetUrl) {
_gaq.push([trackerName + '_trackSocial', 'facebook', 'like',
targetUrl, opt_pageUrl]);
});
FB.Event.subscribe('edge.remove', function(targetUrl) {
_gaq.push([trackerName + '_trackSocial', 'facebook', 'unlike',
targetUrl, opt_pageUrl]);
});
FB.Event.subscribe('message.send', function(targetUrl) {
_gaq.push([trackerName + '_trackSocial', 'facebook', 'send',
targetUrl, opt_pageUrl]);
});
}
} catch (e) {}
};
/**
* Returns the normalized tracker name configuration parameter.
* @param {string} opt_trackerName An optional name for the tracker object.
* @return {string} If opt_trackerName is set, then the value appended with
* a . Otherwise an empty string.
* @private
*/
_ga.buildTrackerName_ = function(opt_trackerName) {
return opt_trackerName ? opt_trackerName + '.' : '';
};
/**
* Tracks everytime a user clicks on a tweet button from Twitter.
* This subscribes to the Twitter JS API event mechanism to listen for
* clicks coming from this page. Details here:
* http://dev.twitter.com/pages/intents-events#click
* This method should be called once the twitter API has loaded.
* @param {string} opt_pageUrl An optional URL to associate the social
* tracking with a particular page.
* @param {string} opt_trackerName An optional name for the tracker object.
*/
_ga.trackTwitter = function(opt_pageUrl, opt_trackerName) {
var trackerName = _ga.buildTrackerName_(opt_trackerName);
try {
if (twttr && twttr.events && twttr.events.bind) {
twttr.events.bind('tweet', 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', 'tweet',
targetUrl, opt_pageUrl]);
}
});
}
} catch (e) {}
};
_ga.trackVkontakte = function(opt_pageUrl, opt_trackerName) {
var trackerName = _ga.buildTrackerName_(opt_trackerName),
targetUrl;
try {
if (VK && VK.Observer && VK.Observer.subscribe) {
VK.Observer.subscribe('widgets.like.liked', function() {
_gaq.push([trackerName + '_trackSocial', 'vkontakte', 'like',
targetUrl, opt_pageUrl]);
});
VK.Observer.subscribe('widgets.like.unliked', function() {
_gaq.push([trackerName + '_trackSocial', 'vkontakte', 'unlike',
targetUrl, opt_pageUrl]);
});
}
} catch (e) {}
};
/**
* Extracts a query parameter value from a URI.
* @param {string} uri The URI from which to extract the parameter.
* @param {string} paramName The name of the query paramater to extract.
* @return {string} The un-encoded value of the query paramater. underfined
* if there is no URI parameter.
* @private
*/
_ga.extractParamFromUri_ = function(uri, paramName) {
if (!uri) {
return;
}
var uri = uri.split('#')[0]; // Remove anchor.
var parts = uri.split('?'); // Check for query params.
if (parts.length == 1) {
return;
}
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;
};
window.onerror=function(m,u,l){_gaq.push(["_trackEvent","error",m,u,l]);};
$(function(){
$("body").delegate("a", "click", function(){
$this = $(this);
if($this.attr("hostname") !== window.location.hostname){
_gaq.push(["_trackEvent", "links", "external", $this.attr("href"), 0]);
}
});
});
//http://imakewebthings.github.com/jquery-waypoints/scroll-analytics/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment