Created
October 11, 2017 22:04
-
-
Save nuclearghost/479698cbaac38348832a1d2548db6bfa to your computer and use it in GitHub Desktop.
Parse Cloud Front User Agents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require('fs'); | |
parse = require('user-agent-parser'); | |
// parse('user agent string') | |
var total = 0, | |
iOS = 0, | |
iOS10 = 0, | |
iOS9 = 0, | |
Android = 0, | |
AndroidChrome52 = 0, | |
AndroidChrome53 = 0, | |
AndroidOther = 0, | |
nonMobile = 0; | |
fs.readFile('data2', 'utf8', function(err, data) { | |
if (err) { | |
return console.log(err); | |
} | |
let lines = data.split('\n'); | |
lines.forEach(function(line) { | |
let words = line.split('\t'); | |
if (words[0] !== '2017-09-26') { | |
return; | |
} | |
total += 1; | |
let ua = decodeUA(words[10]); | |
parsed = parse(ua); | |
if (parsed.os.name === 'Android') { | |
updateAndroidCounts(parsed); | |
} else if (parsed.os.name === 'iOS') { | |
updateiOSCounts(parsed); | |
} else { | |
nonMobile += 1; | |
} | |
}); | |
printVariables(); | |
}); | |
function decodeUA(ua) { | |
try { | |
return decodeURIComponent(decodeURIComponent(ua)); | |
} catch (e) { | |
console.log(ua); | |
return ''; | |
} | |
} | |
function updateAndroidCounts(parsed) { | |
Android += 1; | |
if (parsed.browser.name === 'Chrome') { | |
let major = parseInt(parsed.browser.major); | |
if (major >= 53) { | |
AndroidChrome53 += 1; | |
} else { | |
AndroidChrome52 += 1; | |
} | |
} else { | |
AndroidOther += 1; | |
} | |
} | |
function updateiOSCounts(parsed) { | |
iOS += 1; | |
let version = parseInt(parsed.os.version); | |
if (version >= 10) { | |
iOS10 += 1; | |
} else { | |
iOS9 += 1; | |
} | |
} | |
function printVariables() { | |
console.log(`Total ${total}`); | |
console.log(`iOS Total ${iOS}`); | |
console.log(`iOS 10 or greater ${iOS10}`); | |
console.log(`iOS 9 or less ${iOS9}`); | |
console.log(`Android Total ${Android}`); | |
console.log(`Android Chrome 53 or later ${AndroidChrome53}`); | |
console.log(`Android Chrome 52 or earleir ${AndroidChrome52}`); | |
console.log(`Android Non Chrome ${AndroidOther}`); | |
console.log(`Non Mobile Total ${nonMobile}`); | |
console.log( | |
`Percentage that needs sprites ${(iOS9 + AndroidOther + AndroidChrome52) / | |
total}` | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment