Skip to content

Instantly share code, notes, and snippets.

@tunetheweb
Created May 23, 2020 11:19
Show Gist options
  • Save tunetheweb/55dc5be4be4e48d85925cb583c35e14d to your computer and use it in GitHub Desktop.
Save tunetheweb/55dc5be4be4e48d85925cb583c35e14d to your computer and use it in GitHub Desktop.
Percent of pages that include link[rel=alternate] for stylesheets
#standardSQL
# Percent of pages that include link[rel=alternate] for stylesheets
# Warning this uses 500GB of BigQuery usage so can be expensive to run multiple times!
CREATE TEMP FUNCTION hasAlternateCSS(payload STRING)
RETURNS BOOLEAN LANGUAGE js AS '''
try {
var $ = JSON.parse(payload);
var almanac = JSON.parse($._almanac);
return !!almanac['link-nodes'].find(
e => e.rel.toLowerCase().includes('alternate')
&&
(
e.as.toLowerCase() == 'style' || e.href.toLowerCase().includes('.css') || e.type.toLowerCase() == 'text/css'
)
);
} catch (e) {
return false;
}
''';
SELECT
_TABLE_SUFFIX AS client,
COUNTIF(hasAlternateCSS(payload)) AS num_pages,
COUNT(0) AS total,
ROUND(COUNTIF(hasAlternateCSS(payload)) * 100 / COUNT(0), 2) AS pct_alternate_css
FROM
`httparchive.pages.2020_05_01_*`
GROUP BY
client
@tunetheweb
Copy link
Author

Results as of 1st May 2020:

Row client num_pages total pct_alternate_css  
1 mobile 3 5349874 0.0  
2 desktop 2 4507943 0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment