Skip to content

Instantly share code, notes, and snippets.

@jorgegorka
Last active December 18, 2015 22:28
Show Gist options
  • Save jorgegorka/5854280 to your computer and use it in GitHub Desktop.
Save jorgegorka/5854280 to your computer and use it in GitHub Desktop.
Track user interaction with Google Analytics
jQuery(function($) {
// last time we send tracking information
// default is 60 seconds ( converted to miliseconds )
var send_interval = 60 * 1000;
// Last time we send the event to GA
var last_time = new Date().getTime();
// Track mouse movement
$(window).mousemove(function() {
sendTrackData();
});
// has send_interval seconds passed since last event?
function shouldTrack() {
// The very present :-)
var right_now = new Date().getTime();
return ((right_now - last_time) > send_interval);
};
// send track data to GA
function sendTrackData() {
if (shouldTrack()) {
_gaq.push(['_trackEvent', 'Page', "'" + $(document).attr('title') + "'", "'" + fullDate() + "'", 0, false]);
last_time = new Date().getTime();
};
};
// returns current date human readable
function fullDate() {
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
var curr_hour = d.getHours();
var curr_minute = d.getMinutes();
return curr_date + "-" + curr_month + "-" + curr_year + " " + curr_hour + ":" + curr_minute;
};
// this will be triggered when mobile devices do scroll
//$(document).bind('scrollstart', function() {
//sendTrackData();
//});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment