Sparkbox 提供了这段代码片段,帮助了解浏览器窗口调整大小的频率。
(function() {
var resizeTimer;
// Assuming we have jQuery present
$( window ).on( "resize", function() {
// Use resizeTimer to throttle the resize handler
clearTimeout( resizeTimer );
resizeTimer = setTimeout(function() {
/* Send the event to Google Analytics
*
* https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiEventTracking
* _trackEvent(category, action, opt_label, opt_value, opt_noninteraction)
*/
var $window = $( window );
_gaq.push( [ "_trackEvent", "User Actions", "Browser Resize", $window.width() + " x " + $window.height() ] );
}, 1000);
});
})();
请注意,在 Google Analytics 中跟踪事件非常简单。这可以用于几乎任何目的。
非常棒的想法。有没有什么理由不使用 resizeTimer 缓存 $(window)?
确实很酷,但跟踪此类事件的目的是什么?