Skip to content

Instantly share code, notes, and snippets.

@simondahla
Last active October 18, 2018 14:30
Show Gist options
  • Save simondahla/7888578 to your computer and use it in GitHub Desktop.
Save simondahla/7888578 to your computer and use it in GitHub Desktop.
/*
Google Analytics Classic - Browser Viewport Size
Credit @beantin
CC BY 2.5 2013
*/
function viewport() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement &&
( document.documentElement.clientWidth
|| document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body &&
( document.body.clientWidth
|| document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
_gaq.push(['_trackEvent','Viewport','Size',myWidth+'x'+myHeight,, true]);
_gaq.push(['_trackEvent','Viewport','Width',myWidth+'x'+myHeight, myWidth, true]);
_gaq.push(['_trackEvent','Viewport','Height',myWidth+'x'+myHeight, myHeight, true]);
}
/*
Google Analytics Universal - Browser Viewport Size
Credit @beantin
CC BY 2.5 2013
//This is tracked by Universal Analytics by default
//This is only needed if you want to track this as separte events
*/
function viewport() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement &&
( document.documentElement.clientWidth
|| document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body &&
( document.body.clientWidth
|| document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
//This is tracked by Universal Analytics by default
//This is only needed if you want to track this as separte events
ga('send', 'event', 'Viewport', 'Size', myWidth+'x'+myHeight, {'nonInteraction': 1});
ga('send', 'event', 'Viewport', 'Width', myWidth+'x'+myHeight, myWidth, {'nonInteraction': 1});
ga('send', 'event', 'Viewport', 'Height', myWidth+'x'+myHeight, myHeight, {'nonInteraction': 1});
}
<body onLoad="viewport();">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment