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L,OAAIA,aAAkCC,SAC3BD,EAAuB,GAG3BA,CAAsB,EAG3BN,EAAsBD,I,MACxB,OAA2D,QAApD,EAAAS,SAASC,cAAc,cAAcV,EAAQW,eAAO,eAAEC,cAAe,EAAE,EAG5EN,EAA+BN,GAC1BA,EAAQa,QAAQb,EAAQc,eAAeF,aAAe,GAG3D9B,EAAgB,CAACJ,EAAyBqC,IACrCrC,EAAWqB,QAAOJ,GAAKoB,EAAgBC,SAASrB,EAAEQ,SAGvDc,EAAclC,I,MAChB,QAA+B,IAApBpB,OAAOuD,UACd,OAGJ,MAAMnD,EAA+B,QAAtB,EAAAgB,aAAK,EAALA,EAAOf,uBAAe,eAAEC,GAClCX,EAAeS,KAIpBJ,OAAOuD,UAAUC,KAAK7D,EAAeS,IACrCR,EAA0BQ,GAAUT,EAAeS,GAAO,OAG7B,IAAtBJ,OAAOC,YACdD,OAAOC,WAAW6C,UAAUW,OAAM,KAAMC,OAxIdzD,EAwImCD,OAAOC,YAvIzD,mBAAmB0D,GAAG,mDAAoD7D,IACjF,MAAM8D,EAAc/D,EAAkBC,GAEjC8D,IAILjE,EAAeiE,EAAYxD,QAAUwD,EACrCN,EAAWxD,GAAc,IAG7BG,EAAW,mBAAmB0D,GAAG,wBAAyB7D,IACtD,MAAM8D,EAAc/D,EAAkBC,GAEjC8D,IAILjE,EAAeiE,EAAYxD,QAAUwD,EAAW,SAGpD3D,EAAW,mBAAmB0D,GAAG,kBAAmB7D,IAC5CA,EAAc+D,uBAAyB/D,EAAcgE,WACrDR,EAAWxD,EACf,IAzBqB,IAACG,CAwIqD,G","sources":["webpack://ERV.Web/./Assets/script/erv/epi_forms_gtm_tracking.ts"],"sourcesContent":["type InputFormField = {\r\n label: string,\r\n value: string\r\n}\r\n\r\ntype SelectFormField = InputFormField & {\r\n optionLabel: string\r\n}\r\n\r\ntype FormField = InputFormField | SelectFormField\r\n\r\ntype GtmFormInfo = {\r\n event: string,\r\n eventCategory: string,\r\n epiFormsEventType: string,\r\n siteUrl: string,\r\n formId: string,\r\n formName: string,\r\n formFields: object[]\r\n}\r\n\r\ntype EpiFormsStepNavigationEventType = \"formsNavigationNextStep\" | \"formsNavigationPrevStep\"\r\ntype EpiFormsEventType = EpiFormsStepNavigationEventType | \"formsStartSubmitting\"\r\n\r\nlet epiFormsGtmObj: Record = {}\r\nlet lastPushedFormInteraction: Record = {}\r\n\r\nconst attachEventListeners = ($$epiforms) => {\r\n $$epiforms(\".EPiServerForms\").on(\"formsNavigationNextStep formsNavigationPrevStep\", (epiFormsEvent) => {\r\n const gtmFormInfo = createGtmFormInfo(epiFormsEvent);\r\n\r\n if (!gtmFormInfo) {\r\n return;\r\n }\r\n\r\n epiFormsGtmObj[gtmFormInfo.formId] = gtmFormInfo;\r\n pushGtmObj(epiFormsEvent);\r\n });\r\n\r\n $$epiforms(\".EPiServerForms\").on(\"formsStartSubmitting\", (epiFormsEvent) => {\r\n const gtmFormInfo = createGtmFormInfo(epiFormsEvent);\r\n\r\n if (!gtmFormInfo) {\r\n return;\r\n }\r\n\r\n epiFormsGtmObj[gtmFormInfo.formId] = gtmFormInfo;\r\n });\r\n\r\n $$epiforms(\".EPiServerForms\").on(\"formsSubmitted\", (epiFormsEvent) => {\r\n if (epiFormsEvent.isFinalizedSubmission && epiFormsEvent.isSuccess) {\r\n pushGtmObj(epiFormsEvent);\r\n }\r\n });\r\n}\r\n\r\nconst createGtmFormInfo = (epiFormsEvent: any): GtmFormInfo | undefined => {\r\n const currentForm = window.$$epiforms(epiFormsEvent.target).get(0);\r\n const formId = epiFormsEvent?.workingFormInfo?.Id;\r\n if (!formId) {\r\n console.warn(\"[EpiForms GTM] No formId defined.\")\r\n return undefined;\r\n }\r\n\r\n const gtmFormDetailsElement = getGtmDetailsFormElement(currentForm);\r\n if (!gtmFormDetailsElement) {\r\n console.warn(\"[EpiForms GTM] No gtmFormDetilsElement found.\");\r\n return undefined;\r\n }\r\n\r\n const formName = gtmFormDetailsElement.dataset['gtmFormName'] || 'No form name specified';\r\n const formFieldsToIncludeInGtm = getFormFieldsToInclude(gtmFormDetailsElement);\r\n const formFields = getFormFields(currentForm.elements)\r\n const filteredFormFields = filterByLabel(formFields, formFieldsToIncludeInGtm)\r\n\r\n return {\r\n event: getEventName(formId, epiFormsEvent.type),\r\n eventCategory: 'EpiForms',\r\n epiFormsEventType: epiFormsEvent.type,\r\n siteUrl: window.location.href,\r\n formId: formId,\r\n formName: formName,\r\n formFields: filteredFormFields\r\n }\r\n}\r\n\r\nconst getEventName = (formId: string, epiFormsEventType: EpiFormsEventType) => {\r\n const isFirstInteraction = !lastPushedFormInteraction[formId]\r\n\r\n if (epiFormsEventType == 'formsStartSubmitting') {\r\n return 'EpiFormsInteraction - Submit'\r\n }\r\n\r\n if (isFirstInteraction) {\r\n return 'EpiFormsInteraction - InitialInteraction'\r\n }\r\n\r\n return 'EpiFormsInteraction';\r\n}\r\n\r\nconst getFormFieldsToInclude = (formNameElement: HTMLElement): string[] => {\r\n return formNameElement?.dataset[\"gtmFieldsToInclude\"]?.split(',')?.map(x => x.trim()) || [];\r\n}\r\n\r\nconst getFormFields = (elements: NodeListOf): FormField[] => {\r\n const elementsWithLabels = Array.from(elements).filter(element => !!getLabelForElement(element))\r\n const formFields = elementsWithLabels.map(element => {\r\n if (element instanceof HTMLSelectElement) {\r\n return {\r\n label: getLabelForElement(element),\r\n value: element.value,\r\n optionLabel: getSelectedOptionForElement(element)\r\n }\r\n }\r\n\r\n return {\r\n label: getLabelForElement(element),\r\n value: element.value\r\n }\r\n });\r\n\r\n return formFields;\r\n}\r\n\r\nconst getGtmDetailsFormElement = (currentForm: any): HTMLElement | undefined => {\r\n let gtmFormDetailsElements = currentForm.elements[\"gtmFormDetails\"];\r\n if (!gtmFormDetailsElements) {\r\n return undefined;\r\n }\r\n if (gtmFormDetailsElements instanceof NodeList) {\r\n return gtmFormDetailsElements[0] as HTMLElement\r\n }\r\n\r\n return gtmFormDetailsElements;\r\n}\r\n\r\nconst getLabelForElement = (element: HTMLInputElement) => {\r\n return document.querySelector(`label[for=\"${element.id}\"]`)?.textContent || ''\r\n}\r\n\r\nconst getSelectedOptionForElement = (element: HTMLSelectElement) => {\r\n return element.options[element.selectedIndex].textContent || ''\r\n}\r\n\r\nconst filterByLabel = (formFields: FormField[], labelsToInclude: string[]) => {\r\n return formFields.filter(x => labelsToInclude.includes(x.label))\r\n}\r\n\r\nconst pushGtmObj = (event: any) => {\r\n if (typeof window.dataLayer == 'undefined') {\r\n return;\r\n }\r\n\r\n const formId = event?.workingFormInfo?.Id;\r\n if (!epiFormsGtmObj[formId]) {\r\n return;\r\n }\r\n\r\n window.dataLayer.push(epiFormsGtmObj[formId]);\r\n lastPushedFormInteraction[formId] = epiFormsGtmObj[formId];\r\n}\r\n\r\nif (typeof window.$$epiforms !== 'undefined') {\r\n window.$$epiforms(document).ready(() => attachEventListeners(window.$$epiforms));\r\n}"],"names":["epiFormsGtmObj","lastPushedFormInteraction","createGtmFormInfo","epiFormsEvent","currentForm","window","$$epiforms","target","get","formId","workingFormInfo","Id","console","warn","gtmFormDetailsElement","getGtmDetailsFormElement","formName","dataset","formFieldsToIncludeInGtm","getFormFieldsToInclude","formFields","getFormFields","elements","filteredFormFields","filterByLabel","event","getEventName","type","eventCategory","epiFormsEventType","siteUrl","location","href","isFirstInteraction","formNameElement","split","map","x","trim","Array","from","filter","element","getLabelForElement","HTMLSelectElement","label","value","optionLabel","getSelectedOptionForElement","gtmFormDetailsElements","NodeList","document","querySelector","id","textContent","options","selectedIndex","labelsToInclude","includes","pushGtmObj","dataLayer","push","ready","attachEventListeners","on","gtmFormInfo","isFinalizedSubmission","isSuccess"],"sourceRoot":""}