Expand my Community achievements bar.

Help shape the future of AI assistance by participating in this quick card sorting activity. Your input will help create a more effective system that better serves your needs and those of your colleagues.

Adobe Analytics Product String extension - data layer with an array

Avatar

Level 3

Hi,

I need to collect product data by a data layer, I have an array of products, something like:

digitalData = {
          "transaction": {
                              "item": [{
                                   "productInfo": {
                                              "productName": "My Product"
                                             }
                                   }]
 }
};


  Even if for Adobe Analytics Product String extension I see: "Use bracket notation to denote any object in the variable that is an array ( i.e. myDataLayer.thisIsAnArray[n].productID )", it seems not work.

Someone has a solution or experience about this?

Also, for a simple data element, can I read an array with the same sintx?

Many thanks. 

2 Replies

Avatar

Community Advisor

Try the AA Product String Builder extension by Search Discovery.

yuhuisg_0-1638368941845.png

 

Avatar

Level 10

This is why I never use third party public extension. You have no idea what they actually do under the hood.

 

I have created a private extension to do this but I think you can do this as custom javascript code. (done this before doing private extensions). 

 

Ok first we will create a helper to getObjectProperty at a specific location. Won't avoid you to do the chained validation if specific location exist then check next location exist etc.. (ps: directly ported from core library as they do not expose it as shared module)

 

/**
 * Returns the deep property value of an object.
 * @param {Object} obj The object where the property will be searched.
 * @param {String} property The property name to be returned. It can contain dots. (eg. prop.subprop1)
 * @returns {*}
 */
function getObjectProperty(obj, property) {
    var propertyChain = property ? property.split('.') : [];
    var currentValue = obj;

    for (var i = 0, len = propertyChain.length; i < len; i++) {
        if (currentValue == null) {
            return undefined;
        }

        currentValue = currentValue[propertyChain[i]];
    }

    return currentValue;
};

Ok now you can create this code in your data element to get the product details.

 

/**
 * Build the product string in Adobe Analytics format.
 * Grabs product details from the data layer
 * @param {Object} event Direct Call rule event details
 * @returns {String} products value in Adobe Analytics s.products format
 */
function getProductsDetails(event) {
    try {
        var pathToTransationItems = 'digitalData.transaction.item';
        var pathToProductName = 'productInfo.productName';
        var productsArray = getObjectProperty(window, pathToTransationItems);
        var productString = '';
        if (productsArray && productsArray.length > 0) {
            for (var i = 0; i < productsArray.length; i++) {
                var product = productsArray[i];
                var value = ';' + getObjectProperty(product, pathToProductName );
                productString += (i == 0) ? value : ',' + value;
            }
        }
        return productString;
    } catch (e) {
        _satellite.logger.error('Failed to get product details -- ', e);
    }
}

And then simply add 

try {
    return getProductsDetails();
} catch (e) {
    _satellite.logger.error('Failed to get product details', e);
}

I would advise you to consider deploying you own private extension. Check out my dev.to page as auto-tagging is in progress https://dev.to/alcazes