1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-23 02:10:01 +01:00
blog/_src/_assets/js/analytics.js

83 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-09-09 19:22:04 +02:00
const krlcAnalytics = (() => { // eslint-disable-line no-unused-vars
//
// Track Responsive Breakpoints
//
// stolen & adapted from
// http://philipwalton.com/articles/measuring-your-sites-responsive-breakpoint-usage/
//
const gaBreakpoints = () => {
// Do nothing in browsers that don't support `window.matchMedia`.
if (!window.matchMedia) {
return
}
2017-09-09 02:44:03 +02:00
2017-09-09 19:22:04 +02:00
// Prevent rapid breakpoint changes for all firing at once.
let timeout
2017-09-09 02:44:03 +02:00
2017-09-09 19:22:04 +02:00
const breakpoints = {
xxs: '(max-width: 479px)',
xs: '(min-width: 480px) and (max-width: 767px)',
sm: '(min-width: 768px) and (max-width: 991px)',
md: '(min-width: 992px) and (max-width: 1199px)',
lg: '(min-width: 1200px) and (max-width: 1599px)',
hg: '(min-width: 1600px)'
2017-09-09 02:44:03 +02:00
}
2017-09-09 19:22:04 +02:00
Object.keys(breakpoints).forEach(breakpoint => {
const mql = window.matchMedia(breakpoints[breakpoint])
// Set the initial breakpoint on page load.
2017-09-09 02:44:03 +02:00
if (mql.matches) {
2017-09-09 19:22:04 +02:00
window.ga('set', 'dimension1', breakpoint)
2017-09-09 02:44:03 +02:00
}
2017-09-09 19:22:04 +02:00
// Update the breakpoint as the matched media changes
mql.addListener(() => {
if (mql.matches) {
clearTimeout(timeout)
timeout = setTimeout(() => {
window.ga('set', 'dimension1', breakpoint)
}, 1000)
}
})
})
}
2017-09-09 19:22:04 +02:00
//
// Track Viewport
//
const gaViewport = () => {
const width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0)
const height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
const dimensions = width + 'x' + height
2017-09-09 19:22:04 +02:00
window.ga('set', 'dimension2', dimensions)
}
2017-09-09 19:22:04 +02:00
//
// Track Pixel Density
//
const gaPixelDensity = () => {
// Heads up!
// window.devicePixelRatio doesn't work correctly in IE but whatever
const pixeldensity = window.devicePixelRatio
2017-09-09 19:22:04 +02:00
window.ga('set', 'dimension3', pixeldensity)
}
2017-09-09 19:22:04 +02:00
return {
2017-09-09 19:34:38 +02:00
init() {
const dnt = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack
if (dnt !== 'yes' && dnt !== '1') {
gaBreakpoints()
gaViewport()
gaPixelDensity()
}
}
2017-09-09 19:22:04 +02:00
}
})(); // eslint-disable-line semi