Skip to content

Instantly share code, notes, and snippets.

@chrisblakley
Last active August 29, 2015 14:07
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 chrisblakley/2fd2181c5735017c69aa to your computer and use it in GitHub Desktop.
Save chrisblakley/2fd2181c5735017c69aa to your computer and use it in GitHub Desktop.
Tracking Social Media Events and Logged-In Users in Google Analytics
<!-- Google -->
<img style="display: none;" onLoad="google_user(true)" alt="" src="https://accounts.google.com/CheckCookie?continue=https%3A%2F%2Fwww.google.com%2Fintl%2Fen%2Fimages%2Flogos%2Faccounts_logo.png&amp;followup=https%3A%2F%2Fwww.google.com%2Fintl%2Fen%2Fimages%2Flogos%2Faccounts_logo.png&amp;chtml=LoginDoneHtml&amp;checkedDomains=youtube&amp;checkConnection=youtube%3A291%3A1" />
<!-- Google Plus (Note: May want to place this within a "is not mobile" check) -->
<img style="display: none;" onLoad="google_plus_user(true)" alt="" src="https://plus.google.com/up/?continue=https://www.google.com/intl/en/images/logos/accounts_logo.png&amp;type=st&amp;gpsrc=ogpy0" />
<!-- Twitter -->
<img style="display: none;" onLoad="twitter_user(true)" alt="" src="https://twitter.com/login?redirect_after_login=%2Fimages%2Fspinner.gif" />
<script>
function twitter_user(status) {
if (status) {
ga('set', 'dimension2', 'Logged In');
};
}
function google_plus_user(status) {
if (status) {
ga('set', 'dimension3', 'Logged In');
};
}
function google_user(status) {
if (status) {
ga('set', 'dimension4', 'Logged In');
};
}
</script>
<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>
<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>
<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