/* * Copyright (c) Linets 2023. * @author Jhonny Negrin */ define([ 'jquery', 'underscore', 'Linets_RetailRocket/js/action/track-transaction', 'Linets_RetailRocket/js/action/track-email', 'jquery/ui' ], function ($, _, orderTracker, emailTracker) { $.widget('mage.rrUrlObserver', { options: { serviceUrl: '', successPageUrls: [], formUrls: [], formFields: [] }, /** * Widget initialization * * @private */ _create: function () { if (this.matchUrl(this.options.successPageUrls)) { let storage = $.initNamespaceStorage('rr-storage').localStorage; this.triggerTransactionTracker(this.options.serviceUrl, { 'orderId': storage.get('order_id'), 'quoteId': storage.get('quote_id') }); } else { this.clearTransactionStorage(); } if (this.matchUrl(this.options.formUrls)) { this.triggerEmailTracker(this.options.formFields); } }, /** * Match current Url and return * * @return {Boolean} */ matchUrl: (urls) => { let matched = false, currentUrl = window.location.pathname; if (!_.isArray(urls)) { return false; } urls.forEach((url) => { let cleanUrl = url.trim(); if (!matched && (currentUrl.indexOf(cleanUrl) >= 0 || cleanUrl === '*')) { matched = true; } }); return matched; }, /** * Pushes data to Retail Rocket (Transaction Tracker) * * @return {void} */ triggerTransactionTracker: (serviceUrl, order) => { if (_.isUndefined(order) || _.isEmpty(order) || !order.hasOwnProperty('orderId') || !order.hasOwnProperty('quoteId')) { console.log('Order not tracked (invalid)', window.location.pathname, order); return; } orderTracker(serviceUrl + '/' + order.orderId + '/' + order.quoteId); }, /** * Pushes email data to Retail Rocket * * @param fields {Array} * @return {void} */ triggerEmailTracker: (fields) => { fields.forEach((field)=>{ let cleanField = field.trim(); $(cleanField).on('change', (event)=>{ emailTracker($(event.target).val()); }); }) }, clearTransactionStorage: () => { let storage = $.initNamespaceStorage('rr-storage').localStorage; storage.removeAll(); } }); return $.mage.rrUrlObserver; });