Public
Edited
Mar 6, 2023
Importers
Insert cell
# gov.uk dpr and viewport width data, july 2021
Insert cell
data = FileAttachment("gov_uk_width_dpr.json").json()
Insert cell
totalSessions = data.reduce( ( totalSessions, o ) => {
return totalSessions + o.sessions;
}, 0 )
Insert cell
Insert cell
roundViewportWidthBy = 10
Insert cell
roundDprBy = 1/32
Insert cell
Insert cell
checker = binned.reduce( ( a, o ) => a + o.sessionsPercent, 0 )
Insert cell
nonBinnedUniqueWidths = processed.reduce( (uniqueWidths, o) => uniqueWidths.add( o.viewportWidth ), new Set() ).size
Insert cell
nonBinnedUniqueDPRs = processed.reduce( (uniqueWidths, o) => uniqueWidths.add( o.dpr ), new Set() ).size
Insert cell
binnedUniqueWidths = binned.reduce( (uniqueWidths, o) => uniqueWidths.add( o.viewportWidth ), new Set() ).size
Insert cell
binnedUniqueDPRs = binned.reduce( (uniqueWidths, o) => uniqueWidths.add( o.dpr ), new Set() ).size
Insert cell
// userData looks like [ { viewportWidth, dpr, sessionsPercent }, +... ], and all percentages add to 1
function weightedRandomUserAgent( userData ) {
let i, sum=0, r=Math.random();
for (i in userData) {
sum += userData[i].sessionsPercent;
if (r <= sum) {
return { viewportWidth: userData[i].viewportWidth, dpr: userData[i].dpr }
};
}
}
Insert cell
weightedRandomUserAgent( binned )
Insert cell
gimme_a_user_agent = function() {
return weightedRandomUserAgent( binned );
}
Insert cell
Insert cell
Insert cell
sortedDprFrequencies = {
const dprs = processed.reduce( (ac, cv) => {
if ( ac[cv.dpr] ) {
ac[cv.dpr] += cv.sessionsPercent;
} else {
ac[cv.dpr] = cv.sessionsPercent;
}
return ac;
}, {} );
return Object.keys(dprs)
.map( dpr => ({ dpr: dpr, sessionsPercent: dprs[dpr] }) )
.sort( (a, b) => b.sessionsPercent - a.sessionsPercent );
}
Insert cell
sortedDprFrequencies.map( d => `${d.dpr}\t${d.sessionsPercent}` ).join('\n')
Insert cell
top = 2
Insert cell
topDprs = sortedDprFrequencies.slice(0, top).map( o => parseFloat(o.dpr) )
Insert cell
sortedDprFrequencies.slice(0, top).reduce( (a, o) => a + o.sessionsPercent, 0 )
Insert cell
sortedDprFrequencies.slice(top, -1).reduce( (a, o) => a + o.sessionsPercent, 0 )
Insert cell
longTail = processed.filter( o => !(topDprs.includes( o.dpr )) )
Insert cell
longTail.reduce( (ac, cv) => ac + cv.sessionsPercent, 0)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more