Created
March 17, 2014 21:31
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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