/
Record a Search event
Record a Search event
Raise a custom event when a visitor uses a search mechanism on the website.
Consider the following example search form:
<form id="search-form" action="/search">
<input type="text" name="q" />
</form>
A Send2CRM event could be raised each time this is submitted by using the following script:
let searchForm = document.querySelector('form#search-form');
if (searchForm) {
// Attach an event listener to the search form.
searchForm.addEventListener('submit', (evt) => {
// Don't submit just yet.
evt.preventDefault();
// Get search text.
let searchTextVal = searchForm.querySelector('input[name="q"]')?.value;
if (searchTextVal) {
// TODO: sanitize or least truncate long searchText before adding to the event?
// Raise a custom Send2CRM session event.
send2crm?.analytics?.setCustomEvent('WEBSEARCH', { searchText: searchTextVal });
// Submit the form now.
searchForm.submit();
}
});
}
All events are included in identified visitor updates to Salesforce.