使用 JavaScript 记录热图的用户数据
我想知道像crazyegg.com 这样的网站如何在会话期间存储用户点击数据。显然,有一些底层脚本存储每次点击数据,但是如何将这些数据填充到数据库中?在我看来,简单的解决方案是通过 AJAX 发送数据,但是当您考虑到几乎不可能获得跨浏览器页面卸载功能设置时,我想知道是否还有其他更高级的方法来获取指标数据。
我什至看到一个记录每次鼠标移动的网站,我猜他们肯定不会在每次鼠标移动事件时将该数据发送到数据库。
那么,简而言之,我需要什么样的技术来监控网站上的用户活动,然后存储这些信息以创建指标数据?我并不是想重新创建 GA,我只是很想知道这种事情是如何完成的。
提前致谢
I was wondering how sites such as crazyegg.com store user click data during a session. Obviously there is some underlying script which is storing each clicks data, but how is that data then populated into a database? It seems to me the simple solution would be to send data via AJAX but when you consider that it's almost impossible to get a cross browser page unload function setup, I'm wondering if there is perhaps some other more advanced way of getting metric data.
I even saw a site which records each mouse movement and I am guessing they are definitely not sending that data to a database on each mouse move event.
So, in a nutshell, what kind of technology would I need in order to monitor user activity on my site and then store this information in order to create metric data? I am not looking to recreate GA, I'm just very interested to know how this sort of thing is done.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
事实证明,热图分析比仅仅捕获光标坐标要复杂得多。有些网站是右对齐,有些是左对齐,有些是100%宽度,有些是固定宽度“居中”……页面元素可以绝对定位,也可以相对定位,浮动等等。哦,还有不同的屏幕分辨率,甚至多显示器配置。
它在 HeatTest 中的工作原理(我是创始人之一,根据规则必须透露这一点):
document.onclick = function(e){ }
(这不适用于和
> 元素,必须破解你的方法)
//body/div[3]/button[id=search 的形式记录被单击元素的 XPath 地址(因为坐标不可靠,见上文) ]
和元素内的坐标。现在,有趣的部分 - 服务器。
它需要大量的CPU 功率和内存使用。 很多。因此,大多数热图服务(包括我们和 CrazyEgg)都拥有用于此任务的虚拟机和云服务器堆栈。
Heatmap analytics turns out to be WAY more complicated than just capturing the cursor coordinates. Some websites are right-aligned, some are left-aligned, some are 100%-width, some are fixed-width-"centered"... A page element can be positioned absolutely or relatively, floated etc. Oh, and there's also different screen resolutions and even multi-monitor configurations.
Here's how it works in HeatTest (I'm one of the founders, have to reveal that due to the rules):
document.onclick = function(e){ }
(this will not work with<a>
and<input>
elements, have to hack your way around)//body/div[3]/button[id=search]
and the coordinates within the element.Now, the interesting part - the server.
It takes a lot of cpu-power and memory usage. A lot. So most of the heatmap-services including both us and CrazyEgg, have stacks of virtual machines and cloud servers for this task.
许多跟踪系统使用的基本思想是使用 1x1px 图像,通过额外的 GET 参数请求该图像。该请求被添加到服务器日志文件中,然后处理日志文件以生成一些统计信息。
因此,简约的点击跟踪功能可能如下所示:
AJAX 不会有用,因为它受同源策略的约束(您将无法向跟踪服务器发送请求)。并且您必须将 AJAX 代码添加到您的跟踪脚本中。
如果您想发送更多数据(例如光标移动),您可以将坐标存储在变量中,并定期轮询 GET 参数中具有更新路径的新图像。
现在有很多很多问题:
当您制定了跟踪脚本后,您只需要创建一个工具来获取原始服务器日志并将其转换为闪亮的热图: )
The fundamental idea used by many tracking systems uses a 1x1px image which is requested with extra GET parameters. The request is added to server log file, then log files are processed to generate some statistics.
So a minimalist click tracking function might look like this:
AJAX wouldn't be useful because it is subject to same-origin policy (you won't be able to send requests to your tracking server). And you'd have to add AJAX code to your tracking script.
If you want to send more data (like cursor movements) you'd store the coordinates in a variable and periodically poll for a new image with updated path in the GET parameter.
Now there are many many problems:
When you have the tracking script worked out you only need to create a tool that takes raw server logs and turns them into shiny heatmaps :)
不知道crazyegg是如何做到这一点的确切实现细节,但我这样做的方法是将鼠标事件存储在一个数组中,我会通过AJAX定期发送到后端 - 例如,收集并发送捕获的鼠标事件每 30 秒发送一次到服务器。这减少了为每个事件创建请求的压力,但也确保我最多只会丢失 30 秒的数据。您还可以将发送添加到卸载事件,这会增加您获得的数据量,但您不会依赖它。
一些关于我如何实现它的例子(使用 jQuery,因为我的普通 JS 技能有点生疏):
请注意,我还没有以任何方式测试或尝试过这个,但这应该给你一个总体的想法。
Don't know the exact implementation details of how crazyegg does it, but the way I would do it is to store mouse events in an array which I'd send periodically over AJAX to the backend – e.g. the captured mouse events are collected and sent every 30 seconds to the server. This recudes the strain of creating a request for every event, but it also ensures that I will only lose 30 seconds of data at maximum. You can also add the sending to the unload event which increases the amount of data you get, but you wouldn't be dependent on it.
Some example on how I'd implement it (using jQuery as my vanilla JS skills are a bit rusty):
Note that I haven't tested or tried this in any way but this should give you a general idea.
如果您只是寻求交互,则可以将
替换为
。这些将自动与用户单击位置的 X、Y 坐标一起提交。
jQuery 还很好地实现了 mousemove 事件绑定,可以跟踪当前鼠标位置。我不知道你想要的最终结果,但你可以 setTimeOut(submitMousePosition, 1000) 每秒发送一个带有鼠标位置的 ajax 调用或类似的东西。
If you're just looking for interaction, you could replace your
<input type="button">
with<input type="image">
. These are automatically submitted with the X, Y coordinates of where the user has clicked.jQuery also has a good implementation of the mousemove event binding that can track the current mouse position. I don't know your desired end result, but you could setTimeOut(submitMousePosition, 1000) to send an ajax call with the mouse position every second or something like that.
我真的不明白为什么您认为不可能将一个用户会话中的所有点击点存储到数据库中?
他们的座右铭是“查看人们点击的位置”
一旦收集到足够的数据,就可以很容易地在批处理过程中制作热图。
人们确实低估了数据库、索引和分片。这里唯一困难的是为底层架构筹集足够的资金:)
I really don't see why do you think that is impossible to store all click points in one user session to the database?
Their moto is "See Where People Click"
Once when you gather enough data it is fairly easy to make heat maps in batch processes.
People are really underestimating databases, indexing and sharding. The only hard thing here is to gather enough money for underlying architecture :)