Skip to content

Instantly share code, notes, and snippets.

@knennigtri
Last active April 5, 2022 14:15
Show Gist options
  • Save knennigtri/9cf49b0dcb2bb1219acfa436d1ec4331 to your computer and use it in GitHub Desktop.
Save knennigtri/9cf49b0dcb2bb1219acfa436d1ec4331 to your computer and use it in GitHub Desktop.
Adobe Launch data element for CIF and Core components
/*
* This method can get core components from the ACDL via eventInfo.path or by first occurance
* Example 1. With an event that contains event.message.eventInfo.path value (core cmp event)
* var cmpProperty = getCmpProperty(event);
* Example 2. It can be used to get the page component properties even if the event was not triggered by the event
* var pageCmpProperty = getCmpProperty(event, "page");
* Example 3. It can be used to get the first cmp instance of a specified component on the page
* var productCmpProperty = getCmpProperty(event, "component", "product");
* returns an JSON object that contains the
* this.path - the unique component path in the ACDL
* this.component - the component json
*/
return function(event, cmpType, cmpName){
if(event && event.message && event.message.hasOwnProperty("eventInfo")){
var coreCmpPath="";
//If the eventInfo.path is not given, then find the first occurance of the cmp in the ACDL
if(!event.message.eventInfo.hasOwnProperty("path") && cmpType){
var jsonObj;
var cmpPathStr;
//Get the page json if cmpType equals the page cmp
if(cmpType.toLowerCase().includes("page")){
jsonObj= JSON.parse(JSON.stringify(event.message.eventInfo.page));
cmpPathStr = "page.";
cmpName = "page"
} else if(cmpType.toLowerCase().includes("component")){
//Get the component json for all other components
jsonObj= JSON.parse(JSON.stringify(event.message.eventInfo.component));
cmpPathStr = "component.";
}
//Find the first occurance in the json (page json only has 1)
for(var key in jsonObj){
if(JSON.stringify(key).includes(cmpName)){
val = JSON.stringify(key).replaceAll('\"','');
coreCmpPath = cmpPathStr + val;
//console.debug("Using cmp in ACDL: " + coreCmpPath);
}
}
} else {
//Get component path that triggered the event. This is standard with AEM Core Components
coreCmpPath = event.message.eventInfo.path;
}
var cmpEvent = {
//include the unique component path in the ACDL
path: coreCmpPath,
//get the state of the component
component: window.adobeDataLayer.getState(coreCmpPath)
};
//return the json object of the component
return cmpEvent;
}
}
@knennigtri
Copy link
Author

Example data element using this method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment