Last active
December 18, 2015 22:28
-
-
Save jorgegorka/5854280 to your computer and use it in GitHub Desktop.
Track user interaction with Google Analytics
This file contains 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
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