onActionEvent method is used in Salesforce Personalization (formerly Evergage) to capture user and account attributes and send them with events for personalization purposes. Here’s a breakdown of the key concepts and code samples:
The onActionEvent method is used to capture and modify user and account attributes before sending an event to Salesforce Personalization. This is typically done in two configurations:
Global configuration: Captures user data globally across the entire site.
pageTypes configuration: Captures data for specific types of pages (e.g., search, preferences).
The global configuration runs on all pages of the site and adds attributes (such as email or name) to the user object before sending the event.
Example: Capturing User Email Globally
global: {
onActionEvent: (actionEvent) => {
const email = SalesforceInteractions.cashDom("header.logged-in-user-email").text().trim();
if (email) {
actionEvent.user = actionEvent.user || {}; // Initialize user object if it doesn’t exist
actionEvent.user.attributes = actionEvent.user.attributes || {}; // Initialize attributes if needed
actionEvent.user.attributes.emailAddress = email; // Set email attribute
}
return actionEvent; // Return modified event
}
}
Purpose: This code captures the email of a logged-in user and adds it to the user.attributes.emailAddress field before sending an event.
The pageTypes configuration applies to specific pages and triggers when certain criteria are met (like being on a search results page or submitting a form).
Example: Capturing Search Term on a Search Page
pageTypes: [
{
name: "search", // Name of the page type
isMatch: () => /\/search/.test(window.location.pathname), // Matches URLs that contain "/search"
onActionEvent: (actionEvent) => {
const searchTerm = SalesforceInteractions.cashDom(".content.searchTerm").text().trim();
if (searchTerm) {
actionEvent.user = actionEvent.user || {};
actionEvent.user.attributes = actionEvent.user.attributes || {};
actionEvent.user.attributes.lastSearchTerm = searchTerm; // Add last search term to user attributes
}
return actionEvent;
}
},
]
Purpose: This code checks if the current page is a search page, and if so, captures the last search term and adds it to the user.attributes.lastSearchTerm field.
You can also use listeners to capture form submissions and other user interactions on specific pages or globally.
Global Example: Capturing User Information on Form Submission
global: {
listeners: [
SalesforceInteractions.listener("submit", "form.footer", () => {
const email = SalesforceInteractions.cashDom("form.footer input.email").val();
const name = SalesforceInteractions.cashDom("form.footer input.name").val();
SalesforceInteractions.sendEvent({
interaction: {
name: "Form Submission | Footer", // Name of the interaction
},
user: {
identities: {
emailAddress: email // Set email identity
},
attributes: {
name: name // Capture and set user name
}
}
});
})
]
}
Purpose: This listener listens for a form submission in the footer. When the form is submitted, it captures the user's email and name and sends an event to Salesforce Personalization.
Listeners can also be added in the pageTypes configuration to capture events only on specific pages.
Example: Capturing User Preferences on Preferences Page
pageTypes: [
{
name: "preferences",
isMatch: () => /\/preferences/.test(window.location.pathname), // Match the preferences page URL
listeners: [
SalesforceInteractions.listener("submit", "form.user-prefs", () => {
const email = SalesforceInteractions.cashDom("form.user-prefs input.email").val();
const name = SalesforceInteractions.cashDom("form.user-prefs input.name").val();
SalesforceInteractions.sendEvent({
interaction: {
name: "Form Submission | Preferences",
},
user: {
identities: {
emailAddress: email
},
attributes: {
name: name // Capture user name
}
}
});
})
]
},
]
Purpose: This listener tracks form submissions on the preferences page, capturing and sending the user's email and name to Salesforce Personalization.
Similar to user attributes, account attributes can also be captured and sent with events.
Example: Capturing Account Information from a Form
javascript
Copy code
global: {
listeners: [
SalesforceInteractions.listener("submit", "form.survey", () => {
const industry = SalesforceInteractions.cashDom("form.survey input.industry").val();
const employeeSize = SalesforceInteractions.cashDom("form.survey input.employeeSize").val();
SalesforceInteractions.sendEvent({
interaction: {
name: "Form Submission", // Interaction name
},
account: {
attributes: {
industry: industry, // Capture account industry
employeeSize: employeeSize // Capture company size
}
}
});
})
]
}
Purpose: This captures account-related attributes like industry and employee size from a survey form and sends them with the event.
onActionEvent: Modifies event data before it is sent to Salesforce Personalization by adding user and account attributes (like email, name, or search terms).
global configuration: Applies to all pages and can be used to capture user data (e.g., email, name) globally.
pageTypes configuration: Used for specific page types (e.g., search or preferences pages) and captures attributes or sends events based on user interactions on those pages.
listeners: Listen for specific events (like form submissions) and capture data (e.g., user identity or preferences) to send to Salesforce Personalization.
This method is crucial for personalizing experiences based on user data and interactions on your site.