Skip to content

Instantly share code, notes, and snippets.

@rviscomi
Last active September 15, 2020 04:57
Show Gist options
  • Save rviscomi/71328c6b395f377e7d7f6c7be5ab6da7 to your computer and use it in GitHub Desktop.
Save rviscomi/71328c6b395f377e7d7f6c7be5ab6da7 to your computer and use it in GitHub Desktop.
Reusing the 2019 approach to detecting custom property adoption, the results are: 14.47% desktop and 19.29% mobile. Using the new "css-variables" custom metric, the results are: 27.27% desktop and 26.50% mobile.
#standardSQL
# 02_01: % of sites that use custom properties.
CREATE TEMPORARY FUNCTION usesCustomProps(css STRING)
RETURNS BOOLEAN LANGUAGE js AS '''
try {
var reduceValues = (values, rule) => {
if ('rules' in rule) {
return rule.rules.reduce(reduceValues, values);
}
if (!('declarations' in rule)) {
return values;
}
return values.concat(rule.declarations.filter(d => d.property.startsWith(`--`)));
};
var $ = JSON.parse(css);
return $.stylesheet.rules.reduce(reduceValues, []).length > 0;
} catch (e) {
return false;
}
''';
SELECT
client,
COUNTIF(num_stylesheets > 0) AS freq,
total,
ROUND(COUNTIF(num_stylesheets > 0) * 100 / total, 2) AS pct
FROM (
SELECT
client,
page,
COUNTIF(usesCustomProps(css)) AS num_stylesheets
FROM
`httparchive.almanac.parsed_css`
WHERE
date = '2020-08-01'
GROUP BY
client,
page)
JOIN
(SELECT _TABLE_SUFFIX AS client, COUNT(0) AS total FROM `httparchive.summary_pages.2020_08_01_*` GROUP BY client)
USING
(client)
GROUP BY
client,
total
#standardSQL
# Percent of pages that use custom properties.
CREATE TEMPORARY FUNCTION getCustomPropertyCount(payload STRING) RETURNS INT64 LANGUAGE js AS '''
try {
var $ = JSON.parse(payload);
var vars = JSON.parse($['_css-variables']);
return Object.values(vars.summary).length;
} catch (e) {
return 0;
}
''';
SELECT
client,
COUNTIF(has_custom_properties) AS freq,
total,
COUNTIF(has_custom_properties) / total AS pct
FROM (
SELECT
_TABLE_SUFFIX AS client,
getCustomPropertyCount(payload) > 0 AS has_custom_properties
FROM
`httparchive.pages.2020_08_01_*`)
JOIN (
SELECT
_TABLE_SUFFIX AS client,
COUNT(DISTINCT url) AS total
FROM
`httparchive.pages.2020_08_01_*`
GROUP BY
_TABLE_SUFFIX)
USING (client)
GROUP BY
client,
total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment