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

feat: add support for speculative prerender #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ It's key differentiator are:
- 🌍 Experience Platform enabled: the library fully integrates with our main Adobe Experience Platform and all the services of our ecosystem
- 🚀 extremely fast: the library is optimized to reduce load delay, TBT and CLS, and has minimal impact on your Core Web Vitals
- 👤 privacy-first: the library does not track end users by default, and can easily be integrated with your preferred consent management system to open up more advanced use cases
- 🔬 prerender support: the library supports [speculative prerendering](https://developer.mozilla.org/en-US/docs/Web/API/Speculation_Rules_API) out-of-the-box, and won't fire Analytics events (and artificially inflate your page views) until the page is actually viewed

## Prerequisites

Expand Down
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,21 @@ export async function sendAnalyticsEvent(xdmData, dataMapping = {}, configOverri
// eslint-disable-next-line no-console
console.assert(config.analytics, 'Analytics tracking is disabled in the martech config');
try {
// eslint-disable-next-line no-undef
return window[config.alloyInstanceName]('sendEvent', {
const fn = () => window[config.alloyInstanceName]('sendEvent', {
documentUnloading: true,
xdm: xdmData,
data: dataMapping,
edgeConfigOverrides: configOverrides,
});
if (document.prerendering) {
return new Promise((resolve) => {
document.addEventListener('prerenderingchange', async () => {
await fn();
resolve();
}, { once: true });
});
}
return fn();
} catch (err) {
handleRejectedPromise(new Error(err));
return Promise.reject(new Error(err));
Expand Down