The onActionEvent function is a key part of the Personalization SDK (either SalesforceInteractions or Evergage namespace) that allows you to capture and modify user actions in real time. This function is used within the pageType configuration to send specific user action events to Personalization.
Purpose: The onActionEvent function is triggered whenever a specified user action occurs on a particular page type. It allows you to capture data related to that action, modify it, and then send it to the Personalization platform.
Common Use Case: A typical use case is capturing a user's search term on a search results page and sending this information as part of the user's attributes to be used in Personalization.
The provided code snippets show how to use the onActionEvent function in both the SalesforceInteractions and Evergage namespaces. Let's break down each part of the code.
1. Identifying the Page Type (name and isMatch Properties)
{
name: "search", // Name of the page type, e.g., search page.
isMatch: () => /\/search/.test(window.location.pathname), // Function to determine if the current page matches this page type.
name: The name property identifies the type of page you are targeting. In this case, it's a "search" page.
isMatch: The isMatch function checks the current URL to see if it matches the /search path. If it does, the configuration for this page type will be applied.
2. Using onActionEvent to Capture User Data
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;
}
return actionEvent;
},
onActionEvent: This function is executed whenever a user action occurs on the page type that matches the isMatch condition.
actionEvent Parameter: This object represents the action being taken by the user. The actionEvent object can be modified to include additional data before being sent to the Personalization platform.
Capturing the Search Term:
const searchTerm = SalesforceInteractions.cashDom(".content.searchTerm").text().trim();: This line uses a utility function (cashDom) to find the search term on the page. It locates the element with the class .searchTerm within the .content container, retrieves the text, and trims any extra whitespace.
if (searchTerm): Checks if a search term was found and is not empty.
Modifying actionEvent:
actionEvent.user = actionEvent.user || {};: Ensures that the user property exists on the actionEvent object.
actionEvent.user.attributes = actionEvent.user.attributes || {};: Ensures that the attributes property exists on the user object.
actionEvent.user.attributes.lastSearchTerm = searchTerm;: Adds the captured search term to the user's attributes as lastSearchTerm.
Return the Modified Event:
return actionEvent;: The modified actionEvent object, now containing the search term, is returned and subsequently sent to the Personalization platform.