-
-
Notifications
You must be signed in to change notification settings - Fork 168
/
lcp_lazy.sql
46 lines (42 loc) · 1.43 KB
/
lcp_lazy.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
CREATE TEMP FUNCTION getLoadingAttr(attributes STRING) RETURNS STRING LANGUAGE js AS '''
try {
const data = JSON.parse(attributes);
const loadingAttr = data.find(attr => attr["name"] === "loading")
return loadingAttr.value
} catch (e) {
return "";
}
''';
CREATE TEMP FUNCTION hasLazyHeuristics(attributes STRING) RETURNS BOOLEAN LANGUAGE js AS '''
try {
const data = JSON.parse(attributes);
const classes = data.find(attr => attr["name"] === "class").value;
const hasLazyClasses = classes.indexOf('lazyload') !== -1;
const hasLazySrc = data.includes(attr => attr["name"] === "data-src");
return hasLazyClasses || hasLazySrc;
} catch (e) {
return false;
}
''';
WITH lcp_stats AS (
SELECT
_TABLE_SUFFIX AS client,
getLoadingAttr(JSON_EXTRACT(payload, '$._performance.lcp_elem_stats.attributes')) = 'lazy' AS native_lazy,
hasLazyHeuristics(JSON_EXTRACT(payload, '$._performance.lcp_elem_stats.attributes')) AS custom_lazy
FROM
`httparchive.pages.2022_06_01_*`
WHERE
# <img> LCP only.
JSON_EXTRACT_SCALAR(payload, '$._performance.lcp_elem_stats.nodeName') = 'IMG'
)
SELECT
client,
COUNT(0) AS total,
COUNTIF(native_lazy) / COUNT(0) AS pct_native_lazy,
COUNTIF(custom_lazy) / COUNT(0) AS pct_custom_lazy,
COUNTIF(custom_lazy OR native_lazy) / COUNT(0) AS pct_either_lazy,
COUNTIF(custom_lazy AND native_lazy) / COUNT(0) AS pct_both_lazy
FROM
lcp_stats
GROUP BY
client