Skip to content

Latest commit

 

History

History
47 lines (32 loc) · 1.29 KB

File metadata and controls

47 lines (32 loc) · 1.29 KB

@18f/identity-analytics

Utilities and custom elements for logging events and errors in the application.

Usage

Programmatic Event Tracking

Track an event or error from your code using exported function members.

Since JavaScript may be bundled and minified in production environments, including an errorId is required to uniquely identify the source of the error.

import { trackEvent, trackError } from '@18f/identity-analytics';

button.addEventListener('click', () => {
  trackEvent('button_clicked', { success: true });
});

try {
  doSomethingRisky();
} catch (error) {
  trackError(error, { errorId: 'exampleId' });
}

HTML Element Click Tracking

Use the <lg-click-observer> custom element to record clicks within the element.

import '@18f/identity-analytics/click-observer-element';

The custom element will implement the analytics logging behavior, but all markup must already exist.

<lg-click-observer event-name="button_clicked">
  <button type="button">Click me!</button>
</lg-click-observer>

The element supports the following attributes to customize its behavior:

  • event-name: The name of the analytics event that should be logged when clicked
  • payload: (Optional) JSON payload of additional data that should be included in the logged event