import { type EventContextData, type EventProperties, utils } from '@farfetch/blackout-analytics'; import UniqueViewIdStorage from './uniqueViewIdStorage/UniqueViewIdStorage.js'; import type { ProcessedContextWeb } from './context.js'; /** * Helper class used by analytics to manage state that will * be added to the context of each event. */ export default class WebContextStateManager { uniqueViewIdStorage: UniqueViewIdStorage | null; previousUniqueViewId: ProcessedContextWeb[typeof utils.ANALYTICS_PREVIOUS_UNIQUE_VIEW_ID]; currentUniqueViewId: ProcessedContextWeb[typeof utils.ANALYTICS_UNIQUE_VIEW_ID]; lastFromParameter: ProcessedContextWeb[typeof utils.LAST_FROM_PARAMETER_KEY]; lastPageLocation: string | undefined; /** * Initializes last page location with the document.referrer if it is * available. */ constructor(); /** * Updated last page location which will be used to calculate * the pageLocationReferrer context parameter. This is separate * from other state updates since it normally needs to be done after * the event is dispatched to analytics. */ updateLastPageLocation(): void; /** * Gets the current snapshot of the managed state. * * @returns Current snapshot of the managed state. */ getSnapshot(): { __uniqueViewId: string | null | undefined; __previousUniqueViewId: string | null | undefined; __lastFromParameter: string | null | undefined; pageLocationReferrer: string | undefined; }; /** * Updates relevant state based on the passed page event that will be tracked. * * @param _event - Event name. Not used at the moment but added as it might be useful in the future. * @param properties - Properties of the event. * @param _eventContext - Event context. Not used at the moment but added as it might be useful in the future. */ updateStateFromPageEvent(_event: string, properties?: EventProperties, _eventContext?: EventContextData): void; /** * Updates relevant state based on the track event that will be tracked. * * @param _event - Event name. Not used at the moment but added as it might be useful in the future. * @param properties - Properties of the event. * @param _eventContext - Event context. Not used at the moment but added as it might be useful in the future. */ updateStateFromTrackEvent(_event: string, properties?: EventProperties | undefined, _eventContext?: EventContextData | undefined): void; /** * Initializes the uniqueViewId storage based on the referrer, in case the user * opens a link of the website in a new tab. * document.referrer will point to the original URL from the previous tab, * so we try to grab a uniqueViewId based on that to keep the user journey correct in terms of tracking. */ initialize(): void; }