Skip to content

Instantly share code, notes, and snippets.

@averyvery
Created January 13, 2012 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save averyvery/1608795 to your computer and use it in GitHub Desktop.
Save averyvery/1608795 to your computer and use it in GitHub Desktop.
Tracking Window Dimensions with GA
<!DOCTYPE HTML>
<html>
<head>
<title>Tracking Window Size</title>
</head>
<body>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1111111-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/u/ga_debug.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
(function() {
var dimensions = {};
function trackWindowDimensions(){
track('Height', get('Height'));
track('Width', get('Width'));
track('Total Pixels', dimensions['Height'] * dimensions['Width']);
};
function track(name, amount){
_gaq.push(['_trackEvent', 'Window', 'Dimension', name, amount, true]);
};
function get(name){
var key = 'inner' + name,
object = window,
amount;
if(!(key in window)){
key = 'client' + name;
object = document.documentElement || document.body;
}
amount = object[key];
return dimensions[name] = amount;
};
trackWindowDimensions();
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment