Skip to content

Instantly share code, notes, and snippets.

@nhoizey
Created March 8, 2018 09:20
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 nhoizey/b0ffd6a471ead47532ef766e0a409794 to your computer and use it in GitHub Desktop.
Save nhoizey/b0ffd6a471ead47532ef766e0a409794 to your computer and use it in GitHub Desktop.
Get screen density and viewport width, useful for Analytics
// get device pixel ratio in dppx
// https://github.com/ryanve/res/blob/master/src/index.js
var screen_density =
typeof window == 'undefined'
? 0
: +window.devicePixelRatio ||
Math.sqrt(screen.deviceXDPI * screen.deviceYDPI) / 96 ||
0
// keep only 3 decimals: http://jsfiddle.net/AsRqx/
screen_density = +(Math.round(screen_density + 'e+3') + 'e-3')
// get viewport width
// http://stackoverflow.com/a/8876069/717195
var viewport_width = Math.max(
document.documentElement.clientWidth,
window.innerWidth || 0,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment