|
| 1 | +#standardSQL |
| 2 | +-- Core web vitals by Shopify theme |
| 3 | +CREATE TEMP FUNCTION IS_GOOD(good FLOAT64, needs_improvement FLOAT64, poor FLOAT64) RETURNS BOOL AS ( |
| 4 | + good / (good + needs_improvement + poor) >= 0.75 |
| 5 | +); |
| 6 | + |
| 7 | +CREATE TEMP FUNCTION IS_POOR(good FLOAT64, needs_improvement FLOAT64, poor FLOAT64) RETURNS BOOL AS ( |
| 8 | + poor / (good + needs_improvement + poor) > 0.25 |
| 9 | +); |
| 10 | + |
| 11 | +CREATE TEMP FUNCTION IS_NON_ZERO(good FLOAT64, needs_improvement FLOAT64, poor FLOAT64) RETURNS BOOL AS ( |
| 12 | + good + needs_improvement + poor > 0 |
| 13 | +); |
| 14 | + |
| 15 | +-- All Shopify shops in HTTPArchive |
| 16 | +WITH archive_pages AS ( |
| 17 | + SELECT |
| 18 | + client, |
| 19 | + page AS url, |
| 20 | + JSON_VALUE(custom_metrics, '$.ecommerce.Shopify.theme.name') AS theme_name, |
| 21 | + JSON_VALUE(custom_metrics, '$.ecommerce.Shopify.theme.theme_store_id') AS theme_store_id |
| 22 | + FROM |
| 23 | + `httparchive.all.pages` |
| 24 | + WHERE |
| 25 | + date = DATE(REPLACE('${YYYY_MM_DD}', '_', '-')) AND |
| 26 | + is_root_page AND |
| 27 | + JSON_VALUE(custom_metrics, '$.ecommerce.Shopify.theme.name') IS NOT NULL --first grab all shops for market share |
| 28 | +) |
| 29 | + |
| 30 | +SELECT |
| 31 | + client, |
| 32 | + archive_pages.theme_store_id AS id, |
| 33 | + theme_names.theme_name AS top_theme_name, |
| 34 | + COUNT(DISTINCT origin) AS origins, |
| 35 | + -- Origins with good LCP divided by origins with any LCP. |
| 36 | + SAFE_DIVIDE( |
| 37 | + COUNT(DISTINCT IF(IS_GOOD(fast_lcp, avg_lcp, slow_lcp), origin, NULL)), |
| 38 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_lcp, avg_lcp, slow_lcp), origin, NULL)) |
| 39 | + ) AS pct_good_lcp, |
| 40 | + -- Origins with needs improvement are anything not good, nor poor. |
| 41 | + 1 - |
| 42 | + SAFE_DIVIDE( |
| 43 | + COUNT(DISTINCT IF(IS_GOOD(fast_lcp, avg_lcp, slow_lcp), origin, NULL)), |
| 44 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_lcp, avg_lcp, slow_lcp), origin, NULL)) |
| 45 | + ) |
| 46 | + - |
| 47 | + SAFE_DIVIDE( |
| 48 | + COUNT(DISTINCT IF(IS_POOR(fast_lcp, avg_lcp, slow_lcp), origin, NULL)), |
| 49 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_lcp, avg_lcp, slow_lcp), origin, NULL))) |
| 50 | + AS pct_ni_lcp, |
| 51 | + -- Origins with poor LCP divided by origins with any LCP. |
| 52 | + SAFE_DIVIDE( |
| 53 | + COUNT(DISTINCT IF(IS_POOR(fast_lcp, avg_lcp, slow_lcp), origin, NULL)), |
| 54 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_lcp, avg_lcp, slow_lcp), origin, NULL)) |
| 55 | + ) AS pct_poor_lcp, |
| 56 | + |
| 57 | + -- Origins with good TTFB divided by origins with any TTFB. |
| 58 | + SAFE_DIVIDE( |
| 59 | + COUNT(DISTINCT IF(IS_GOOD(fast_ttfb, avg_ttfb, slow_ttfb), origin, NULL)), |
| 60 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_ttfb, avg_ttfb, slow_ttfb), origin, NULL)) |
| 61 | + ) AS pct_good_ttfb, |
| 62 | + -- Origins with needs improvement are anything not good, nor poor. |
| 63 | + 1 - |
| 64 | + SAFE_DIVIDE( |
| 65 | + COUNT(DISTINCT IF(IS_GOOD(fast_ttfb, avg_ttfb, slow_ttfb), origin, NULL)), |
| 66 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_ttfb, avg_ttfb, slow_ttfb), origin, NULL)) |
| 67 | + ) |
| 68 | + - |
| 69 | + SAFE_DIVIDE( |
| 70 | + COUNT(DISTINCT IF(IS_POOR(fast_ttfb, avg_ttfb, slow_ttfb), origin, NULL)), |
| 71 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_ttfb, avg_ttfb, slow_ttfb), origin, NULL))) |
| 72 | + AS pct_ni_ttfb, |
| 73 | + -- Origins with poor TTFB divided by origins with any TTFB. |
| 74 | + SAFE_DIVIDE( |
| 75 | + COUNT(DISTINCT IF(IS_POOR(fast_ttfb, avg_ttfb, slow_ttfb), origin, NULL)), |
| 76 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_ttfb, avg_ttfb, slow_ttfb), origin, NULL)) |
| 77 | + ) AS pct_poor_ttfb, |
| 78 | + |
| 79 | + -- Origins with good FCP divided by origins with any FCP. |
| 80 | + SAFE_DIVIDE( |
| 81 | + COUNT(DISTINCT IF(IS_GOOD(fast_fcp, avg_fcp, slow_fcp), origin, NULL)), |
| 82 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_fcp, avg_fcp, slow_fcp), origin, NULL)) |
| 83 | + ) AS pct_good_fcp, |
| 84 | + -- Origins with needs improvement are anything not good, nor poor. |
| 85 | + 1 - |
| 86 | + SAFE_DIVIDE( |
| 87 | + COUNT(DISTINCT IF(IS_GOOD(fast_fcp, avg_fcp, slow_fcp), origin, NULL)), |
| 88 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_fcp, avg_fcp, slow_fcp), origin, NULL)) |
| 89 | + ) |
| 90 | + - |
| 91 | + SAFE_DIVIDE( |
| 92 | + COUNT(DISTINCT IF(IS_POOR(fast_fcp, avg_fcp, slow_fcp), origin, NULL)), |
| 93 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_fcp, avg_fcp, slow_fcp), origin, NULL))) |
| 94 | + AS pct_ni_fcp, |
| 95 | + -- Origins with poor FCP divided by origins with any FCP. |
| 96 | + SAFE_DIVIDE( |
| 97 | + COUNT(DISTINCT IF(IS_POOR(fast_fcp, avg_fcp, slow_fcp), origin, NULL)), |
| 98 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_fcp, avg_fcp, slow_fcp), origin, NULL)) |
| 99 | + ) AS pct_poor_fcp, |
| 100 | + |
| 101 | + -- Origins with good INP divided by origins with any INP. |
| 102 | + SAFE_DIVIDE( |
| 103 | + COUNT(DISTINCT IF(IS_GOOD(fast_inp, avg_inp, slow_inp), origin, NULL)), |
| 104 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_inp, avg_inp, slow_inp), origin, NULL)) |
| 105 | + ) AS pct_good_inp, |
| 106 | + -- Origins with needs improvement are anything not good, nor poor. |
| 107 | + 1 - |
| 108 | + SAFE_DIVIDE( |
| 109 | + COUNT(DISTINCT IF(IS_GOOD(fast_inp, avg_inp, slow_inp), origin, NULL)), |
| 110 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_inp, avg_inp, slow_inp), origin, NULL)) |
| 111 | + ) |
| 112 | + - |
| 113 | + SAFE_DIVIDE( |
| 114 | + COUNT(DISTINCT IF(IS_POOR(fast_inp, avg_inp, slow_inp), origin, NULL)), |
| 115 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_inp, avg_inp, slow_inp), origin, NULL))) |
| 116 | + AS pct_ni_inp, |
| 117 | + -- Origins with poor INP divided by origins with any INP. |
| 118 | + SAFE_DIVIDE( |
| 119 | + COUNT(DISTINCT IF(IS_POOR(fast_inp, avg_inp, slow_inp), origin, NULL)), |
| 120 | + COUNT(DISTINCT IF(IS_NON_ZERO(fast_inp, avg_inp, slow_inp), origin, NULL)) |
| 121 | + ) AS pct_poor_inp, |
| 122 | + |
| 123 | + -- Origins with good CLS divided by origins with any CLS. |
| 124 | + SAFE_DIVIDE( |
| 125 | + COUNT(DISTINCT IF(IS_GOOD(small_cls, medium_cls, large_cls), origin, NULL)), |
| 126 | + COUNT(DISTINCT IF(IS_NON_ZERO(small_cls, medium_cls, large_cls), origin, NULL)) |
| 127 | + ) AS pct_good_cls, |
| 128 | + -- Origins with needs improvement are anything not good, nor poor. |
| 129 | + 1 - |
| 130 | + SAFE_DIVIDE( |
| 131 | + COUNT(DISTINCT IF(IS_GOOD(small_cls, medium_cls, large_cls), origin, NULL)), |
| 132 | + COUNT(DISTINCT IF(IS_NON_ZERO(small_cls, medium_cls, large_cls), origin, NULL)) |
| 133 | + ) |
| 134 | + - |
| 135 | + SAFE_DIVIDE( |
| 136 | + COUNT(DISTINCT IF(IS_POOR(small_cls, medium_cls, large_cls), origin, NULL)), |
| 137 | + COUNT(DISTINCT IF(IS_NON_ZERO(small_cls, medium_cls, large_cls), origin, NULL))) |
| 138 | + AS pct_ni_cls, |
| 139 | + -- Origins with poor CLS divided by origins with any CLS. |
| 140 | + SAFE_DIVIDE( |
| 141 | + COUNT(DISTINCT IF(IS_POOR(small_cls, medium_cls, large_cls), origin, NULL)), |
| 142 | + COUNT(DISTINCT IF(IS_NON_ZERO(small_cls, medium_cls, large_cls), origin, NULL)) |
| 143 | + ) AS pct_poor_cls, |
| 144 | + |
| 145 | + -- Origins with good LCP, INP (optional), and CLS divided by origins with any LCP and CLS. |
| 146 | + SAFE_DIVIDE( |
| 147 | + COUNT(DISTINCT IF( |
| 148 | + IS_GOOD(fast_lcp, avg_lcp, slow_lcp) AND |
| 149 | + IS_GOOD(fast_inp, avg_inp, slow_inp) IS NOT FALSE AND |
| 150 | + IS_GOOD(small_cls, medium_cls, large_cls), origin, NULL |
| 151 | + )), |
| 152 | + COUNT(DISTINCT IF( |
| 153 | + IS_NON_ZERO(fast_lcp, avg_lcp, slow_lcp) AND |
| 154 | + IS_NON_ZERO(small_cls, medium_cls, large_cls), origin, NULL |
| 155 | + )) |
| 156 | + ) AS pct_good_cwv |
| 157 | +FROM |
| 158 | + `chrome-ux-report.materialized.device_summary` |
| 159 | +JOIN archive_pages |
| 160 | +ON |
| 161 | + CONCAT(origin, '/') = url AND |
| 162 | + IF(device = 'desktop', 'desktop', 'mobile') = client |
| 163 | +JOIN ( |
| 164 | + -- Add in top theme name for a theme store id AS this should usually be the real theme name |
| 165 | + SELECT |
| 166 | + COUNT(DISTINCT url) AS pages_count, |
| 167 | + theme_store_id, |
| 168 | + theme_name, |
| 169 | + row_number() OVER (PARTITION BY theme_store_id ORDER BY COUNT(DISTINCT url) DESC) AS rank |
| 170 | + FROM archive_pages |
| 171 | + GROUP BY |
| 172 | + theme_store_id, |
| 173 | + theme_name |
| 174 | + ORDER BY COUNT(DISTINCT url) DESC |
| 175 | +) theme_names |
| 176 | +-- Include null theme store ids so that we can get full market share within CrUX |
| 177 | +ON IFNULL(theme_names.theme_store_id, 'N/A') = IFNULL(archive_pages.theme_store_id, 'N/A') |
| 178 | +WHERE |
| 179 | + date = DATE(REPLACE('${YYYY_MM_DD}', '_', '-')) AND |
| 180 | + theme_names.rank = 1 |
| 181 | +GROUP BY |
| 182 | + client, |
| 183 | + id, |
| 184 | + top_theme_name |
| 185 | +ORDER BY |
| 186 | + origins DESC |
0 commit comments