Skip to main content

Using Same Job Function for Product Search and Campaign Target Group

How can we copy Job Function user enters on "Let's target the right talent" form to be used on Target Group of Campaign Form on Target Group step of Order Journey?

User already enters a Job Function on Let's target the right talent form which is the search-recommend-products (formerly called onboarding) step for searching products, however that does not necessarily mean that the user will be using the same job function for the target group for the campaign they will order.


User may enter X to get best performing channels like Legal, but later enter Y such as Legal - Criminology to the Target Group's Job Function of the campaign, therefore we don't want to make that opinion on behalf of the end user, but you can programmatically copy the Job Function entered in the search form to the campaign's target group as such:

const selectedJobFunctionIds = window.hapi.product.state.jobFunctionsIdsSelected.value

const copyJobFunctionFromSearchToTargetGroup = (jobFunctionIds) => {
// jobFunctions are in a nested tree structure
// so we need to first flatten it to find the option we are looking for
const jobFunctionsTree = window.hapi.product.state.jobFunctions.value
const flattenedJobFunctions = window.hapiUtils.getFlattenedTree(
jobFunctionsTree,
"children",
"name",
"id",
)

const jobFunctionsToCopy = []

jobFunctionIds.forEach(id => {
const jobFunction = flattenedJobFunctions.find(jobFunction => jobFunction.value === id)
if (jobFunction) {
jobFunctionsToCopy.push(jobFunction)
}
})

// now set it as the selected on campaignForm
window.hapi.campaign.state.campaignForm = window.hapiUtils.mergeDeepOverwriteArrays(
window.hapi.campaign.state.campaignForm.value,
{
targetGroup: {
jobCategory: jobFunctionsToCopy
}
}
)
}

copyJobFunctionFromSearchToTargetGroup(selectedJobFunctionIds)

In most cases you would want to run the above code every time user changes the Job Function in the search-recommend-products step. You can do that as such:

window.hapi.product.state.jobFunctionsIdsSelected.onChange((jobFunctionIds) => {
copyJobFunctionFromSearchToTargetGroup(jobFunctionIds)
})