Skip to content

Instantly share code, notes, and snippets.

@davidjgoss
Created March 17, 2014 21:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidjgoss/9608808 to your computer and use it in GitHub Desktop.
Save davidjgoss/9608808 to your computer and use it in GitHub Desktop.
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