Created
February 6, 2015 10:19
-
-
Save zanonnicola/0c16d2dd1b3bf1520b95 to your computer and use it in GitHub Desktop.
Measuring Your Site's Responsive Breakpoint Usage with GA
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 timeout; | |
var breakpoints = { | |
xs: '(max-width: 419px)', | |
sm: '(min-width: 420px) and (max-width: 569px)', | |
md: '(min-width: 570px) and (max-width: 799px)', | |
lg: '(min-width: 800px) and (max-width: 999px)', | |
xl: '(min-width: 1000px)' | |
}; | |
Object.keys(breakpoints).forEach(function(breakpoint) { | |
var mql = window.matchMedia(breakpoints[breakpoint]); | |
// Set the initial breakpoint on page load. | |
if (mql.matches) { | |
ga('set', 'dimensionN', breakpoint); | |
} | |
// Update the breakpoint as the matched media changes, and send an event. | |
mql.addListener(function() { | |
if (mql.matches) { | |
clearTimeout(timeout); | |
timeout = setTimeout(function() { | |
ga('set', 'dimensionN', breakpoint); | |
ga('send', 'event', 'breakpoint', 'change', breakpoint); | |
}, 1000); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment