Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a web page hit counter in 2013

I'm looking to implement a web page hit counter to let the server know what pages are being viewed where I'm trying to avoid sending the server repetitive 'user hits' by the same user, same page. (I'm not super concerned about them clearing their cache etc. and possibly getting counted again)

I've generally seen something like this:

<img src="/the-hit-counter?pageId=SOME_PAGE_ID" />

and then use a cookie to make sure the 'hit' doesn't get counted again.

But is there any reason to not use AJAX to notify the server other than the obvious "the user must have JavaScript enabled"? I'm guessing almost everyone that isn't wearing a tin-foil hat these days will have it enabled in their browser.

With AJAX and JavaScript I could do something like this and bring local storage into the mix and reduce some network bandwidth:

if (!amplify.store('SOME_PAGE_ID')) {
    $.get('/the-hit-counter?pageId=SOME_PAGE_ID');
    amplify.store('SOME_PAGE_ID', "");
}

What am I missing about the JavaScript approach?

like image 715
John Culviner Avatar asked Jul 16 '13 00:07

John Culviner


1 Answers

I think the 2013 way to do this is to just sign up for Google Analytics, and paste its generated JavaScript into your site. Much easier than rolling your own solution, and you get a wealth of user data (demographics, locations, accurate user counts, etc.).

like image 136
Geoffrey Booth Avatar answered Oct 07 '22 06:10

Geoffrey Booth