Last active
August 29, 2015 14:07
-
-
Save chrisblakley/2fd2181c5735017c69aa to your computer and use it in GitHub Desktop.
Tracking Social Media Events and Logged-In Users in Google Analytics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="fb-root"></div> | |
<script type="text/javascript"> | |
window.fbAsyncInit = function() { | |
//Initialize the Facebook JavaScript SDK | |
FB.init({ | |
appId : '000000000000000', //App ID from the app dashboard | |
channelUrl : 'http://yourdomain.com/channel.html', //Channel file for x-domain communication | |
status : true, //Check Facebook Login status | |
xfbml : true //Look for social plugins on the page | |
}); | |
//Logged In Users | |
FB.getLoginStatus(function(response) { | |
if (response.status !== "unknown") { | |
ga('set', 'dimension1', 'Logged In'); | |
} | |
}); | |
//Facebook Likes | |
FB.Event.subscribe('edge.create', function(href, widget) { | |
var currentPage = jQuery(document).attr('title'); | |
ga('send', { | |
'hitType': 'social', | |
'socialNetwork': 'Facebook', | |
'socialAction': 'Like', | |
'socialTarget': href, | |
'page': currentPage | |
}); | |
}); | |
//Facebook Unlikes | |
FB.Event.subscribe('edge.remove', function(href, widget) { | |
var currentPage = jQuery(document).attr('title'); | |
ga('send', { | |
'hitType': 'social', | |
'socialNetwork': 'Facebook', | |
'socialAction': 'Unlike', | |
'socialTarget': href, | |
'page': currentPage | |
}); | |
}); | |
//Facebook Send/Share | |
FB.Event.subscribe('message.send', function(href, widget) { | |
var currentPage = jQuery(document).attr('title'); | |
ga('send', { | |
'hitType': 'social', | |
'socialNetwork': 'Facebook', | |
'socialAction': 'Send', | |
'socialTarget': href, | |
'page': currentPage | |
}); | |
}); | |
//Facebook Comments | |
FB.Event.subscribe('comment.create', function(href, widget) { | |
var currentPage = jQuery(document).attr('title'); | |
ga('send', { | |
'hitType': 'social', | |
'socialNetwork': 'Facebook', | |
'socialAction': 'Comment', | |
'socialTarget': href, | |
'page': currentPage | |
}); | |
}); | |
}; | |
//Load the SDK asynchronously | |
(function(d, s, id) { | |
var js, fjs = d.getElementsByTagName(s)[0]; | |
if (d.getElementById(id)) return; | |
js = d.createElement(s); js.id = id; | |
js.src = "//connect.facebook.net/en_GB/all.js"; | |
fjs.parentNode.insertBefore(js, fjs); | |
}(document, 'script', 'facebook-jssdk')); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
})(window,document,'script','//www.google-analytics.com/analytics.js','ga'); | |
ga('create', 'UA-00000000-0', 'yourdomain.com'); | |
ga('send', 'pageview'); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
window.twttr = function(d,s,id){ | |
var js,fjs=d.getElementsByTagName(s)[0]; | |
if(!d.getElementById(id)){ | |
js=d.createElement(s); | |
js.id=id;js.src="//platform.twitter.com/widgets.js"; | |
fjs.parentNode.insertBefore(js,fjs); | |
} | |
return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } }); | |
}(document,"script","twitter-wjs"); | |
twttr.ready(function(twttr){ | |
twttr.events.bind('tweet', track_tweet); | |
twttr.events.bind('follow', track_follow); | |
}); | |
//Tweets | |
function track_tweet( event ) { | |
if ( event ) { | |
var href = jQuery(location).attr('href'); | |
var pageTitle = jQuery(document).attr('title'); | |
ga('send', { | |
'hitType': 'social', | |
'socialNetwork': 'Twitter', | |
'socialAction': 'Tweet', | |
'socialTarget': href, | |
'page': pageTitle | |
}); | |
} | |
} | |
//Follows | |
function track_follow( event ) { | |
if ( event ) { | |
var href = jQuery(location).attr('href'); | |
var pageTitle = jQuery(document).attr('title'); | |
ga('send', { | |
'hitType': 'social', | |
'socialNetwork': 'Twitter', | |
'socialAction': 'Follow', | |
'socialTarget': href, | |
'page': pageTitle | |
}); | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment