Nebula icon indicating copy to clipboard operation
Nebula copied to clipboard

Detect page prerender in analytics via Speculation Rules API

Open chrisblakley opened this issue 9 months ago • 4 comments

This is using the Speculation Rules API and could be used to track beyond a simple dimension, but for now that is the initial intent. I am not planning on implementing the Speculation Rules API to define what pages need to be prerendered– I plan to continue using the DOM for that, but I'm curious if the long-term timeline necessitates this API. For now the focus is on tracking.

Particularly I'd love to incorporate this into the page timing GA4 data so we can (possibly) subdivide "Subsequent" page types into those that are preloaded and those that are not. Probably just with an appended name like "Subsequent" and "Subsequent (Prerendered)".

https://developer.chrome.com/blog/prerender-pages/

function pagePrerendered(){ //Probably change this to isPagePrerendered
  return (
    document.prerendering || self.performance?.getEntriesByType?.('navigation')[0]?.activationStart > 0
  );
}

gtag('set', { 'dimension1': pagePrerendered() }); //Update this to GA4 syntax

This is available in Chrome 117, but I do not know the status of Firefox and Safari, so more testing will be needed before we can fully implement this. That being said, the above function is checking if the API exists before it is used.

chrisblakley avatar Sep 22 '23 20:09 chrisblakley