Hi,
I am trying to grab a string from a digitalData datalayer event and pass that to a a JS/jQuery "Custom Code" Data Element. I can get the right value to return in Console using the below code, but it passes nothing (likely "undefined") when implementing the same thing in the Custom Code Data Element.
if (digitalData.event.length) {
digitalData.event.forEach(function(e) {
if (e.eventName === "Product Search") {
console.log(e.eventDetails.search_query)
}});
} else {
/// do nothing
}
For additional background, the array is digitalData.event and each item in the array has a different number. What I am looking to capture/pass to the Data Element could related to any number in that array (digitalData.event.[n]). Within that item in the array, I am looking to identify that it has eventName = "Product Search" and then pass the string value of its eventDetails.search_query. Additionally, I do want it to pass nothing/unspecified if nothing in the array has an eventName = "Product Search".
In looking at documentation, I notice a lot of people pointing to the Data Layer Manager extension as created by Search Discovery. However, the sensitive nature of the site in question's industry often precludes use of 3rd-party providers wherein there is risk for data leakage or privacy concerns, so would prefer not to have to use that. If there is a first party Adobe extension, that might be acceptable to use, though seems like it should be achievable without such a thing (I could be wrong).
Any and all help would be greatly appreciated, thank you!!
Solved! Go to Solution.
Views
Replies
Total Likes
I was able to get this to work as intended using the following:
let searches = digitalData.event.filter(x => x.eventName.toLowerCase() === 'product search').reverse()
if (searches.length > 0) {
return searches[0].eventDetails.search_query
} else {
/// do nothing
}
Views
Replies
Total Likes
A data element can return one value only, yet from your code, it looks like you want to return a value whenever a "Product Search" event is found in your digitalData.event array. So generally, that data element isn't going to work reliably.
Also, recall that a data element only gets executed when it is used in another data element or in a Rule's event, condition or action.
If you want the search_query from the most recent item in digitalData.event, then you'd have to traverse your array from the last item (i.e. from index digitalData.event.length - 1) to the first item, and when it finds a "Product Search", then return that item's search_query.
Hope that helps.
I was able to get this to work as intended using the following:
let searches = digitalData.event.filter(x => x.eventName.toLowerCase() === 'product search').reverse()
if (searches.length > 0) {
return searches[0].eventDetails.search_query
} else {
/// do nothing
}
Views
Replies
Total Likes