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.
然后使用cookie确保“点击”不再被计算。
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.
但是有没有理由不使用AJAX来通知服务器,除了明显的“用户必须启用JavaScript”?我猜这几天几乎所有没戴锡纸帽的人都会在浏览器中启用它。
With AJAX and JavaScript I could do something like this and bring local storage into the mix and reduce some network bandwidth:
使用AJAX和JavaScript,我可以做这样的事情并将本地存储带入混合并减少一些网络带宽:
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?
我对JavaScript方法有什么看法?
1 个解决方案
#1
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.).
我认为2013年的方法是只注册Google Analytics,并将生成的JavaScript粘贴到您的网站中。比滚动您自己的解决方案容易得多,您可以获得丰富的用户数据(人口统计,位置,准确的用户数量等)。
#1
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.).
我认为2013年的方法是只注册Google Analytics,并将生成的JavaScript粘贴到您的网站中。比滚动您自己的解决方案容易得多,您可以获得丰富的用户数据(人口统计,位置,准确的用户数量等)。