Django 中准确的页面浏览量
准确统计页面浏览次数的好方法是什么?
我正在使用姜戈。 具体来说,我不想刷新页面来增加计数。
What is a good approach to keeping accurate counts of how many times a page has been viewed?
I'm using Django. Specifically, I don't want refreshing the page to up the count.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我所知,目前没有浏览器向服务器发送任何类型的消息/标头来说明请求是否来自刷新。
我认为不计算用户刷新页面的唯一方法是跟踪用户查看页面的 IP 和时间,然后,如果用户上次查看页面的时间不到 30 分钟,那么您会将其忽略为刷新并且不增加页面浏览次数。
IMO 无论如何,大多数页面刷新都应该算作页面视图,因为我刷新的唯一原因是查看可能已添加的新数据,或者浏览器崩溃后偶尔的意外刷新/重新加载(上述方法会忽略)。
As far as I'm aware, no browsers out there at the moment send any kind of message/header to the server saying whether the request was from a refresh or not.
The only way I can see to not count a user refreshing the page is to track the IPs and times that a user views a page, and then if the user last viewed the page less than 30 minutes ago, say, you would dismiss it as a refresh and not increment the page view count.
IMO most page refreshes should be counted as a page view anyway, as the only reason I have for refreshing is to see new data that might have been added, or the occasional accidental refresh/reloading after a browser crash (which the above method would dismiss).
您可以为每个用户提供 cookie,该 cookie 在当天结束时到期,其中包含唯一的编号。 如果他重新加载页面,您可以检查当天她是否已被计入。
You could give each user cookie, that expires at the end of the day, containing a unique number. If he reloads a page you can check wether she has been counted already that day.
您可以创建一个包含页面唯一访问者的表,例如 VisitorIP + X-Forwarded-For 内容、User-Agent 字符串以及某种 PageID。 如果数据本身不相关,您可以根据这些值创建 md5/sha1 哈希(当然除了 PageID)。 但请注意,该表将增长得非常快。
我建议不要为此目的设置 cookie。 它们的大小有限,并且用户访问的页面很多,您可能会达到该限制并使解决方案变得不可靠。 此外,它还使得在客户端缓存此类页面变得更加困难(请参阅可缓存性),因为它变得互动内容。
You could create a table with unique visitors of the pages, e.g. VisitorIP + X-Forwarded-For content, User-Agent string along with a PageID of some sorts. If the data itself is irrelevant, you can create a md5/sha1 hash from these values (besides the PageID of course). Be warned however that this table will grow really fast.
I'd advise against setting cookies for that purpose. They have a limited size and with many visited pages by the user, you could reach that limit and make the solution unreliable. Also it makes it harder to cache such page on client-side (see Cacheability), since it becomes interactive content.
您可以编写一个 django 中间件并捕获 request.url,然后设置一个包含 url / accesses 列的表。 注意并发更新的事务。
如果遇到负载问题,可以使用带有 incr 或 add 功能的 memcached 并定期更新数据库表以避免事务锁。
You can write a django middleware and catch request.url, then setup a table with url / accesses columns. Beware of transactions for concurrent update.
If you have load problems, you can use memcached with incr or add function and periodicaly update the database table to avoid transaction locks.