uxm
Version:
User experience metrics
2 lines (1 loc) • 3.23 kB
JavaScript
var n="undefined"!=typeof window?window.performance:null;function e(n){void 0===n&&(n={});var t={deviceType:f(),effectiveConnectionType:i(),timeToFirstByte:u(),firstPaint:o(),firstContentfulPaint:a(),domContentLoaded:d(),onLoad:l()};return t.onLoad?((n.url||n.all)&&(t.url=c()),(n.userAgent||n.all)&&(t.userAgent=s()),(n.deviceMemory||n.all)&&(t.deviceMemory=m()),(n.userTiming||n.all)&&(t.userTiming=p()),(n.longTasks||n.all)&&(t.longTasks=y()),(n.resources||n.all)&&(t.resources=v()),Promise.resolve(t)):new Promise(function(n){return setTimeout(n,250)}).then(function(){return e(n)})}function t(e){n&&n.mark&&n.mark(e)}function r(e,t){if(n&&n.measure)try{n.measure(e,t)}catch(n){console.error(n)}}function i(){var n="undefined"!=typeof navigator?navigator.connection||navigator.mozConnection||navigator.webkitConnection:null;return n?n.effectiveType:null}function o(){if(!n||void 0===window.PerformancePaintTiming)return null;var e=n.getEntriesByType("paint").find(function(n){return"first-paint"===n.name});return e?Math.round(e.startTime):null}function a(){if(!n||void 0===window.PerformancePaintTiming)return null;var e=n.getEntriesByType("paint").find(function(n){return"first-contentful-paint"===n.name});return e?Math.round(e.startTime):null}function u(){if(!n)return null;var e=n.getEntriesByType("navigation");return e&&e[0]?Math.round(e[0].responseStart):null}function d(){if(!n)return null;var e=n.getEntriesByType("navigation");return e&&e[0]?Math.round(e[0].domContentLoadedEventEnd):null}function l(){if(!n)return null;var e=n.getEntriesByType("navigation");return e&&e[0]?Math.round(e[0].loadEventEnd):null}function f(n){void 0===n&&(n=s()),n=n.toLowerCase();var e=function(e){return-1!==n.indexOf(e)},t=e("windows"),r=t&&e("phone"),i=t&&e("touch")&&!r,o=!t&&e("iphone"),a=e("ipod"),u=e("ipad"),d=!t&&e("android"),l=d&&e("mobile"),f=d&&!e("mobile");return l||o||a||r?"phone":u||f||i?"tablet":"desktop"}function c(){return window.location.href}function s(){return window.navigator.userAgent}function m(){var n="undefined"!=typeof navigator?navigator.deviceMemory:void 0;return void 0===n?null:n>1?"full":"lite"}function p(){if(!n||"undefined"==typeof PerformanceMark)return null;var e=n.getEntriesByType("mark").map(function(n){return{type:"mark",name:n.name,startTime:Math.round(n.startTime)}}),t=n.getEntriesByType("measure").map(function(n){return{type:"measure",name:n.name,startTime:Math.round(n.startTime),duration:Math.round(n.duration)}});return e.concat(t)}function v(){return n&&"undefined"!=typeof PerformanceResourceTiming?n.getEntriesByType("navigation").concat(n.getEntriesByType("resource")).map(function(n){return{url:n.name,type:n.initiatorType,size:n.transferSize,startTime:Math.round(n.startTime),duration:Math.round(n.duration)}}):null}function y(){return void 0===window.__lt?null:window.__lt.e.map(function(n){return{startTime:Math.round(n.startTime),duration:Math.round(n.duration)}})}export{e as uxm,t as mark,r as measure,i as getEffectiveConnectionType,o as getFirstPaint,a as getFirstContentfulPaint,u as getTimeToFirstByte,d as getDomContentLoaded,l as getOnLoad,f as getDeviceType,c as getUrl,s as getUserAgent,m as getDeviceMemory,p as getUserTiming,v as getResources,y as getLongTasks};