Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating CLS approach #611

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions packages/clarity-js/src/performance/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import * as navigation from "@src/performance/navigation";

let observer: PerformanceObserver;
const types: string[] = [Constant.Navigation, Constant.Resource, Constant.LongTask, Constant.FID, Constant.CLS, Constant.LCP];
let sessionEntries: PerformanceEntry[] = [];
let sessionValue = 0;

export function start(): void {
// Capture connection properties, if available
Expand All @@ -26,6 +28,8 @@ export function start(): void {
bind(window, "load", setTimeout.bind(this, observe, 0));
} else { observe(); }
} else { internal.log(Code.PerformanceObserver, Severity.Info); }
sessionEntries = [];
sessionValue = 0;
}

function observe(): void {
Expand Down Expand Up @@ -73,8 +77,9 @@ function process(entries: PerformanceEntryList): void {
if (visible) { metric.max(Metric.FirstInputDelay, entry["processingStart"] - entry.startTime); }
break;
case Constant.CLS:
// Scale the value to avoid sending back floating point number
if (visible && !entry["hadRecentInput"]) { metric.sum(Metric.CumulativeLayoutShift, entry["value"] * 1000); }
if (visible) {
calculateCls(entry);
}
break;
case Constant.LCP:
if (visible) { metric.max(Metric.LargestPaint, entry.startTime); }
Expand All @@ -83,9 +88,37 @@ function process(entries: PerformanceEntryList): void {
}
}

function calculateCls(entry: PerformanceEntry): void {
if(entry["hadRecentInput"]) { return; }

const firstSessionEntry = sessionEntries[0];
const lastSessionEntry = sessionEntries[sessionEntries.length - 1];

// If the entry occurred less than 1 second after the previous entry
// and less than 5 seconds after the first entry in the session,
// include the entry in the current session. Otherwise, start a new
// session.
// For more info: https://web.dev/articles/cls
if (
sessionValue &&
lastSessionEntry && (entry.startTime - lastSessionEntry.startTime < 1000) &&
firstSessionEntry && (entry.startTime - firstSessionEntry.startTime < 5000)
) {
sessionValue += entry["value"];
sessionEntries.push(entry);
} else {
sessionValue = entry["value"];
sessionEntries = [entry];
}
// Scale the value to avoid sending back floating point number
metric.max(Metric.CumulativeLayoutShift, entry["value"] * 1000)
AbdelrhmanMagdy marked this conversation as resolved.
Show resolved Hide resolved
}

export function stop(): void {
if (observer) { observer.disconnect(); }
observer = null;
sessionEntries = [];
sessionValue = 0;
}

function host(url: string): string {
Expand Down