samhub.js
    Preparing search index...

    Class DataLayer

    The DataLayer class provides a mechanism to manage and interact with a data layer array, commonly used for tracking events in analytics or similar systems. It allows for registering event listeners, processing existing events, and intercepting new events pushed to the data layer.

    const dataLayer = [];
    const dl = new DataLayer(dataLayer);

    dl.on('eventName', (param1, param2) => {
    console.log('Event triggered:', param1, param2);
    });

    dataLayer.push(['eventName', 'value1', 'value2']);
    • The dataLayer array is expected to follow a specific structure where each event is an array, with the first element being the event name and subsequent elements being the event parameters.
    • The DataLayer class overrides the push method of the provided array to intercept and process events.

    The type representing the event names.

    The type representing the event listener functions.

    Index

    Constructors

    Methods

    Constructors

    • Creates an instance of the DataLayer class.

      Parameters

      • dataLayer: any[]

        An array representing the initial data layer (e.g. window.samhubData).

      Returns DataLayer

    Methods

    • Removes a specific listener for the given event name.

      Parameters

      • eventName: string

        The name of the event for which the listener should be removed.

      • listener: EventListener

        The event listener function to be removed.

      Returns void

      If the specified listener is not found in the list of listeners for the event, no action is taken.

    • Registers an event listener for a specific event.

      Parameters

      • eventName: string

        The name of the event to listen for.

      • listener: EventListener

        The callback function to be executed when the event is triggered.

      Returns void

    • Processes all existing events in the dataLayer array. For each event, if the first element is an array and contains at least one item, it extracts the event name and parameters, then triggers the corresponding listeners.

      Returns void

      This method assumes that each event in the dataLayer follows a specific structure where the first element is an array containing the event name and its parameters.