@shopgate/pwa-tracking
Version:
Tracking library for the Shopgate Connect PWA.
17 lines • 5.19 kB
JavaScript
import _regeneratorRuntime from"@babel/runtime/regenerator";function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg);var value=info.value;}catch(error){reject(error);return;}if(info.done){resolve(value);}else{Promise.resolve(value).then(_next,_throw);}}function _asyncToGenerator(fn){return function(){var self=this,args=arguments;return new Promise(function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value);}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err);}_next(undefined);});};}import get from'lodash/get';import{logGroup,getWebStorageEntry,useBrowserConnector,errorManager,SOURCE_TRACKING,CODE_TRACKING,defaultClientInformation}from'@shopgate/pwa-core';import{registerEvents}from'@shopgate/engage/core/commands';import{appWillStart$}from'@shopgate/engage/core/streams';import{event}from'@shopgate/engage/core/classes';import{TYPE_PHONE,OS_ALL,OS_ANDROID}from'@shopgate/pwa-common/constants/Device';import appConfig,{shopNumber,componentsConfig}from'@shopgate/pwa-common/helpers/config';import core from'@shopgate/tracking-core/core/Core';import{COOKIE_CONSENT_UPDATED}from'@shopgate/tracking-core/helpers/events';import{cookieConsentInitialized$,cookieConsentUpdated$}from'@shopgate/engage/tracking/streams';import UnifiedPlugin from'@shopgate/tracking-core/plugins/trackers/Unified';import{track}from"../helpers/index";/**
* Setup tracking subscriptions.
* @param {Function} subscribe The subscribe function.
*/export default function setup(subscribe){subscribe(appWillStart$,function(){registerEvents(['nativeSgTrackingEventFired']);// Route SgTracking events sent from the app to the tracking system
event.addCallback('nativeSgTrackingEventFired',function(_ref){var name=_ref.name,params=_ref.params;track(name,params);});});/**
* Gets triggered when the app starts.
*/subscribe(cookieConsentInitialized$,/*#__PURE__*/function(){var _ref3=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref2){var getState,action,statisticsCookiesAccepted,comfortCookiesAccepted,clientInformationResponse,clientInformation,GaBase,extensionsIndex,trackingExtensions;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:getState=_ref2.getState,action=_ref2.action;statisticsCookiesAccepted=action.statisticsCookiesAccepted,comfortCookiesAccepted=action.comfortCookiesAccepted;if(useBrowserConnector()){_context.next=8;break;}_context.next=5;return getWebStorageEntry({name:'clientInformation'});case 5:_context.t0=_context.sent;_context.next=9;break;case 8:_context.t0={value:defaultClientInformation};case 9:clientInformationResponse=_context.t0;clientInformation={type:get(clientInformationResponse,'value.device.type',TYPE_PHONE),os:get(clientInformationResponse,'value.device.os.platform',OS_ALL),state:getState(),services:get(clientInformationResponse,'value.device.supportedAnalyticsServices',[]),libVersion:get(clientInformationResponse,'value.libVersion'),appVersion:get(clientInformationResponse,'value.appVersion'),cookieConsent:{statisticsCookiesAccepted:statisticsCookiesAccepted,comfortCookiesAccepted:comfortCookiesAccepted}};// TODO: instantiate the UnifiedPlugin only if a native tracker is configured (FB, AppsFlyer)
// eslint-disable-next-line no-new
new UnifiedPlugin();if(appConfig.tracking.hasWebTrackingEngage&&statisticsCookiesAccepted){// eslint-disable-next-line global-require
GaBase=require('@shopgate/tracking-core/plugins/trackers/GaBase')["default"];GaBase.createUniversal({shopNumber:shopNumber,codebaseVersion:get(clientInformationResponse,'value.codebaseVersion'),config:{merchant:[],shopgate:{id:clientInformation.os===OS_ANDROID?appConfig.webTrackingEngage.android:appConfig.webTrackingEngage.ios,useNetPrices:false}}});}try{// eslint-disable-next-line global-require, import/no-dynamic-require
extensionsIndex=require("".concat(process.env.THEME_PATH,"/extensions/tracking"))["default"];trackingExtensions=componentsConfig.tracking||{};Object.keys(trackingExtensions).forEach(function(key){var pluginInit=extensionsIndex[key];if(pluginInit){/**
* Call the init function of the plugin.
* This init function will create the actual instances
*/pluginInit(clientInformation);}});}catch(error){logGroup('Tracking %c: Could not setup plugins',{error:error},'#ED0422');error.code=CODE_TRACKING;error.source=SOURCE_TRACKING;error.context='trackingPlugins';errorManager.queue(error);}core.registerFinished();case 15:case"end":return _context.stop();}},_callee);}));return function(_x){return _ref3.apply(this,arguments);};}());/**
* Gets triggered when the cookie consent selection changes. Registered trackers will be informed
* about the new decisions.
*/subscribe(cookieConsentUpdated$,function(_ref4){var action=_ref4.action;var statisticsCookiesAccepted=action.statisticsCookiesAccepted,comfortCookiesAccepted=action.comfortCookiesAccepted;track(COOKIE_CONSENT_UPDATED,{statisticsCookiesAccepted:statisticsCookiesAccepted,comfortCookiesAccepted:comfortCookiesAccepted});});}