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?
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.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With