Skip to content

Instantly share code, notes, and snippets.

@nickcatal
Created June 14, 2013 04:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nickcatal/5779475 to your computer and use it in GitHub Desktop.
Save nickcatal/5779475 to your computer and use it in GitHub Desktop.
#NN13 Google Analytics Example Snippets
// Disqus Tracking
/*
Put <div id="disqus_thread"></div> above
*/
var disqus_shortname = 'yourfuckingpollingplace'; // required: replace example with your forum shortname
function track_disqus(comment) {
_gaq.push(['_trackEvent', 'Disqus', 'Comment', document.title]);
};
function disqus_config() {
this.callbacks.onNewComment = [function(comment) {
track_disqus(comment);
}];
} /* * * Disqus Include Code * * */
(function() {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
// Facebook
/*
For Facebook remember to add <div id="fb-root"></div> somewhere on your page
*/
window.fbAsyncInit = function() {
FB.init({
appId : '12345', // Replace with your App ID/API Key from the App Dashboard, create a new app at https://developers.facebook.com/
channelUrl : '//www.yourdomain.com/channel.html', // Follow the instructions at https://developers.facebook.com/docs/javascript/gettingstarted/#channel to add a channel file to your domain
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('edge.create', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'like', targetUrl],['_trackEvent', 'Facebook', 'like', targetUrl]);
});
FB.Event.subscribe('edge.remove', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl],['_trackEvent', 'Facebook', 'unlike', targetUrl]);
});
FB.Event.subscribe('message.send', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'send', targetUrl],['_trackEvent', 'Facebook', 'send', targetUrl]);
});
FB.Event.subscribe('comment.create', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'comment', targetUrl],['_trackEvent', 'Facebook', 'comment', targetUrl]);
});
FB.Event.subscribe('comment.remove', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'commentremove', targetUrl],['_trackEvent', 'Facebook', 'commentremove', targetUrl]);
});
};
(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_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
// Google Analytics Code (Include this above all the other JS)
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12345-1'],['_trackPageview']); // Replace UA-12345-1 with your Google Analytics ID
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
// Twitter Code
window.twttr = (function (d,s,id) {
var t, js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return; 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"));
function trackTwitter(intent_event) {
if (intent_event) {
var opt_pagePath;
if (intent_event.target && intent_event.target.nodeName == 'IFRAME') {
opt_pagePath = extractParamFromUri(intent_event.target.src, 'url');
}
_gaq.push(['_trackSocial', 'Twitter', 'Tweet', opt_pagePath], ['_trackEvent', 'Twitter', 'Tweet', opt_pagePath]);
}
}
//Wrap event bindings - Wait for async js to load
twttr.ready(function(twttr) {
//event bindings
twttr.events.bind('tweet', trackTwitter);
twttr.events.bind('follow', function(event) {
_gaq.push(['_trackSocial', 'Twitter', 'Follow', event.data.screen_name], ['_trackEvent', 'Twitter', 'Follow', event.data.screen_name]);
});
});
<!DOCTYPE html>
<html>
<head>
<title>Google Analytics Javascript Callback Tracking</title>
</head>
<body>
<h1>Google Analytics Testing</h1>
<div>
<h2>Facebook Like</h2>
<div class="fb-like" data-href=
"http://www.yourfuckingpollingplace.com/" data-send="true"
data-show-faces="false" data-width="450"></div>
</div>
<div>
<h2>Facebook Comment</h2>
<div class="fb-comments" data-href=
"http://www.yourfuckingpollingplace.com" data-num-posts="10"
data-width="470"></div>
</div>
<div>
<h2>Twitter Follow</h2><a class="twitter-follow-button"
data-show-count="false" href="https://twitter.com/nickcatal">Follow
@NickCatal</a>
</div>
<div>
<h2>Twitter Tweet</h2><a href=
"https://twitter.com/intent/tweet?url=http%3A%2F%2Fwww.yourfuckingpollingplace.com%2F&amp;text=This%20is%20a%20tweet%20about%20%23NN13">Tweet
About YFPP</a>
</div>
<div>
<h2>Disqus</h2>
<div id="disqus_thread"></div>
</div>
<div>
<h2>Link On Page (Using jQuery)</h2><a class="linktoclick" href=
"#">Link To Click</a>
</div>
<div>
<h2>Link On Page (Using Straight HTML)</h2><a href="#" onclick=
"_gaq.push(['_trackEvent', 'Link', 'Click', 'Click On Link Using JQuery'])">Link
to Click</a>
</div>
<div id="fb-root"></div>
<script type="text/javascript">
// Google Analytics Code (Include this above all the other JS)
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12345-1'],['_trackPageview']); // Replace UA-12345-1 with your Google Analytics ID
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript">
// Disqus Tracking
/*
Put <div id="disqus_thread"></div> above
*/
var disqus_shortname = 'yourfuckingpollingplace'; // required: replace example with your forum shortname
function track_disqus(comment) {
_gaq.push(['_trackEvent', 'Disqus', 'Comment', document.title]);
};
function disqus_config() {
this.callbacks.onNewComment = [function(comment) {
track_disqus(comment);
}];
} /* * * Disqus Include Code * * */
(function() {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<script type="text/javascript">
// Facebook
window.fbAsyncInit = function() {
FB.init({
appId : '382719335136154', // Replace with your App ID/API Key from the App Dashboard, create a new app at https://developers.facebook.com/
//channelUrl : '//www.yourdomain.com/channel.html', // Follow the instructions at https://developers.facebook.com/docs/javascript/gettingstarted/#channel to add a channel file to your domain, then uncomment this line
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('edge.create', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'like', targetUrl],['_trackEvent', 'Facebook', 'like', targetUrl]);
});
FB.Event.subscribe('edge.remove', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl],['_trackEvent', 'Facebook', 'unlike', targetUrl]);
});
FB.Event.subscribe('comment.create', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'comment', targetUrl],['_trackEvent', 'Facebook', 'comment', targetUrl]);
});
FB.Event.subscribe('comment.remove', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'commentremove', targetUrl],['_trackEvent', 'Facebook', 'commentremove', targetUrl]);
});
};
(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_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<script type="text/javascript">
// Twitter Code
window.twttr = (function (d,s,id) {
var t, js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return; 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"));
function trackTwitter(intent_event) {
if (intent_event) {
var opt_pagePath;
if (intent_event.target && intent_event.target.nodeName == 'IFRAME') {
opt_pagePath = extractParamFromUri(intent_event.target.src, 'url');
}
_gaq.push(['_trackSocial', 'Twitter', 'Tweet', opt_pagePath], ['_trackEvent', 'Twitter', 'Tweet', opt_pagePath]);
}
}
//Wrap event bindings - Wait for async js to load
twttr.ready(function(twttr) {
//event bindings
twttr.events.bind('tweet', trackTwitter);
twttr.events.bind('follow', function(event) {
_gaq.push(['_trackSocial', 'Twitter', 'Follow', event.data.screen_name], ['_trackEvent', 'Twitter', 'Follow', event.data.screen_name]);
});
});
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
// Link On Page (Using JQuery)
/*
Be sure to include jquery above this code
*/
$('.linktoclick').click(function() {
_gaq.push(['_trackEvent', 'Link', 'Click', 'Click On Link Using JQuery']);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment