Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Асинхронный скрипт подключения к Google Analytics отслеживания социальных кнопок
<!-- Twitter -->
<a href="http://twitter.com/share" class="twitter-share-button" data-lang="ru" data-url="http://simonenko.su">Tweet</a>
<!-- Facebook Like -->
<div class="fb-like" data-href="http://simonenko.su" data-layout="button_count" data-send="false" data-show-faces="true"></div>
<!-- Google +1 -->
<div class="g-plusone" data-href="http://simonenko.su"></div>
<!-- ВКонтакте "Мне нравится" -->
<script>
window.vkAsyncInit = function() {
VK.init({apiId: VKONTAKTE_ID, onlyWidgets: true});
VK.Widgets.Like('vk_like', {pageUrl: 'http://simonenko.su', type: 'mini'});
}
</script>
<div id="vk_like"></div>
var _gaq = _gaq || []; // Настройки Google Analytics
_gaq.push(['_setAccount', 'UA-XXXX-1']); // Не забудьте заменить на свой ID
_gaq.push(['_trackPageview']);
_gaq.push(['_trackPageLoadTime']);
(function(window, document) {
window.___gcfg = { // Настройки языка для Google +1 Button
lang: 'ru'
};
var post_url = 'http://simonenko.su', // Ссылка на отслеживаемую страницу
track = {
vkontakte: function() {
try {
if (VK && VK.Observer && VK.Observer.subscribe) {
// Отслеживаем кнопку "нравится"
VK.Observer.subscribe('widgets.like.liked', function() {
_gaq.push(['_trackSocial', 'vkontakte', 'like', post_url]);
});
VK.Observer.subscribe('widgets.like.unliked', function() {
_gaq.push(['_trackSocial', 'vkontakte', 'unlike', post_url]);
});
}
} catch(error) { }
},
facebook: function() {
try {
if (FB && FB.Event && FB.Event.subscribe) {
// Отслеживаем кнопку "like"
FB.Event.subscribe('edge.create', function() {
_gaq.push(['_trackSocial', 'facebook', 'like', post_url]);
});
FB.Event.subscribe('edge.remove', function() {
_gaq.push(['_trackSocial', 'facebook', 'unlike', post_url]);
});
// Отслеживаем кнопку "send"
FB.Event.subscribe('message.send', function() {
_gaq.push(['_trackSocial', 'facebook', 'send', post_url]);
});
}
} catch(error) { }
},
twitter: function() {
try {
if (twttr && twttr.events && twttr.events.bind) {
// Отслеживаем кнопку "tweet"
twttr.events.bind('tweet', function() {
_gaq.push(['_trackSocial', 'twitter', 'tweet', post_url]);
});
}
} catch(error) { }
}
},
apis = [
'https://apis.google.com/js/plusone.js', // Google +1 Button
'//vk.com/js/api/openapi.js?75', // Vkontakte API
'//platform.twitter.com/widgets.js', // Twitter Widgets
'//connect.facebook.net/ru_RU/all.js#xfbml=1', // Facebook SDK
('https:' == location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js' // Google Analytics
],
socialTrack = [
'',
'vkontakte',
'twitter',
'facebook',
''
],
iterator = apis.length,
script = 'script',
fragment = document.createDocumentFragment(),
element = document.createElement(script),
clone;
while (iterator--) {
clone = element.cloneNode(false);
clone.async = clone.src = apis[iterator];
func = socialTrack[iterator];
if (func && func != '') {
clone.onload = track[func];
}
fragment.appendChild(clone);
}
clone = document.getElementsByTagName(script)[0];
clone.parentNode.insertBefore(fragment, clone);
})(this, document);
var _gaq=_gaq||[];_gaq.push(["_setAccount","UA-XXXX-1"]),_gaq.push(["_trackPageview"]),_gaq.push(["_trackPageLoadTime"]),function(e,t){e.___gcfg={lang:"ru"};var n="http://simonenko.su",r={vkontakte:function(){try{VK&&VK.Observer&&VK.Observer.subscribe&&(VK.Observer.subscribe("widgets.like.liked",function(){_gaq.push(["_trackSocial","vkontakte","like",n])}),VK.Observer.subscribe("widgets.like.unliked",function(){_gaq.push(["_trackSocial","vkontakte","unlike",n])}))}catch(e){}},facebook:function(){try{FB&&FB.Event&&FB.Event.subscribe&&(FB.Event.subscribe("edge.create",function(){_gaq.push(["_trackSocial","facebook","like",n])}),FB.Event.subscribe("edge.remove",function(){_gaq.push(["_trackSocial","facebook","unlike",n])}),FB.Event.subscribe("message.send",function(){_gaq.push(["_trackSocial","facebook","send",n])}))}catch(e){}},twitter:function(){try{twttr&&twttr.events&&twttr.events.bind&&twttr.events.bind("tweet",function(){_gaq.push(["_trackSocial","twitter","tweet",n])})}catch(e){}}},i=["https://apis.google.com/js/plusone.js","//userapi.com/js/api/openapi.js?52","//platform.twitter.com/widgets.js","//connect.facebook.net/ru_RU/all.js#xfbml=1",("https:"==location.protocol?"//ssl":"//www")+".google-analytics.com/ga.js"],s=["","vkontakte","twitter","facebook",""],o=i.length,u="script",a=t.createDocumentFragment(),f=t.createElement(u),l;while(o--)l=f.cloneNode(!1),l.async=l.src=i[o],func=s[o],func&&func!=""&&(l.onload=r[func]),a.appendChild(l);l=t.getElementsByTagName(u)[0],l.parentNode.insertBefore(a,l)}(this,document);
@meritt

This comment has been minimized.

Show comment Hide comment
@meritt

meritt Dec 21, 2011

Подробнее о том как применять этот скрипт можно прочитать здесь: blog.simonenko.su

Owner

meritt commented Dec 21, 2011

Подробнее о том как применять этот скрипт можно прочитать здесь: blog.simonenko.su

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment