Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Report viewport resizes (where width or height has changed by 50px or more) and orientation changes as "Viewport" events to Google Analytics, in order to find out on average how many resizes are down to orientation changes and how many are actual window resizes.
var viewportclock,
startwidth = document.documentElement.clientWidth,
startheight = document.documentElement.clientHeight,
widthchange,
heightchange;
var testResize = function() {
widthchange = startwidth - document.documentElement.clientWidth;
heightchange = startheight - document.documentElement.clientHeight;
if (widthchange > 50 || widthchange < -50) {
window.clearTimeout(viewportclock);
_gaq.push(["_trackEvent", "Viewport", "Resize"]);
}
else if (heightchange > 50 || heightchange < -50) {
window.clearTimeout(viewportclock);
_gaq.push(["_trackEvent", "Viewport", "Resize"]);
}
else {
viewportclock = window.setTimeout(testResize, 1000);
}
};
var catchOrientationChange = function() {
window.removeEventListener("orientationchange", catchOrientationChange);
_gaq.push(["_trackEvent", "Viewport", "Orientation Change"]);
};
viewportclock = window.setTimeout(testResize, 1000);
window.addEventListener("orientationchange", catchOrientationChange);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment