Refreshing Context
Keep event metadata up-to-date when context changes in single-page applications.
Overview
Follow this guide to learn how and when to refresh Elevar’s tracking context so events are enriched with accurate metadata.
Every event captured by Elevar includes contextual metadata that describes the environment in which the event occurred. This context is used to enrich events before they are sent to marketing and analytics destinations.
Context data includes:
- Consent values at the time of the event
- Cookies available during the event
- Click IDs used for marketing attribution
Accurate context ensures that events are attributed correctly and processed as expected by downstream platforms. However, issues arise when context changes without being refreshed.
The Context Refresh Challenge
Headless Websites
In headless websites and single-page applications, the page loads only once. As a result, the Elevar script initializes a single time during the initial visit.
Because Elevar captures context during script initialization, any changes that occur later in the session are not automatically reflected in subsequent events unless the context is manually refreshed.
This can lead to several issues:
- Inaccurate attribution when events retain outdated consent or click ID values
- Misalignment between user behavior and recorded event metadata
- Potential data loss or suppression in marketing and analytics platforms
The Solution: Manually Refreshing Context
To prevent outdated context from affecting event attribution, developers should trigger a manual context refresh when there are significant changes, such as:
- A user accepting or rejecting cookies
- A route change in a single-page application
- Modifications to tracking parameters (e.g., click IDs)
Elevar provides a utility function to refresh context. Calling this function ensures that all subsequent events carry the most up-to-date data.
Example Use Case
- A user lands on a product page with consent values set to "denied."
- Elevar captures the initial context and logs the page view event.
- The user accepts cookies on the same page.
- Without a manual refresh, the "Add to Cart" event will still carry the outdated "denied" consent status.
- By calling Elevar's context refresh utility upon consent change, the new context ("granted") is applied to all subsequent events.
Implementation
Route Changes
Any navigation changes without a reload will ideally notify Elevar via a utility. This utility will update the contextual information that Elevar uses to enrich Data Layer events.
If you want to update the context using the codebase, use the following snippet:
window.ElevarInvalidateContext?.();If you want to update it using GTM, use the following snippet:
if (window.ElevarInvalidateContext) {
window.ElevarInvalidateContext();
}