Last verified: v1.14

Track events


EventObserver

The EventObserver component provides automatic click tracking for conversion analytics. It uses event delegation to monitor clicks on elements with the data-ddi-event attribute and sends events to the analytics system.

Features:

Basic Usage

---
import { EventObserver } from '@dynamic-type/ddi';
---

<!-- Add the observer component once per page -->
<EventObserver />

<!-- Add tracking to any clickable element -->
<button data-ddi-event="newsletter-signup" data-ddi-attribute="demo-page">
    Subscribe to Newsletter
</button>

<a href="/contact" data-ddi-event="contact-page-visit">
    Contact Us
</a>

Tracking Different Events

You can track various types of user interactions by setting different event names:

<!-- Button clicks -->
<button data-ddi-event="cta-primary">
    Get Started
</button>

<!-- Link clicks -->
<a href="/pricing" data-ddi-event="pricing-page-view">
    View Pricing
</a>

<!-- Form submissions -->
<button type="submit" data-ddi-event="form-submit">
    Submit Form
</button>

<!-- Download links -->
<a href="/whitepaper.pdf" data-ddi-event="whitepaper-download">
    Download Whitepaper
</a>

Attributes

Tracked Data

Each event automatically includes:

How It Works

The EventObserver:

  1. Sets up a delegated click listener on the document
  2. Checks if clicked elements have the data-ddi-event attribute
  3. Extracts the event name and optional attribute
  4. Calculates the current scroll position
  5. For external page links: prevents default navigation, tracks the event (with 300ms timeout), then navigates programmatically
  6. For all other cases (internal links, download links, buttons, special protocols): tracks immediately and allows normal behavior
  7. Sends the event data to the analytics system via trackEvent()

External page links (different hostname) are handled specially to ensure tracking completes:

Click tracking examples

Trackable link

Trackable external link

Click any of the above elements to trigger conversion tracking. Check your browser’s developer tools to see the tracking calls.