Skip to content

Instantly share code, notes, and snippets.

@DanielKehoe
Created January 11, 2013 20:32
Show Gist options
  • Save DanielKehoe/4513745 to your computer and use it in GitHub Desktop.
Save DanielKehoe/4513745 to your computer and use it in GitHub Desktop.
JavaScript used to track events on the https://tutorials.railsapps.org site and funnel to segment.io.
// https://segment.io/railsapps/snippet/
var analytics=analytics||[];analytics.load=function(e){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=("https:"===document.location.protocol?"https://":"http://")+"d2dq2ahtl5zl1z.cloudfront.net/analytics.js/v1/"+e+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);var r=function(e){return function(){analytics.push([e].concat(Array.prototype.slice.call(arguments,0)))}},i=["identify","track","pageview","ab"];for(var s=0;s<i.length;s++)analytics[i[s]]=r(i[s])},
analytics.load("xxxxxxxxxx");
// run analytics on every page
$(document).ready(function() {
var user_id = $('#body').data('user_id')
var user_name = $('#body').data('user_name')
var user_plan = $('#body').data('user_plan')
// https://segment.io/railsapps/setup
analytics.identify(user_id, {
name : user_name,
plan : user_plan
});
// track page visits showing controller and view
analytics.track('Page Visit', {
page : $('#body').attr('class')
});
// track any Rails flash notices
if ($('#flash_notice').length) {
analytics.track('Notice', {
msg : $('#flash_notice').text()
});
}
// track any Rails flash alerts
if ($('#flash_alert').length) {
analytics.track('Alert', {
msg : $('#flash_alert').text()
});
}
// track clicks on elements flagged with the 'analyze' class
$('.analyze').click(function() {
analytics.track('Click', {
describe : $(this).data('describe')
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment