I know that it is hard to believe but sometimes you might need more than Google analytics to help determine users’ interactions on a site. For example, suppose you have several links on your page that allow users to move up and down to certain sections on the page. Or suppose you have a single page app that the users stays on and completes everything on one page. You could investigate Google’s _trackEvent(category, action, opt_label, opt_value, opt_noninteraction) but maybe this is an internal web site that you really don’t want Google to track. Using a little jquery and some code you can create your very own analytics.
This article is not a step by step article but rather an article to get you jump started in the right direction. It hopefully will also give you some ideas on how you can use jquery to really get a handle on what your users are doing and where they are going. You could even capture the length of time they spend in a particular area.
This script below attaches a click event on anything that has a ‘#’ as it’s reference. You could add this to images, lines in a table grid, items added to a cart but not actually bought, etc.
[code language=”js”]
<!– Example jQuery Reference –>
<script src="latest-jquery.min.js"></script>
<!– Your capture script –>
<script type=’text/javascript’>
$(function(){
// When a link with an anchor is clicked
$(‘a[href*="#"]’).click(function(e){
// Capture other information such as the
// Amount of time spent (Begin and Ending Times)
// Time of Day
// Browser information
// etc.
var timeOfDay = getDate();
$.post("/api/Analytics", { ‘timeStarted’: timeOfDay })
.done(function (data) {
alert(‘Data Recorded’)
})
.fail(function () {
alert("Data Not Recorded");
})
.always(function () {
});
});
});
</script>
[/code]
Your server side code would look something like this.
[code language=”vb”]
Public Function PostValue(ByVal timeStarted As AnalyticClass) As HttpResponseMessage
–Code goes here to save to database
End Function
End Class
Public Class AnalyticClass
Public Property timestarted As String
—Other Items—-
End Class
[/code]
Looking for quality web hosting? Look no further than Arvixe Web Hosting!