Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Add bfcache not restored reasons api #10338

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
58 changes: 46 additions & 12 deletions src/lib/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ function sendToGoogleAnalytics({
};

let overrides;
let debug_input_delay;
let debug_processing_time;
let debug_presentation_delay;

switch (name) {
case 'CLS':
Expand All @@ -114,28 +117,33 @@ function sendToGoogleAnalytics({
};
break;
case 'INP':
overrides = {
debug_event: attribution.eventType,
debug_time: attribution.eventTime,
debug_load_state: attribution.loadState,
debug_target: attribution.eventTarget || '(not set)',
debug_input_delay: Math.round(
if (attribution.eventEntry) {
debug_input_delay = Math.round(
attribution.eventEntry.processingStart -
attribution.eventEntry.startTime,
),
debug_processing_time: Math.round(
);
debug_processing_time = Math.round(
attribution.eventEntry.processingEnd -
attribution.eventEntry.processingStart,
),
debug_presentation_delay: Math.round(
);
debug_presentation_delay = Math.round(
// RenderTime is an estimate, because duration is rounded, and may get rounded down.
// In rare cases it can be less than processingEnd and that breaks performance.measure().
// Lets make sure its at least 4ms in those cases so you can just barely see it.
Math.max(
attribution.eventEntry.processingEnd + 4,
attribution.eventEntry.startTime + attribution.eventEntry.duration,
) - attribution.eventEntry.processingEnd,
),
);
}
overrides = {
debug_event: attribution.eventType,
debug_time: attribution.eventTime,
debug_load_state: attribution.loadState,
debug_target: attribution.eventTarget || '(not set)',
debug_input_delay: debug_input_delay,
debug_processing_time: debug_processing_time,
debug_presentation_delay: debug_presentation_delay,
};
break;
case 'LCP':
Expand Down Expand Up @@ -261,6 +269,27 @@ function getNavigationType() {
return '(not set)';
}

/**
* Gets the type of navigation for this page. In most cases this is the
* value returned by the Navigation Timing API (normalized to use kebab case),
* but in addition to this it also captures pages that were prerendered
* as well as page that were restored after a discard.
* @returns {string|undefined}
*/
function getBackForwardNotRestoreReasons() {
const navEntry =
self.performance &&
performance.getEntriesByType &&
performance.getEntriesByType('navigation')[0];

if (navEntry) {
if (navEntry.notRestoredReasons) {
return navEntry.notRestoredReasons.reasons.toString();
}
}
return;
}

/**
* Returns a list of any `prerender` speculation rules defined by any
* `script[type=speculationrules]` elements on the page.
Expand Down Expand Up @@ -330,7 +359,12 @@ function getMeta(name) {
function setConfig() {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({measurement_version: version});
window.dataLayer.push({navigation_type: getNavigationType()});
const navigationType = getNavigationType();
window.dataLayer.push({navigation_type: navigationType});
if (navigationType === 'back-forward') {
const reasons = getBackForwardNotRestoreReasons();
window.dataLayer.push({back_forward_not_restore_reasons: reasons});
}
window.dataLayer.push({page_path: location.pathname});
window.dataLayer.push({page_authors: getMeta('authors')});
window.dataLayer.push({page_tags: getMeta('tags')});
Expand Down
28 changes: 28 additions & 0 deletions types/utils/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ declare global {
inert: boolean;
recaptchaLoadCallback?: () => void;
loadScript?: (url: string, type?: string) => void;
dataLayer?: Array;
}

interface Navigator {
connection: NetworkInformation;
deviceMemory: number;
}

interface PerformanceEntry {
deliveryType: string;
activationStart: number;
type: string;
}

interface NotRestoredReasons {
url: string;
src: string;
id: string;
name: string;
blocked: boolean;
reasons: [string];
children: [NotRestoredReasons];
}

interface PerformanceNavigationTiming {
deliveryType: string;
activationStart: number;
notRestoredReasons: NotRestoredReasons
}

var WebComponents: WebComponentsType;
Expand Down
Loading