Skip to content

Instantly share code, notes, and snippets.

@mmas
Created July 4, 2017 23:49
Show Gist options
  • Save mmas/bc093459825bc279cbaaee29d7440736 to your computer and use it in GitHub Desktop.
Save mmas/bc093459825bc279cbaaee29d7440736 to your computer and use it in GitHub Desktop.
var data, ua, platform, browser, os;
data = {
page: window.location.pathname.replace(/\/$/, ''),
language: navigator.language.slice(0, 2).toUpperCase(),
screen_resolution: window.screen.width + 'x' + window.screen.height,
screen_dpi: window.devicePixelRatio,
is_touch_device: 'ontouchstart' in window || 'onmsgesturechange' in window
};
ua = navigator.userAgent.toLowerCase();
platform = navigator.platform.toLowerCase();
browser = ua.match(/(opera|chrome|safari|firefox|msie|trident)\/?\s*([\d\.]+)/) || [];
if (/trident/.test(browser[1])) browser = 'ie';
else if (browser[2]) browser = browser[1];
else if (! browser.length) browser = 'other';
if (browser == 'msie') browser = 'ie';
data.browser = browser.toLowerCase();
if (ua.match(/android/)) os = 'android';
else if (ua.match(/(iphone|ipod|ipad)/)) os = 'ios';
else if (ua.match(/windows phone/)) os = 'windows-phone';
else if (ua.match(/blackberry/)) os = 'blackberry';
else if (platform.match(/mac/)) os = 'mac';
else if (platform.match(/win/)) os = 'windows';
else if (platform.match(/linux/)) os = 'linux';
else os = 'other';
data.os = os;
$.ajax({
url: '/analytics',
data: JSON.stringify(data),
type: 'POST'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment