Skip to content

Instantly share code, notes, and snippets.

View gilbertococchi's full-sized avatar

Gilberto Cocchi gilbertococchi

View GitHub Profile
@gilbertococchi
gilbertococchi / getNavigationType.js
Last active September 24, 2025 09:22
Code Snippet to get the current page Navigation Types extending some support to handle BFCache and Prerender types
/*
* Utility helper to detect a navigationType across all the different standards methods.
* Possible values:
* - "navigate": Navigation started by clicking a link, document was not cached
* - "navigational-prefetch": The resource was retrieved from a prefetched response stored in an in-memory cache via the Speculation Rules API
* - "prerender": Navigation to a Prerendered document via Speculation Rules API: https://developer.mozilla.org/en-US/docs/Web/API/Document/prerendering
* - "cache": Navitation to a resource that was retrieved from the cache
* - "reload": Navigation is through the browser's reload operation
* - "restore": Page was previously discarded by the browser while in a hidden tab
* - "back_forward": Navigation is through the browser's history traversal operation, back/forward Cache handled differently
const RATING_COLORS = {
"good": "#0CCE6A",
"needs-improvement": "#FFA400",
"poor": "#FF4E42",
"invalid": "#FFC0CB",
"default": "inherit",
};
const LCP_THRESHOLDS = {
good: 2500,
SELECT
FORMAT_DATE('%m-%Y', DATE) as month_year,
DEVICETYPE,
USERAGENTFAMILY,
#BEACONTYPE, # 'page view', 'bfcache'
#NAVIGATIONTYPE, # navigate, reload, back forward, null,
ROUND(SAFE_DIVIDE(
COUNTIF(BEACONTYPE="bfcache"),
@gilbertococchi
gilbertococchi / gist:a7237db651259a00e5b1f971e835feca
Last active May 21, 2024 14:48
BigQuery GA4 WebVitals.js v4 INP and LoAF data extraction
WITH web_vitals_events AS (
SELECT
event_name as metric_name, * EXCEPT(event_name, is_last_received_value) FROM (
SELECT *, IF (ROW_NUMBER() OVER (
PARTITION BY (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'metric_id')
ORDER BY (SELECT COALESCE(value.double_value, value.int_value) FROM UNNEST(event_params) WHERE key = 'metric_value') DESC
) = 1, true, false) AS is_last_received_value
FROM `ilaria-food-and-home.analytics_317802288.events_20240519`
WHERE event_name in ('INP')
) WHERE is_last_received_value
@gilbertococchi
gilbertococchi / INPtoLoAF.js
Last active August 22, 2023 16:58
INP to LoAF Attribution
(function() {
if (PerformanceObserver.supportedEntryTypes.includes('long-animation-frame')) {
window.longestINPtoLoAFScript = null;
function process(eventEntry) {
let matchingLoafs = [];
for (const event_entry of eventEntry.entries) {
if (event_entry.entryType == "first-input")
continue;
@gilbertococchi
gilbertococchi / gist:fdb7d29ca8ad813fb28ba3902296838f
Created January 25, 2023 12:47
Wix Origins passing INP using 3P Categories
CREATE TEMP FUNCTION IS_GOOD(good FLOAT64, needs_improvement FLOAT64, poor FLOAT64) RETURNS BOOL AS (
good / (good + needs_improvement + poor) >= 0.75
);
WITH WIX_ORIGINS AS (
SELECT
origin,
fast_inp,
good_inp
FROM (
# Warning: This Query will process 57GB approx, consider BigQuery API costs.
WITH dates AS
(SELECT
yyyymm
FROM
`chrome-ux-report.materialized.device_summary`
GROUP BY yyyymm
ORDER BY yyyymm DESC
LIMIT 13)