最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Implementing a web page hit counter in 2013 - Stack Overflow

programmeradmin1浏览0评论

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'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?

Share asked Jul 16, 2013 at 0:37 John CulvinerJohn Culviner 23k6 gold badges58 silver badges53 bronze badges 4
  • can't really expect everyone to be using javascript. security isn't the only reason why it wouldn't be enabled. – Justin L. Commented Jul 16, 2013 at 1:49
  • I suppose. Google Analytics (one of the answers below) requires it though even as far as I can tell! – John Culviner Commented Jul 17, 2013 at 0:15
  • @JohnCulviner But GA does not seem to provide a way to display hit count on your web page. Any ideas? – Jasper Commented Mar 4, 2016 at 11:09
  • DIY from scratch is what I did! – John Culviner Commented Mar 4, 2016 at 17:52
Add a ment  | 

1 Answer 1

Reset to default 9

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.).

发布评论

评论列表(0)

  1. 暂无评论