Configure CartPurchaseItem events – AA

Configure the CartPurchaseItem events in Adobe Analytics (DTM) or Adobe Launch.
When a visitor makes a purchase, the CartPurchase and CartPurchaseItem events are triggered. These two events always flow together in a batch. To handle these batch events, you need to add a custom code.

📘

Note

This topic is relevant only if you are using Adobe Analytics DTM or Adobe Analytics Launch integrated with Exchange.

Before you begin

Configure AA Launch with Exchange. For more information, see: Configuring Adobe Launch with Acoustic Exchange.

Assumptions
The code assumes that the product purchase details are stored in Adobe's object. One of the approaches to store the product information in Adobe s object is shown here. You can store the product information in any other format or any other variable in Adobe's object.

s.products ="Footwear;Running Shoes;1;69.95;222Tp;black,Running Essentials;Running Socks;10;29.99;223Tp;red"
s.prop1 = "2020RTP";
s.prop2 = "2000$";
s.prop4 = "5%";
s.prop5 = "50";
s.prop6 = "Rs";
s.prop7 = window.location.href;
var identifiersIndex = 0;
  • It is assumed that you have a mapper to map Adobe attributes with the Exchange attributes. For more information, see Acoustic Exchange. The following code snippet shows an example.
var attributesMapper = [];
attributesMapper.push({"adobeName":"prop1", "ubxName":"orderID","type":"string"});
attributesMapper.push({"adobeName":"prop2", "ubxName":"orderTotal","type":"number"});
attributesMapper.push({"adobeName":"prop4", "ubxName":"orderTax","type":"number"});
attributesMapper.push({"adobeName":"prop5", "ubxName":"orderShipping","type":"number"});
attributesMapper.push({"adobeName":"adobe_interactionId", "ubxName":"interactionId","type":"string"});
attributesMapper.push({"adobeName":"adobe_quantity", "ubxName":"quantity","type":"number"});
attributesMapper.push({"adobeName":"prop6", "ubxName":"currency","type":"string"});
attributesMapper.push({"adobeName": "prop3", "ubxName": "channeltenantId", "type": "string" })
  • The attributesMapperItem for cartPurchaseItem object should be a mapping from the object returned from iterator.getNext() and not the Adobe s object.
var attributesMapperItem = [];
attributesMapperItem.push({"adobeName":"prop1", "ubxName":"orderID","type":"string"});
attributesMapperItem.push({"adobeName":"prop2", "ubxName":"orderTotal","type":"number"});
attributesMapperItem.push({"adobeName":"prop4", "ubxName":"orderTax","type":"number"});
attributesMapperItem.push({"adobeName":"prop5", "ubxName":"orderShipping","type":"number"});
attributesMapperItem.push({"adobeName":"adobe_interactionId", "ubxName":"interactionId","type":"string"});
attributesMapperItem.push({"adobeName":"prop6", "ubxName":"currency","type":"string"});
attributesMapperItem.push({"adobeName":"adobe_color", "ubxName":"color","type":"string"});
attributesMapperItem.push({"adobeName":"adobe_price", "ubxName":"basePrice","type":"number"});
attributesMapperItem.push({"adobeName":"adobe_productName", "ubxName":"productName", "type":"string"});
attributesMapperItem.push({"adobeName":"adobe_price", "ubxName":"price", "type":"string"});
attributesMapperItem.push({"adobeName":"prop7", "ubxName":"productURL", "type":"string"});
attributesMapperItem.push({"adobeName": "adobe_category", "ubxName": "category", "type": "string" })
attributesMapperItem.push({"adobeName":"adobe_quantity", "ubxName":"quantity", "type":"string"});
attributesMapperItem.push({"adobeName":"adobe_productId", "ubxName":"productID", "type":"string"});
attributesMapperItem.push({"adobeName":"prop3", "ubxName": "channeltenantId", "type": "string" })

The following steps should be performed as part of the events mapping step for AA.

  1. Create an iterator to handle the multiple CartPurchaseItem events. It returns individual product item objects iteratively when its getNext() method is called.
var iterator = { 
cursor : -1,
products : s.products.split(','),
getNext : function(){
this.cursor++;

if(this.cursor >=  this.products.length)
return undefined;

var productDetails =  this.products[this.cursor].split(';');
var itemObj = {};
itemObj.adobe_category   =  productDetails[0];
itemObj.adobe_productName   =  productDetails[1];
itemObj.adobe_quantity   =  productDetails[2];
itemObj.adobe_price   =  productDetails[3];
itemObj.adobe_productId   =  productDetails[4];
itemObj.adobe_color   =  productDetails[5];
itemObj.prop1 = s.prop1;
itemObj.prop2 = s.prop2;
itemObj.prop3 = s.prop3;
itemObj.prop4= s.prop4;
itemObj.prop5 = s.prop5;
itemObj.prop6= s.prop6;
itemObj.prop7 = s.prop7;
return itemObj;
}
}
  1. Create an eventListMap as shown for ibmcartPurchase and ibmcartPurchaseItem events.
var eventListMap = [];
eventListMap.push({"ubxEventType" : "ibmcartPurchase", "identifiersMapper" : identifiersMapper, "attributesMapper" : attributesMapper}); 
eventListMap.push({"ubxEventType" : "ibmcartPurchaseItem", "identifiersMapper" : identifiersMapper, "attributesMapper" : attributesMapperItem, "itemIterator" : iterator });
  1. Add the functions for sending the events in batchevent format required by Exchange. Then, call the sendBatchEventFromJSONPayload function at the end.

📘

Note:

Do not make any changes to the code snippets provided in this step, and add them as-is.

sendBatchEventFromJSONPayload = function (payloadJson, eventMapperList, identifiersIndex) {
    if (!payloadJson)
    {
        ubxCapture.consoleWrite.error("payloadJson is missing");
        return;
    }

    if (!eventMapperList)
    {
        ubxCapture.consoleWrite.error("eventMapperList is missing");
        return;
    }
    else if (!Array.isArray(eventMapperList))
    {
        ubxCapture.consoleWrite.error("eventMapperList is not an array");
        return;
    }
    if (typeof identifiersIndex === 'undefined')
    {
        ubxCapture.consoleWrite.error("identifiersIndex is missing");
        return;
    }
    else if (identifiersIndex<0 || identifiersIndex>=eventMapperList.length)
    {
        ubxCapture.consoleWrite.error("identifiersIndex is not a valid number");
        return;
    }

    try
    {
        var eventList = [];
        for (var i = 0; i < eventMapperList.length; i++)
        {
          
           var eventMapper = {
             ubxEventType : eventMapperList[i].ubxEventType,
             identifiersMapper : eventMapperList[i].identifiersMapper,
             attributesMapper : eventMapperList[i].attributesMapper,
             itemIterator : eventMapperList[i].itemIterator
           }
            //var eventMapper = adobe_ubx.createEventMapperwithIterator(eventMapperList[i].ubxEventType,
                //eventMapperList[i].identifiersMapper,
               // eventMapperList[i].attributesMapper,eventMapperList[i].itemIterator);
                       if(eventMapper.itemIterator == undefined ){
                               var eventObj = adobe_ubx.mapToUBXEvent(payloadJson, eventMapper);
                               eventList.push(eventObj);
                              }
                             else{
                               var eventArr = adobe_ubx.mapToUBXEventItem(payloadJson, eventMapper,eventMapperList[i].itemIterator);
                               eventList = eventList.concat(eventArr);
                              }
                                                
        }

        if (eventList.length > 0)
        {
            var batchEvent = {};
           batchEvent.events = eventList;
           batchEvent.identifiers = eventList[identifiersIndex].identifiers;
           ibm_ubx.sendBatchEvent(batchEvent, adobe_ubx.host, adobe_ubx.authKey,"ADOBEANALYTICS",adobe_ubx.requestType);
        }
        else
        {
            ubxCapture.consoleWrite.log("No event mapped to UBX event.");
        }
    }
    catch (err)
    {
        ubxCapture.consoleWrite.error("Exception in sendBatchEventFromJSONPayload(): " + err);
    }
};

adobe_ubx.mapToUBXEventItem = function (payloadJson, adobeUBXEventMapperObj, itemIterator) {
     if(!itemIterator.hasOwnProperty('getNext')){
        ubxCapture.consoleWrite.error("getNext function is not define" );
        return false;
      }
    var eventObjArr = [];
  var item = {};
           while(item){     
            item = itemIterator.getNext();
    
      if(item == undefined){
        break;
      }
                          var eventObj = {};
                                    eventObj.eventCode = adobeUBXEventMapperObj.ubxEventType;
                                    eventObj.identifiers = adobe_ubx.createFieldArrayObject(payloadJson, adobeUBXEventMapperObj.identifiersMapper);
                                    eventObj.attributes = adobe_ubx.createFieldArrayObject(item, adobeUBXEventMapperObj.attributesMapper);
    eventObjArr.push(eventObj);

            }
            return eventObjArr;
};

adobe_ubx.createEventMapperwithIterator = function (ubxEventType, identifiersMapper, attributesMapper, itemIterator) {
    var eventMapper = {};
    eventMapper.ubxEventType = ubxEventType;
    eventMapper.identifiersMapper = identifiersMapper;
    eventMapper.attributesMapper = attributesMapper;
            eventMapper.itemIterator = itemIterator
    return eventMapper;
};

sendBatchEventFromJSONPayload(s, eventListMap, identifiersIndex);