diff --git a/src/botPage/view/View.js b/src/botPage/view/View.js index ef8ce478c3..cb626fbd12 100644 --- a/src/botPage/view/View.js +++ b/src/botPage/view/View.js @@ -39,6 +39,7 @@ import { getToken, } from '../../common/utils/storageManager'; import { isProduction } from '../../common/utils/tools'; +import GTM from '../../common/gtm'; let realityCheckTimeout; @@ -352,6 +353,7 @@ export default class View { this.stop(); Elevio.logoutUser(); googleDrive.signOut(); + GTM.setVisitorId(); removeTokens(); }) .catch(() => {}); @@ -569,6 +571,7 @@ export default class View { .then(() => { this.stop(); Elevio.logoutUser(); + GTM.setVisitorId(); const activeToken = $(e.currentTarget).attr('value'); const tokenList = getTokenList(); setStorage('tokenList', ''); diff --git a/src/botPage/view/index.js b/src/botPage/view/index.js index e8d2e962bf..497d2d8c25 100644 --- a/src/botPage/view/index.js +++ b/src/botPage/view/index.js @@ -4,6 +4,7 @@ import 'notifyjs-browser'; import View from './View'; import '../../common/binary-ui/dropdown'; import Elevio from '../../common/elevio'; +import GTM from '../../common/gtm'; $.ajaxSetup({ cache: false, @@ -29,6 +30,7 @@ view.initPromise.then(() => { $('.barspinner').hide(); window.dispatchEvent(new Event('resize')); Elevio.init(); + GTM.setVisitorId(); trackJs.configure({ userId: $('.account-id') .first() diff --git a/src/common/appId.js b/src/common/appId.js index 6776205808..63f5b42cba 100644 --- a/src/common/appId.js +++ b/src/common/appId.js @@ -11,6 +11,7 @@ import { parseQueryString, isProduction, getExtension } from '../common/utils/to import { getLanguage } from './lang'; import AppIdMap from './appIdResolver'; import Elevio from './elevio'; +import GTM from './gtm'; export const AppConstants = Object.freeze({ STORAGE_ACTIVE_TOKEN: 'activeToken', @@ -133,6 +134,7 @@ export async function addTokenIfValid(token, tokenObjectList) { } catch (e) { removeToken(tokenObjectList[0].token); Elevio.logoutUser(); + GTM.setVisitorId(); throw e; } return api.disconnect(); diff --git a/src/common/gtm.js b/src/common/gtm.js new file mode 100644 index 0000000000..3fc21bd414 --- /dev/null +++ b/src/common/gtm.js @@ -0,0 +1,31 @@ +import { getAppIdFallback } from './appId'; +import AppIdMap from './appIdResolver'; +import { getTokenList } from './utils/storageManager'; + +const GTM = (() => { + const isGtmApplicable = () => Object.values(AppIdMap).includes(`${getAppIdFallback()}`); + + const pushDataLayer = data => { + if (isGtmApplicable()) { + // eslint-disable-next-line no-undef + dataLayer.push({ + ...data, + }); + } + }; + + const setVisitorId = () => { + const tokenList = getTokenList(); + if (tokenList.length > 0) { + pushDataLayer({ visitorId: tokenList[0].loginInfo.loginid }); + } else { + pushDataLayer({ visitorId: undefined }); + } + }; + + return { + setVisitorId, + }; +})(); + +export default GTM;