Skip to content

Instantly share code, notes, and snippets.

@zanonnicola
Created February 6, 2015 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zanonnicola/0c16d2dd1b3bf1520b95 to your computer and use it in GitHub Desktop.
Save zanonnicola/0c16d2dd1b3bf1520b95 to your computer and use it in GitHub Desktop.
Measuring Your Site's Responsive Breakpoint Usage with GA
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