unified-datalayer
Version:
A multi-framework utility package for managing XP Data Layer events
112 lines (111 loc) • 3.75 kB
TypeScript
import { AccountMod } from "./modules/Account";
import { CartMod } from "./modules/Cart";
import { CheckoutMod } from "./modules/Checkout";
import { OrderModule } from "./modules/Order";
import { PageMod } from "./modules/Page";
import { ProductDisplayMod } from "./modules/ProductDisplay";
import { ProductListingMod } from "./modules/ProductListing";
import { WishlistMod } from "./modules/Wishlist";
import type { DataLayerConfig, EventData } from "./types";
/**
* DataLayer - A unified interface for managing data layer events
*
* This class provides access to various modules for common tracking scenarios
* and handles the core data layer functionality including initialisation, smart nullification and event pushing.
*/
declare class DataLayer {
private static instance;
private config;
private user;
private isFirstEventAfterRefresh;
private dataLayer;
private previousEvent;
private propertiesToNullify;
page: PageMod;
pdp: ProductDisplayMod;
plp: ProductListingMod;
cart: CartMod;
checkout: CheckoutMod;
account: AccountMod;
wishlist: WishlistMod;
order: OrderModule;
/**
* Constructor initialises the data layer and all modules
*/
private constructor();
/**
* Get the singleton instance of DataLayer
*/
static getInstance(): DataLayer;
/**
* Initialise the DataLayer with configuration options
* @param {DataLayerConfig} options Configuration options including required site information
* @throws Error if siteInfo is not provided
* @example
* const dl = getDataLayer();
*
* dl.init({
* siteInfo: {
* name: "my-site",
* experience: "desktop",
* currency: "AUD",
* division: "myCompany",
* domain: "www.my-site.com.au",
* env: "prod",
* version: "4.2.0"
* }
* });
*/
init(options: DataLayerConfig): void;
/**
* Pushes an event to the Adobe Data Layer with properly formatted data
* @param eventName The name of the event
* @param eventData Object containing the event data
* @throws Error if DataLayer has not been initialised with siteInfo
*/
pushEvent(eventName: string, eventData?: EventData): Promise<void>;
clearProducts(): void;
/**
* Validates that the DataLayer is properly initialised
* @private
*/
private validateInitialisation;
/**
* Prepares the event object with all necessary data
* @private
*/
private prepareEventObject;
/**
* Applies nullification logic to track state changes between events
* @private
*/
private applyNullificationLogic;
/**
* Handles nullification for nested properties in the 'default' object
* @private
*/
private handleDefaultObjectNullification;
/**
* Stores a clean version of the event as previousEvent by removing nullified properties
* @private
*/
private storeCleanPreviousEvent;
/**
* Reset the first event flag - useful for testing or when you need to force
* the site information to be sent again
*/
resetFirstEventFlag(): void;
/**
* Configure properties that should be automatically nullified
* @param properties - Map of object keys to arrays of properties to nullify
*/
setPropertiesToNullify(properties: Record<string, string[]>): void;
/**
* Add properties to nullify for a specific object key
* @param key - The object key (e.g., 'default')
* @param properties - Array of property names to nullify
*/
addPropertiesToNullify(key: string, properties: string[]): void;
}
export default DataLayer;
export declare function getDataLayer(): DataLayer;