/** * Google Analytics 4 Integration. Will load the google analytics 4 script and * apply the ecommerce recommended events. * * @example Adding GA4 integration to analytics * ``` * * import analytics, \{ integrations \} from '\@farfetch/blackout-react/analytics'; * * analytics.addIntegration('GA4', integrations.GA4, \{ * measurementId: 'G-XXXXX', * \}); * ``` */ import { type ConsentData, type EventData, integrations, type LoadIntegrationEventData, type PageviewEventData, type SetUserEventData, type StrippedDownAnalytics, type TrackEventData, type TrackTypesValues, utils } from '@farfetch/blackout-analytics'; import type { EventScopeCommandsHandlers, GA4Command, GA4CommandList, GA4IntegrationOptions, ProductMappings, ScopeCommands } from './types/index.js'; /** * Google Analytics 4 Integration. */ declare class GA4 extends integrations.Integration { private googleConsentMode; private enableAutomaticPageViews; private schemaEventsMap; private initializePromise; private initializePromiseResolve?; private optionsValidationResultsMap; private measurementId; private scopeCommands; private productMappings; private nonInteractionEvents; private onPreProcessCommands?; private setCustomUserIdProperty; private customLoadScriptFn?; /** * Creates an instance of Google Analytics 4 integration. Setup Google Analytics 4 * and initializes event commands map that will be used to obtain the command list * associated with an event. * * @param options - User configured options. * @param loadData - Analytics's load event data. * @param strippedDownAnalytics - Analytics stripped down instance. */ constructor(options: GA4IntegrationOptions, loadData: LoadIntegrationEventData, strippedDownAnalytics: StrippedDownAnalytics); static [utils.CONSENT_CATEGORIES_PROPERTY]: utils.DefaultConsentKeys; /** * Initializes member variables from options and tries to initialize Google * Analytics 4. * * @param options - Options passed for the GA4 integration. * @param loadData - Analytics's load event data. */ initialize(options: GA4IntegrationOptions, loadData: LoadIntegrationEventData): void; getDataLayerName(): string; /** * Sets the consent object. This method is called by analytics whenever the consent * changes, so there's no need to validate if it has changed or not. * * @param consent - Object to be written on the dataLayer. * * @returns This allows chaining of class methods. */ setConsent(this: this, consent: ConsentData): this; /** * Method to check if the integration is ready to be loaded. * * @param consent - User consent data. * @param options - Options passed for the GA4 integration. * * @returns If the integration is ready to be loaded. */ static shouldLoad(consent: ConsentData, options: GA4IntegrationOptions): boolean; /** * Send page events to GA4. * * @param data - Event data provided by analytics. */ trackPage(data: PageviewEventData): void; /** * Process GA4 page event to perform as track event in ga4 usable events. * * @param data - Event data provided by analytics. * * @returns Promise that will resolve when the method finishes. */ processPageEvent(data: PageviewEventData): Promise; /** * Send events to GA4 if the input event data passes schema validation. * * @param data - Event data provided by analytics. * * @returns Promise that will resolve when the method finishes. */ track(data: EventData): Promise; /** * Tracks an event. Send event to GA4 if the input event data passes schema * validation. * * @param data - Event data provided by analytics. */ trackEvent(data: TrackEventData): void; /** * Validates the event against a schema. If no schema is defined for the event, * assume the event is valid. * * @param data - Event data provided by analytics. * * @returns - If the event passed schema validation or not. */ isEventDataValid(data: TrackEventData): boolean; /** * Execute user scope commands builder if there is any specified. The command list * returned by the builder will be sent to GA4 instance. * * @param data - Event data provided by analytics. */ onSetUser(data: SetUserEventData | LoadIntegrationEventData): Promise; /** * Send event data to GA4 by compiling the data to a command list that will feed * the gtag function. * * @param data - Event data provided by analytics. */ sendEvent(data: TrackEventData): void; /** * Feeds the gtag instance with the command list passed in. * * @param commandList - List of commands to be executed by ga instance. * @param data - Event data provided by analytics. */ processCommandList(commandList: GA4CommandList | undefined, data: EventData | SetUserEventData | LoadIntegrationEventData): void; /** * Attaches the unique event ID generated by the analytics package. * * @param command - The command being processed. * @param data - Event data provided by analytics. * * @returns The modified command. */ postProcessEvent(command: GA4Command, data: EventData | SetUserEventData | LoadIntegrationEventData): GA4Command; /** * Process the command list to check if there is an event that should receive the * command `nonInteraction`. If so, append the command to the list. * * @param commandList - List of commands to be executed by gtag instance. * @param eventName - The event name passed. */ processInteractionEvents(commandList: GA4CommandList, eventName: string): void; /** * Return a GA4 command list for the event. * * @param data - Event data provided by analytics. * @param scopeCommands - Commands by scope configuration. * @param productMappings - User-configured product property mappings. * * @returns The GA4 command list for the event. It will return empty if there is an error or no command * builders exist for the event. */ buildCommandListForEvent(data: TrackEventData, scopeCommands: ScopeCommands, productMappings: ProductMappings): GA4CommandList | undefined; /** * Returns extra commands to be sent to GA4 along with the main commands for a * pageview if there is an `extras` option configured for pageviews. * * @param data - Event data provided by analytics. * @param scopeCommands - Commands by scope configuration. * * @returns An array with the commands or undefined if there is no extra commands builder function. */ getExtraCommandsForPage(data: PageviewEventData, scopeCommands: ScopeCommands): GA4CommandList | undefined; /** * Returns extra commands to be sent to GA4 along with the main commands for an * event if there is an `extras` option configured for the specified event. * * @param data - Event data provided by analytics. * @param scopeCommands - Commands by scope configuration. * * @returns An array with the commands or undefined if there is no extra commands builder function. */ getExtraCommandsForEvent(data: TrackEventData, scopeCommands: ScopeCommands): GA4CommandList | undefined; /** * Returns the main commands that correspond to the event analytics has passed in. * * @param data - Event data provided by analytics. * @param scopeCommands - Commands by scope configuration. * @param productMappings - User-configured product property mappings. * * @returns An array with the commands or undefined if there is no extra commands builder function. */ getMainCommandsForEvent(data: TrackEventData, scopeCommands: ScopeCommands, productMappings: ProductMappings): GA4CommandList | undefined; /** * Returns the main command builder for an event. * * @param event - Event name. * @param scopeCommands - Commands by scope configuration. * * @returns Main command builder for the event if there is one, undefined otherwise. */ getMainCommandBuilderForEvent(event: string, scopeCommands: ScopeCommands): EventScopeCommandsHandlers['main']; /** * Returns the extra commands builder for an event. * * @param event - Event name. * @param scopeCommands - Commands by scope configuration. * * @returns Extra commands builder for the event if there is one, undefined otherwise. */ getExtrasCommandBuilderForEvent(event: string, scopeCommands: ScopeCommands): EventScopeCommandsHandlers['extras']; /** * Executes a command builder. * * @throws * If the command builder passed is not a function or the output is not of the * proper type. * * @param commandBuilder - The command builder to execute. * @param args - Arguments to be passed to the command builder. * * @returns Output of the command builder execution. */ executeCommandBuilder(commandBuilder: T, ...args: T extends (...args: infer R) => GA4CommandList ? R : never): GA4CommandList | undefined; /** * Validates if the provided command list is in the expected format and. * * @param commandList - The command list to validate. * * @returns An object with a boolean `isValid` with the result of the validation and a string * `errorMessage` with the validation error if available. */ validateCommandList(commandList: GA4CommandList): { isValid: boolean; errorMessage?: string; }; /** * Method that will resolve which way gtag script should be loaded, default or * custom. * * @param options - User configured options. */ loadGtagScript(options: GA4IntegrationOptions): void; /** * Method that will load the script and append it to the DOM. * * @param options - User configured options. */ internalLoadScript(options: GA4IntegrationOptions): void; /** * Method that will be called when the GA4 script loads. */ scriptOnload: () => void; } export default GA4;