diff --git a/libs/blocks/marketo/marketo.js b/libs/blocks/marketo/marketo.js index a99f83500a..5825a853e1 100644 --- a/libs/blocks/marketo/marketo.js +++ b/libs/blocks/marketo/marketo.js @@ -29,6 +29,8 @@ const MUNCHKIN_ID = 'marketo munckin'; const SUCCESS_TYPE = 'form.success.type'; const SUCCESS_CONTENT = 'form.success.content'; const SUCCESS_SECTION = 'form.success.section'; +const PROGRAM_POI = 'program.poi'; +const POI_FILTER = 'field_filters.products'; const FORM_MAP = { 'success-type': SUCCESS_TYPE, 'destination-type': SUCCESS_TYPE, @@ -39,6 +41,7 @@ const FORM_MAP = { 'sfdc-campaign-id': 'program.campaignids.sfdc', }; export const FORM_PARAM = 'form'; +const QUERY_POI = 'poi'; export const formValidate = (formEl) => { formEl.classList.remove('hide-errors'); @@ -53,6 +56,7 @@ export const decorateURL = (destination, baseURL = window.location) => { const { hostname, pathname, search, hash } = destinationUrl; if (!hostname) { + /* c8 ignore next 2 */ throw new Error('URL does not have a valid host'); } @@ -146,6 +150,14 @@ export const formSuccess = (formEl, formData) => { return false; }; +export function setProductOfInterest(formData, search = window.location.search) { + const productOfInterest = new URLSearchParams(search).get(QUERY_POI); + if (!productOfInterest) return; + + formData[PROGRAM_POI] = productOfInterest; + formData[POI_FILTER] = 'hidden'; +} + const readyForm = (form, formData) => { const formEl = form.getFormElem().get(0); const el = formEl.closest('.marketo'); @@ -238,6 +250,7 @@ export default function init(el) { if (destinationUrl) formData[SUCCESS_CONTENT] = destinationUrl; } + setProductOfInterest(formData); setPreferences(formData); const fragment = new DocumentFragment(); diff --git a/test/blocks/marketo/marketo.test.js b/test/blocks/marketo/marketo.test.js index 2a8fa9e91d..fd833cebfc 100644 --- a/test/blocks/marketo/marketo.test.js +++ b/test/blocks/marketo/marketo.test.js @@ -3,7 +3,7 @@ import { expect } from '@esm-bundle/chai'; import sinon, { stub } from 'sinon'; import { delay } from '../../helpers/waitfor.js'; import { setConfig } from '../../../libs/utils/utils.js'; -import init, { setPreferences, decorateURL, FORM_PARAM } from '../../../libs/blocks/marketo/marketo.js'; +import init, { setPreferences, decorateURL, setProductOfInterest, FORM_PARAM } from '../../../libs/blocks/marketo/marketo.js'; const innerHTML = await readFile({ path: './mocks/body.html' }); window.lana = { log: stub() }; @@ -44,6 +44,16 @@ describe('marketo', () => { }); }); +describe('marketo setProductOfInterest', () => { + it('sets POI by query param', async () => { + const formData = {}; + setProductOfInterest(formData, '?poi=poi1'); + + expect(formData).to.have.property('program.poi'); + expect(formData['program.poi']).to.equal('poi1'); + }); +}); + describe('marketo decorateURL', () => { it('decorates absolute URL with local base URL', () => { const baseURL = new URL('http://localhost:6456/marketo-block');