Shadow DOM Insights: Area and Event Tracking

This document provides step-by-step instructions for configuring Insights tracking for areas and elements inside Shadow DOM roots. Shadow DOM encapsulation allows web developers to create custom web components with isolated styles and functionality, but requires special configuration to enable proper tracking of user interactions within these enclosed DOM structures.

1. Overview

Purpose

This guide enables you to:

• Configure Areas for elements inside a Shadow root
• Enable time activity tracking for detailed user interaction analysis
• Monitor and collect data on user interactions with target elements
• Extract meaningful labels from Shadow DOM elements
• Validate tracking configuration using debug mode
• Troubleshoot tracking issues for web components

Applicability

This guide applies to tracking user interactions on web pages that use custom web components implemented with Shadow DOM. If your application uses standard HTML without Shadow DOM, refer to the standard Insights tracking guide instead.


2. Prerequisites

Before starting this configuration, ensure you have:

• Familiarity with the webpage's HTML and DOM structure
• Access to the Insights Configuration Tool
• Ability to inspect web elements using browser DevTools
• Understanding of CSS selectors
• Access to modify application tracking settings
• Basic knowledge of web components and Shadow DOM concepts


Understanding Shadow DOM

What is Shadow DOM?

Shadow DOM is a browser feature that allows web developers to encapsulate DOM subtrees within custom web components. This encapsulation ensures that styles and functionality are isolated from the main document, enabling cleaner component-based architecture. However, this isolation also affects how tracking tools access and monitor elements within the Shadow DOM.

Why Shadow DOM Requires Special Tracking Configuration

Standard CSS selectors and tracking methods may not work with Shadow DOM because:

• Shadow DOM elements are isolated from the main DOM tree
• Standard CSS selectors cannot pierce through shadow boundaries
• Event listeners on the main document may not capture events within Shadow DOM
• Custom web components require explicit selector paths to access enclosed elements

3. HTML Structure

My Application UI Structure

Understanding your application's HTML structure is essential for proper tracking configuration. Below is an example of a typical application using Shadow DOM web components.

HTML Tree

Below is the structure of the UI:

<body>
├── <div class="main-component">
│   ├── <h1>My Application</h1>
├── <div class="content">
│   └── <app-container>
│       └── Shadow Root (of <app-container>)
│           ├── <div id="app-container">
│               ├── <app-card id="app1" title="App 1">
│               │   └── Shadow Root (of <app-card>)
│               │       └── <div id="app1-child">
│               │           └── <button>Click Me</button>
│               ├── <app-card id="app2" title="App 2">
│                   └── Shadow Root (of <app-card>)
│                       └── <div id="app2-child">
│                           └── <button>Click Me</button>

Key Components Explained

ComponentPurpose
Main ComponentContains the app title and navigation elements
App-ContainerRoot web component that holds other components within its Shadow DOM
App-CardIndividual card component representing an application with its own Shadow DOM
Shadow RootEncapsulated DOM boundary within each web component

Scenario 1: Configuring Areas and Interaction Tracking in Shadow DOM

This scenario explains how to define areas and trackable elements within a Shadow root using CSS selectors. You will enable interaction tracking and time activity tracking to monitor user engagement with elements inside custom Shadow DOM structures.

Use Case

You want to track when users interact with specific app cards within your application and measure how long they spend viewing each card.


Configuration Steps

Step 1: Validate Shadow DOM Integration

  1. Launch your application in a web browser
  2. Right-click on the page and select "Inspect" to open DevTools
  3. Find the web component elements (e.g., , ) in the HTML tree
  4. Click the disclosure triangle next to the web component to expand the Shadow Root
  5. Locate the target elements you want to track (e.g., #app1-child, #app2-child)
  6. Ensure shadow DOM mode is set to "open" for seamless tracking

Tip: Use DevTools to interact with the shadow DOM elements and confirm their IDs or selectors.

DevTools Shadow DOM Inspection

DevTools Shadow DOM Inspection. Expand Shadow Root in DevTools to view elements and selectors.


Step 2: Define Areas in the Insights Tool

  1. Log in to the Insights Configuration Tool
  2. Click "Create New Area" to add the first trackable area
  3. Enter Name: "My Application - App 1" and CSS Selector: "#app1-child"
  4. Click "Create New Area" again for the second area
  5. Enter Name: "My Application - App 2" and CSS Selector: "#app2-child"
  6. Click "Save" to persist your area definitions

Example Configuration Screenshot:

Enter area name and CSS selector for Shadow DOM elements (#app1-child, #app2-child).

Step 3: Enable Time Activity Tracking

  1. Click on "My Application - App 1" area to edit it
  2. Locate the "Time Activity Tracking" toggle in the configuration panel
  3. Toggle Time Activity Tracking ON
  4. Specify the minimum interaction duration (e.g., 5 seconds means track only if user views for 5+ seconds)
  5. Follow the same steps for "My Application - App 2"
  6. Save your configuration

Enable Time Activity toggle and set minimum interaction duration for area tracking.

Step 4: Preview and Validate results

  1. In the configuration tool, enable "Debug Mode" to visualize tracking in real-time
  2. Visit your application in the browser
  3. Look for colored borders or highlights on configured areas - these indicate active tracking
  4. Click on app cards to trigger tracking events
  5. Open DevTools console to view tracking events as they fire
  6. Watch the debug console for time activity events when you hover over or interact with areas

Example

Below is an example of the debug mode visualization:

Debug Mode Visualization. Borders indicate active tracking areas; debug console shows tracking events.


Scenario 2: Tracking and Extracting Labels from Shadow DOM Elements

This scenario details how to define areas and trackable elements using selectors that target custom Shadow root tags while extracting meaningful labels from specific elements. This ensures precise interaction tracking with human-readable labels in your reports.

Use Case

You want to track clicks on app cards and automatically extract the app name and button text to label the tracked interactions with meaningful information for reporting.


Steps to Configure

Step 1: Define Areas and Trackable Elements

  1. Log in to the Insights Configuration Tool
  2. Create an area named "My Application - App Container" with selector "#app-container"
  3. Add trackable element: Name "App Card 1", Selector "app-card[id="app1"]", enable Click Tracking
  4. Add trackable element: Name "App Card 2", Selector "#app2", enable Click Tracking
  5. Ensure click events are captured for both trackable elements
  6. Click "Save" to persist your trackable elements

Trackable Elements. Define Shadow DOM elements with CSS selectors and enable click tracking.

Step 2: Configure Text Extraction

  1. In configuration tool, click on "Text Extraction" tab
  2. Name: "App 1 Title", Selector: "#app1-child", Method: "textContent"
  3. Use regex filter like "^App" to extract only text starting with "App"
  4. Name: "Button Text", Selector: "button", Method: "textContent", Regex: "Click"
  5. Click "Preview" to test extraction and see sample values
  6. Save your text extraction rules

Text Extraction. Define extraction method (textContent, HTML attribute) and optional regex filters.

Step 3: Enable Interaction Tracking

  1. Go back to the main tracking configuration area

  2. Toggle "Click Event Tracking" ON for the configured trackable elements

  3. Configure event throttling to avoid excessive data collection (e.g., 1 second minimum between events)

  4. Toggle option to include extracted labels in tracking events

  5. Save your tracking settings

Interaction Tracking. Enable click event tracking and custom data extraction for Shadow DOM elements.

Step 4: Validate with Debug Mode

  1. Turn on Debug Mode in configuration tool

  2. Navigate to your application

  3. Click on app cards to trigger tracked events

  4. Open DevTools console to see captured events with extracted labels

  5. Confirm that labels are correctly extracted from Shadow DOM elements

  6. Examine the event data to ensure all custom fields are populated

Debug Console Output. View captured events with extracted labels and custom data fields.


Interpreting Tracked Metrics

Once your Shadow DOM tracking is configured and active, the Insights dashboard will display metrics based on your setup:

MetricInterpretation
Interaction CountTotal number of tracked interactions (clicks, hovers, etc.) with configured areas
Time ActivityTotal time users spent viewing or interacting with each area (in seconds/minutes)
Label DistributionBreakdown of interactions by extracted labels (e.g., how many clicks per app name)
Event SequencesOrder of events showing user navigation paths through Shadow DOM elements
Engagement RatePercentage of page visits that included interaction with tracked areas

View interaction counts, time activity, and label distribution for tracked Shadow DOM elements.

Advanced Configuration Topics

Dynamic Shadow DOM Elements

If your web components dynamically generate Shadow DOM elements (e.g., loading more cards on scroll), ensure your CSS selectors remain consistent and predictable. Use generic selectors that match all dynamically generated elements.

Example: Instead of #app1-child, use app-card > div or app-card [data-trackable="true"]

Dynamic Elements Tracking. Generic selectors to track dynamically generated Shadow DOM elements.

Multiple Levels of Shadow DOM

If you have nested Shadow DOM (Shadow DOM within Shadow DOM), configure separate tracking for each level. Start from the outermost Shadow root and work inward.

Event Bubbling and Shadow DOM

By default, events do not bubble out of Shadow DOM. Ensure your tracking configuration explicitly listens for events within each Shadow root rather than relying on event propagation from the main document.

Nested Shadow DOM Configuration. Configure separate tracking for each Shadow root level in nested structures.

Validating Your Configuration

After setting up Shadow DOM tracking, use these validation steps to ensure everything is working correctly:

  1. Turn on Debug Mode in the configuration tool

  2. Click, hover over, and interact with all configured areas

  3. Verify that events appear in the DevTools console

  4. Confirm that CSS selectors correctly match intended elements

  5. Verify extracted labels match expected values

  6. Check that metrics appear in the Insights dashboard within 5-10 minutes

  7. Examine raw event data to ensure all custom fields are populated

Metrics should appear within 5-10 minutes after configuration; verify counts are increasing.

4. FAQs

1. What is a Shadow Root?

A Shadow Root is the encapsulated DOM subtree of a web component that ensures the isolation of styles and functionality from the main document.


2. How can elements inside a shadow root be inspected?

  1. Open DevTools (right-click > Inspect).
  2. Locate the shadow root's parent element (e.g., <app-container>).
  3. Expand the shadow root to view child elements.

3. Can dynamically added shadowRoot elements be tracked?

Yes, if their selectors remain consistent and predictable. Update configurations as necessary.


4. What happens if an incorrect CSS selector is used?

Tracking fails. To fix:

  • Verify the selector using DevTools.
  • Enable Debug Mode for real-time validation.

5. Conclusion

By following these instructions, you can effectively configure Insights tracking for shadowRoot elements, extract meaningful data, and monitor user interactions accurately.

Related Features

The following features work in conjunction with Shadow DOM tracking:
• Insights Dashboard - View metrics generated from Shadow DOM tracking
• Application Settings - Configure application-wide tracking options
• UserActivityTracking - Monitor detailed user interactions within tracked Shadow DOM elements
• Analytics Reports - Generate reports based on Shadow DOM tracking data
• Event Tracking - Combine Shadow DOM tracking with custom event tracking
• Data Filtering - Filter and segment Shadow DOM tracking data in reports


Did this page help you?