Skip to content

Instantly share code, notes, and snippets.

@timmfin
Created May 4, 2023 15:07
Show Gist options
  • Save timmfin/de343f532b98805e545d167859947f6c to your computer and use it in GitHub Desktop.
Save timmfin/de343f532b98805e545d167859947f6c to your computer and use it in GitHub Desktop.
Emulating crux technology calculation - BigQuery source
CREATE TEMP FUNCTION IS_GOOD(good FLOAT64, needs_improvement FLOAT64, poor FLOAT64) RETURNS BOOL AS (
good / (good + needs_improvement + poor) >= 0.75
);
CREATE TEMP FUNCTION IS_NON_ZERO(good FLOAT64, needs_improvement FLOAT64, poor FLOAT64) RETURNS BOOL AS (
good + needs_improvement + poor > 0
);
SELECT
date,
app,
client,
COUNT(DISTINCT url) AS origins,
COUNT(DISTINCT IF(good_fid, url, NULL)) AS origins_with_good_fid,
COUNT(DISTINCT IF(good_cls, url, NULL)) AS origins_with_good_cls,
COUNT(DISTINCT IF(good_lcp, url, NULL)) AS origins_with_good_lcp,
COUNT(DISTINCT IF(any_fid, url, NULL)) AS origins_with_any_fid,
COUNT(DISTINCT IF(any_cls, url, NULL)) AS origins_with_any_cls,
COUNT(DISTINCT IF(any_lcp, url, NULL)) AS origins_with_any_lcp,
COUNT(DISTINCT IF(good_cwv, url, NULL)) AS origins_with_good_cwv,
COUNT(DISTINCT IF(any_lcp AND any_cls, url, NULL)) AS origins_eligible_for_cwv,
SAFE_DIVIDE(COUNTIF(good_cwv), COUNTIF(any_lcp AND any_cls)) AS pct_eligible_origins_with_good_cwv
FROM (
SELECT
date,
CONCAT(origin, '/') AS url,
IF(device = 'desktop', 'desktop', 'mobile') AS client,
IS_NON_ZERO(fast_fid, avg_fid, slow_fid) AS any_fid,
IS_GOOD(fast_fid, avg_fid, slow_fid) AS good_fid,
IS_NON_ZERO(small_cls, medium_cls, large_cls) AS any_cls,
IS_GOOD(small_cls, medium_cls, large_cls) AS good_cls,
IS_NON_ZERO(fast_lcp, avg_lcp, slow_lcp) AS any_lcp,
IS_GOOD(fast_lcp, avg_lcp, slow_lcp) AS good_lcp,
(IS_GOOD(fast_fid, avg_fid, slow_fid) OR fast_fid IS NULL) AND
IS_GOOD(small_cls, medium_cls, large_cls) AND
IS_GOOD(fast_lcp, avg_lcp, slow_lcp) AS good_cwv
FROM
`chrome-ux-report.materialized.device_summary`
WHERE
date = '2023-03-01'
) JOIN (
SELECT DISTINCT
CAST(REGEXP_REPLACE(_TABLE_SUFFIX, r'(\d)_(\d{2})_(\d{2}).*', r'202\1-\2-\3') AS DATE) AS date,
app,
IF(ENDS_WITH(_TABLE_SUFFIX, 'desktop'), 'desktop', 'mobile') AS client,
url
FROM
`httparchive.technologies.202*`
WHERE
app = 'HubSpot CMS Hub'
) USING (date, url, client)
GROUP BY
date,
app,
client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment