@shopgate/engage
Version:
Shopgate's ENGAGE library.
12 lines • 8.91 kB
JavaScript
import _regeneratorRuntime from"@babel/runtime/regenerator";var _excluded=["permission"],_excluded2=["permission"],_excluded3=["permission"];function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}function _objectWithoutProperties(source,excluded){if(source==null)return{};var target=_objectWithoutPropertiesLoose(source,excluded);var key,i;if(Object.getOwnPropertySymbols){var sourceSymbolKeys=Object.getOwnPropertySymbols(source);for(i=0;i<sourceSymbolKeys.length;i++){key=sourceSymbolKeys[i];if(excluded.indexOf(key)>=0)continue;if(!Object.prototype.propertyIsEnumerable.call(source,key))continue;target[key]=source[key];}}return target;}function _objectWithoutPropertiesLoose(source,excluded){if(source==null)return{};var target={};var sourceKeys=Object.keys(source);var key,i;for(i=0;i<sourceKeys.length;i++){key=sourceKeys[i];if(excluded.indexOf(key)>=0)continue;target[key]=source[key];}return target;}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{softOptInShown$,softOptInSelected$,hardOptInShown$,hardOptInSelected$,routeWillEnter$}from'@shopgate/engage/core/streams';import{getCurrentQuery}from'@shopgate/engage/core/selectors';import{track}from'@shopgate/engage/tracking/helpers';import{reloadApp}from'@shopgate/engage/core/action-creators';import{appSupportsCookieConsent,hasSGJavaScriptBridge}from'@shopgate/engage/core/helpers';import{analyticsSetConsent}from'@shopgate/engage/core/commands';import{cookieConsentInitialized$,cookieConsentUpdated$}from"../streams";import{getIsCookieConsentHandled}from"../selectors/cookieConsent";import{PRIVACY_SETTINGS_PATTERN}from"../constants";var softTrackingOptInShown$=softOptInShown$.filter(function(_ref){var _action$meta;var action=_ref.action;return(action===null||action===void 0?void 0:(_action$meta=action.meta)===null||_action$meta===void 0?void 0:_action$meta.permission)==='tracking';});var softTrackingOptInSelected$=softOptInSelected$.filter(function(_ref2){var _action$meta2;var action=_ref2.action;return(action===null||action===void 0?void 0:(_action$meta2=action.meta)===null||_action$meta2===void 0?void 0:_action$meta2.permission)==='tracking';});var hardTrackingOptInShown$=hardOptInShown$.filter(function(_ref3){var _action$meta3;var action=_ref3.action;return(action===null||action===void 0?void 0:(_action$meta3=action.meta)===null||_action$meta3===void 0?void 0:_action$meta3.permission)==='tracking';});var hardTrackingOptInSelected$=hardOptInSelected$.filter(function(_ref4){var _action$meta4;var action=_ref4.action;return(action===null||action===void 0?void 0:(_action$meta4=action.meta)===null||_action$meta4===void 0?void 0:_action$meta4.permission)==='tracking';});var privacySettingsWillEnter$=routeWillEnter$.filter(function(_ref5){var action=_ref5.action;return action.route.pattern===PRIVACY_SETTINGS_PATTERN;})// Only emit when cookie consent is already handled
.filter(function(_ref6){var getState=_ref6.getState;return getIsCookieConsentHandled(getState());});/**
* Analytics subscriptions
* @param {Function} subscribe The subscribe function
*/export default function analytics(subscribe){/**
* Sends cookie consent decisions to the app
* @param {Object} params Function params
* @param {Object} params.action Cookie consent update action
*/var sendConsentToApp=/*#__PURE__*/function(){var _ref8=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref7){var action,statisticsCookiesAccepted,comfortCookiesAccepted;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:action=_ref7.action;if(!(!appSupportsCookieConsent()||!hasSGJavaScriptBridge())){_context.next=3;break;}return _context.abrupt("return");case 3:statisticsCookiesAccepted=action.statisticsCookiesAccepted,comfortCookiesAccepted=action.comfortCookiesAccepted;// Send consent decisions to the app
_context.next=6;return analyticsSetConsent({statistics:statisticsCookiesAccepted,comfort:comfortCookiesAccepted});case 6:case"end":return _context.stop();}},_callee);}));return function sendConsentToApp(_x){return _ref8.apply(this,arguments);};}();subscribe(cookieConsentInitialized$,/*#__PURE__*/function(){var _ref10=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(_ref9){var action;return _regeneratorRuntime.wrap(function _callee2$(_context2){while(1)switch(_context2.prev=_context2.next){case 0:action=_ref9.action;_context2.next=3;return sendConsentToApp({action:action});case 3:case"end":return _context2.stop();}},_callee2);}));return function(_x2){return _ref10.apply(this,arguments);};}());subscribe(cookieConsentUpdated$,/*#__PURE__*/function(){var _ref12=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(_ref11){var dispatch,action;return _regeneratorRuntime.wrap(function _callee3$(_context3){while(1)switch(_context3.prev=_context3.next){case 0:dispatch=_ref11.dispatch,action=_ref11.action;_context3.next=3;return sendConsentToApp({action:action});case 3:// The PWA is reloaded whenever cookie consent settings changed to guarantee that all trackers
// are turned off
dispatch(reloadApp());case 4:case"end":return _context3.stop();}},_callee3);}));return function(_x3){return _ref12.apply(this,arguments);};}());subscribe(softTrackingOptInShown$,function(_ref13){var getState=_ref13.getState;track('customEvent',{eventCategory:'softTrackingOptIn',eventAction:'shown',eventLabel:null,additionalEventParams:{eventName:'softTrackingOptInShown'}},getState());});subscribe(softTrackingOptInSelected$,function(_ref14){var action=_ref14.action,getState=_ref14.getState;var selection=action.selection,_action$meta5=action.meta,permission=_action$meta5.permission,meta=_objectWithoutProperties(_action$meta5,_excluded);track('customEvent',{eventCategory:'softTrackingOptIn',eventAction:'decision',eventLabel:selection,additionalEventParams:_extends({eventName:'softTrackingOptInSelected',selection:selection},meta)},getState());});subscribe(hardTrackingOptInShown$,function(_ref15){var action=_ref15.action,getState=_ref15.getState;var _action$meta6=action.meta,permission=_action$meta6.permission,meta=_objectWithoutProperties(_action$meta6,_excluded2);track('customEvent',{eventCategory:'hardTrackingOptIn',eventAction:'shown',eventLabel:null,additionalEventParams:_extends({eventName:'hardTrackingOptInShown'},meta)},getState());});subscribe(hardTrackingOptInSelected$,function(_ref16){var action=_ref16.action,getState=_ref16.getState;var selection=action.selection,_action$meta7=action.meta,permission=_action$meta7.permission,meta=_objectWithoutProperties(_action$meta7,_excluded3);track('customEvent',{eventCategory:'hardTrackingOptIn',eventAction:'decision',eventLabel:selection,additionalEventParams:_extends({eventName:'hardTrackingOptInSelected',selection:selection},meta)},getState());});subscribe(cookieConsentUpdated$,function(_ref17){var action=_ref17.action,getState=_ref17.getState;var _getCurrentQuery=getCurrentQuery(getState()),_getCurrentQuery$sour=_getCurrentQuery.source,source=_getCurrentQuery$sour===void 0?'other':_getCurrentQuery$sour;var allowComfortNew=action.comfortCookiesAccepted,allowStatisticsNew=action.statisticsCookiesAccepted,_action$previous=action.previous,_action$previous2=_action$previous===void 0?{}:_action$previous,allowComfortOld=_action$previous2.comfortCookiesAccepted,allowStatisticsOld=_action$previous2.statisticsCookiesAccepted;track('customEvent',{eventCategory:'softTrackingSettings',eventAction:'changed',eventLabel:null,additionalEventParams:{eventName:'softTrackingSettingsChanged',context:source,allowComfortOld:allowComfortOld,allowComfortNew:allowComfortNew,allowStatisticsOld:allowStatisticsOld,allowStatisticsNew:allowStatisticsNew}},getState());});subscribe(privacySettingsWillEnter$,function(_ref18){var getState=_ref18.getState;var _getCurrentQuery2=getCurrentQuery(getState()),_getCurrentQuery2$sou=_getCurrentQuery2.source,source=_getCurrentQuery2$sou===void 0?'other':_getCurrentQuery2$sou;track('customEvent',{eventCategory:'softTrackingSettings',eventAction:'shown',eventLabel:null,additionalEventParams:{eventName:'softTrackingSettingsShown',context:source}},getState());});}