{"version":3,"file":"EinsteinTracking.js","names":["EinsteinTracking","a","setters","Component","default","deepMerge","Event","appendUrl","ajax","execute","constructor","element","options","arguments","length","activityURL","bindEvents","on","onEvent","eventName","data","eventType","ecommerce","add","triggerActivity","activityName","products","map","product","ID","id","quantity","price","forEach","recommenderName","detail","click","filter","pid","eventData","encodeURIComponent","JSON","stringify","url","navigator","sendBeacon","method","destroy","removeListener"],"sources":["components/global/EinsteinTracking.js"],"sourcesContent":["import Component from 'core/Component';\nimport { deepMerge } from 'toolbox/deepMerge';\nimport { Event } from 'services/EventEmitter';\nimport { appendUrl } from 'toolbox/queryString';\nimport { ajax } from 'toolbox/ajax';\n\n/**\n * This is a description of the EinsteinTracking constructor function.\n * @class\n * @classdesc This component contains all the activities tracking related to Einstein API.\n * This component is only injected in the storefront when the Einstein API Recommendations Provider is enabled.\n * @extends Component\n */\nexport default class EinsteinTracking extends Component {\n    /**\n     * Constructor of the class that mainly merge the options of the components\n     * @param {HTMLElement} element HTMLElement of the component\n     * @param {Object} options options that belongs to the component\n     */\n    constructor(element, options = {}) {\n        super(element, deepMerge({\n            // This is URL to call to register an activity\n            activityURL: undefined,\n        }, options));\n    }\n\n    /**\n     * Should contain only event listeners and nothing else\n     * All the event handlers should be into a separated function. No usage of anonyous function\n     */\n    bindEvents() {\n        Event.on('analytics.event', this.onEvent, this);\n    }\n\n    /**\n     * Handler called when sending basic events\n     * @param { String } eventName - of the event\n     * @param { Object } data - Data that needs to be passed in the event\n     */\n    onEvent(eventName, data = {}) {\n        if (typeof eventName === 'object') {\n            // if first argument is an object then we don't pass the eventName and use the default\n            // The first param become the data object\n            data = eventName;\n        }\n\n        switch (data.eventType) {\n        case 'addProduct':\n            if (!data.ecommerce || !data.ecommerce.add) {\n                break;\n            }\n\n            this.triggerActivity({\n                activityName: 'addToCart',\n                data: {\n                    products: data.ecommerce.add.products.map(product => ({\n                        ID: product.id,\n                        quantity: product.quantity,\n                        price: product.price,\n                    })),\n                },\n            });\n            break;\n        case 'productImpression':\n            data.product.forEach((product) => {\n                const { recommenderName } = product;\n\n                if (recommenderName) {\n                    this.triggerActivity({\n                        activityName: 'viewRecommendation',\n                        data: {\n                            recommenderName,\n                            products: [{ ID: product.id }],\n                        },\n                    });\n                }\n            });\n            break;\n        case 'productDetail':\n            data.ecommerce.detail.products.forEach((product) => {\n                this.triggerActivity({\n                    activityName: 'viewProduct',\n                    data: {\n                        product: {\n                            ID: product.id,\n                        },\n                    },\n                });\n            });\n            break;\n        case 'productClick':\n            // Only register clicks on recommendations tiles, meaning that the recommenderName is provided on the product object\n            // Skip the others\n            data.ecommerce.click.products\n                .filter(product => typeof product.recommenderName !== 'undefined' && product.recommenderName !== '')\n                .forEach((product) => {\n                    this.triggerActivity({\n                        activityName: 'clickRecommendation',\n                        data: {\n                            recommenderName: product.recommenderName,\n                            product: {\n                                ID: product.pid,\n                            },\n                        },\n                    });\n                });\n            break;\n        default:\n            // Other event types are not supported by the Einstein API\n            break;\n        }\n    }\n\n    /**\n     * Trigger the activity event if the activity URL is provided as option\n     *\n     * @param {Object} eventData The data to pass to the event\n     */\n    triggerActivity(eventData) {\n        const activityURL = this.options.activityURL || undefined;\n        if (!activityURL) {\n            return;\n        }\n\n        eventData.data = encodeURIComponent(JSON.stringify(eventData.data));\n        const url = appendUrl(activityURL, eventData);\n\n        if (navigator.sendBeacon) {\n            navigator.sendBeacon(url);\n        } else {\n            ajax(url, { method: 'POST' });\n        }\n    }\n\n    /**\n     * Destroy is called automatically after the component is being removed from the DOM\n     * You must always destroy the listeners attached to an element to avoid any memory leaks\n     */\n    destroy() {\n        Event.removeListener('analytics.event', this.onEvent, this);\n    }\n}\n"],"mappings":"wKAaqBA,CAAgB,QAAAC,CAAA,oBAAAC,OAAA,WAAAD,CAAA,EAb9BE,CAAS,CAAAF,CAAA,CAAAG,OAAA,WAAAH,CAAA,EACPI,CAAS,CAAAJ,CAAA,CAATI,SAAS,WAAAJ,CAAA,EACTK,CAAK,CAAAL,CAAA,CAALK,KA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