Getting started
Nested Contexts
To fully understand a user's journey, data collection strategies need to reflect the complexity of modern digital experiences. Users navigate between pages, interact with distinct UI components, including complex components like media players, often consuming multiple pieces of media simultaneously.
Datazoom organizes collection around a context hierarchy. The Global Context represents the overall application experience and carries the app_session_id. Within that global session, you can create Custom Contexts for non-player experiences and Player Contexts for individual media players; each Player Context generates its own content_session_id. Ad Break and Ad Contexts then nest beneath the relevant Player Context using ad_break_id and ad_session_id. This structure keeps events, metadata, state, timers, and counters scoped to the correct context while preserving their relationship to the parent app session, giving data teams an accurate view of the complete user journey.
By assigning a specific flow or media player to a Datazoom context, the SDK automatically scopes the events, metadata, and state specifically to that context. Here is how the identifiers are used to build a complete picture:
app_session_id (Global Context): When the user opens your app or webpage, Datazoom generates a unique app_session_id upon the datazoom_loaded event. This ID persists throughout the application experience until the session ends (based on your customized inactivity timeouts) or is abandoned.
[context_name] (Custom Context): You can create a non-player context for specific multi-step processes (like a checkout flow) or specific UI components (like a scrolling carousel or interactive dashboard).
content_session_id (Player Context): Every time a media player initiates playback, it generates a unique content_session_id nested within the app session.
ad_break_id (Ad Break Context): If a media player launches an ad break, an ad_break_id is created and nested within that specific player's content_session_id.
ad_session_id (Ad Context): An ad_id is created for each ad and nested within that ad’s ad_break_id.
Metadata Resolution: Global vs. Local
When managing multiple nested contexts, you will often have metadata that applies to the entire app (e.g., user_id, browser_name, device_type) alongside metadata specific to a localized context (e.g., a title in a player context, or a custom_cart_value in a checkout context).
Global Metadata: Defined at the application level. It is automatically inheritable by all events triggered across all localized contexts.
Local Metadata (Context-Specific): Defined directly within a localized context, such as a specific media player or checkout flow., this data is strictly scoped to the events generated within that environment.
Note on Precedence: If the same metadata key exists at both the global and local levels, Datazoom uses the value from the most specific active context. For example, an app might set a global custom_title: "Sports", while a video player context sets custom_title: "Quarterfinal Highlights".
Solving for Multiple Concurrent Players
A primary benefit of these nested sessions is its ability to seamlessly handle multiple media players on a single screen.
Whether it's a news article with multiple auto-playing video embeds, a sports broadcasting app offering Picture-in-Picture (PiP), or a social media feed with scrolling video carousels, tracking these interactions accurately presents a unique challenge. With nested contexts, if you have three concurrent video players on the screen, Datazoom will generate three distinct content_session_ids nested under one app_session_id.
You can easily distinguish which video caused an error, triggered an ad, or completed playback as well as user interactions specific to a particular player are accurately tracked and isolated to that specific player's context.
Implementation Example
To collect data from multiple concurrent players, you simply need to create a Player Context for each media element. Here is an example of how this looks using Datazoom's Javascript Collector for standard HTML5 <video> elements.
Initialize the Global Datazoom SDK First, ensure the Datazoom Beacon is included and loaded on your page. This establishes the Global Context and begins tracking the app_session_id.
Create Contexts for Each Player For every media player rendered in the DOM, fetch the element and pass it into Datazoom's createContext() method.
// Locate your video elements in the DOM
var primaryVideoElement = document.getElementById("main-news-video");
var secondaryVideoElement = document.getElementById("sidebar-ad-video");
// Create distinct Player Contexts for each element
var primaryPlayerContext = datazoom.createContext(primaryVideoElement);
var secondaryPlayerContext = datazoom.createContext(secondaryVideoElement);
// (Optional) Set custom metadata specific to each player's local context
primaryPlayerContext.setMetadata({
custom_player_role: "primary_content",
custom_video_category: "Breaking News"
});
secondaryPlayerContext.setMetadata({
custom_player_role: "sidebar_carousel",
custom_video_category: "Sponsored"
});
By setting this up, actions taken on the primaryVideoElement (like a pause or a buffer event) will be sent strictly through the primaryPlayerContext. It will carry the global app_session_id, its unique content_session_id, and the local custom_player_role: "primary_content".
Best Practices
Minimize Global Clutter: Keep your global metadata restricted to truly overarching application state (e.g., centralized User IDs, cohort IDs, page titles). Leave flow-specific or video-specific data to localized Custom and Player Contexts.
Destroy Contexts on Unmount: If your application is a Single Page Application (SPA) like React or Angular, or if users continuously scroll past videos (like a social feed), be sure to destroy the context when the element is removed from the DOM to prevent memory leaks: primaryPlayerContext.destroy();
Use Fluxdata: Datazoom maintains session timers and counters (e.g., playback_duration_content_ms, stall_duration_content_ms). Because these are securely scoped to their specific context hierarchy, the final fluxdata calculated at the end of a content_session_id will accurately reflect the performance of that specific player engagement, rather than an aggregate of all elements on the screen.
For more specific instructions regarding context creation for your given application platform, media player, ad framework or custom use cases, please see the respective articles in the Datazoom Collectors documentation.