You can also change the function to work with matchMedia and resolutions with dpi instead of devicePixelRatio to detect if it's retina or not:
Create Custom JavaScript variable, pixel_ratio
:
function () {
//Configure resolutions here, will return value of ratio:
var pixel_ratio = [
{'pixel_ratio': 'all', 'ratio': 1},
{'pixel_ratio': '(min-resolution: 144dpi)', 'ratio': 1.5},
{'pixel_ratio': '(min-resolution: 192dpi)', 'ratio': 2}
];
var result;
pixel_ratio.forEach(function (element) {
var ratio = 1;
//If browser support matchMedia, use it
if (window.matchMedia) {
// return current media query
if (window.matchMedia(element.pixel_ratio).matches) {
result = element.ratio;
}
} else { //matchMedia not supported
if (window.screen.systemXDPI !== undefined
&& window.screen.logicalXDPI !== undefined
&& window.screen.systemXDPI > window.screen.logicalXDPI) {
// Only allow for values > 1
ratio = window.screen.systemXDPI / window.screen.logicalXDPI;
} else if (window.devicePixelRatio !== undefined) {
ratio = window.devicePixelRatio;
}
if (element.ratio === (Math.round(ratio * 100) / 100)) {
result = element.ratio;
}
}
});
return result + 'x';
}
Create custom dimensions in GA for pixel_ratio
. Select scope as “Hit”. Note the index value for each dimension.
In GTM, configure the custom dimensions in your base GA pageview tag, matching the dimension index value from GA.