Expand my Community achievements bar.

Join us January 15th for an AMA with Champion Achaia Walton, who will be talking about her article on Event-Based Reporting and Measuring Content Groups!
SOLVED

Implementing custom code using Adobe Launch - Part 2

Avatar

Level 4
<script type='text/javascript'>
document.addEventListener('DOMContentLoaded', function(event) {
    try {
        var img = document.createElement('img'),
    		cjcid = '855555',
    		cjactionid = (document.location.hostname.indexOf('mobile.') > -1) ? '360841' : '348384',
    		products = "",
    		oid = (typeof digitalData !== 'undefined' && typeof digitalData.order !== 'undefined') ? digitalData.order.id : 'no order id',
    		i = 1,
    		items = (typeof digitalData !== 'undefined' && typeof digitalData.order !== 'undefined') ? JSON.parse(JSON.stringify(bt.order.items)) : [],
    		eid = digitalData_cookie('cjevent'),
    		cjevent = (eid) ? "&CJEVENT=" + eid : "",
    		src="https://www.emjcd.com/u?" + 
    		        "TYPE=" + cjactionid + 
    		        "&CID=" + cjcid + 
    		        "&CURRENCY=USD" + 
    		        "&OID=" + oid + 
    		        "&METHOD=IMG" + 
    		        cjevent;
    	items.forEach(function(product) {
    		var param = [];
    		param.push("ITEM" + i + "=" + product.valuespecificsku);
    		param.push("AMT" + i + "=" + product.price*.965);
    		param.push("QTY" + i + "=" + product.quantity);
    		products += "&" + param.join("&");
    		i += 1;
    	});
    	img.width = "20";
    	img.height = "1";
    	img.src=src + products;
    	document.body.appendChild(img);
    } catch (ex) {console.log('error: cj: ' + ex.message);}
});
</script>

I have implemented below based on this Re: Implementing custom code using Adobe Launch

 

 

Now, My question is how do I implement a for loop like below "ITEM", "AMT", and "PRODUCT" using the custom code. Will it be something like this?

4a096c9a-a9f6-407a-9e14-6d117a039d94.png

 

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Double check that you're not using the IIFE method in your data element. It should just be the part between the (function { ... }()), i.e.

 var products = "";
i=1;
items = JSON.parse(JSON.stringify(bt.order.items));
items.forEach(function(product) {
    		var param = [];
            param.push("ITEM" + i + "=" + product.valuespecificsku);
    		param.push("AMT" + i + "=" + product.price * 0.965);
    		param.push("QTY" + i + "=" + product.quantity);
    		products += "&" + param.join("&");
    		i += 1;
  }); 
  return products;

View solution in original post

5 Replies

Avatar

Community Advisor

If I understand your post correctly, then yes, the code in your 2nd screenshot will work.

Avatar

Community Advisor

if the question is how do I create a data element to take in a parameter of type array and iterating through it then the answer is using the event option for data elements.

 

It is a little known fact but you can pass arguments to data element when using custom code as follow:

 

//syntax
_satellite.getVar('dataElementID', data);

//argument as array
_satellite.getVar('dataElementID', [1,2,3]);

//argument as object
_satellite.getVar('dataElementID', {"a":"b"});

//argument as primitive
_satellite.getVar('dataElementID', "test");
_satellite.getVar('dataElementID', 1);
_satellite.getVar('dataElementID', true);

//argument as value of another variable
var x = [1,2,3]
_satellite.getVar('dataElementID', x);

Now in your code you simply need to catch the event variable and you can use its value. So in your data element custom code add for example:

console.log(event)

Now call you data element in the developer console with one of the syntax above and you should see the value of the argument being printed.

 

If we come back to your loop, there are many ways to achieve this and forEach if one of the option. Notice that Adobe Launch supports ES6 and forEach also can return the index which is what I am going to use in my code.

1. Create data element and select custom code

2. We will consider that we will pass the "items" as an argument

3. Code would be as follow:

try {
    var items = event;
    //move params outside as ultimately we will output all to products as a return
    var params = [];
    //index is the array index staring at 0
    items.forEach(function(product, index) {
        var i = index + 1;
        params.push(`ITEM${i}=${product.valuespecificsku}`);
        params.push(`AMT${i}=${product.price * .965}`);
        params.push(`QTY${i}=${product.quantity}`);
    });
    return `&${param.join("&")}`;
} catch (e) {
    _satellite.logger.error('[Data Element] Failed in myDataElementName', e);
}

Hope this helps, please notice I used my event argument value.

 

here you have a nice dynamic data element, we could think of it as a function that returns a value based on arguments provided

 

 

Avatar

Level 4

Thanks for the insights and explanation. And, that is super helpful in understanding. 

I tried above but it says " illegal return statement"

 

Also, when I use the below code in the console which I have put as a custom code in the "ITEM" data element, I get the values printed in the console. Attached is the screenshot. But, when I call that value using _satellite.getvar("ITEM") . It does not print any value

(function(){ 
 var products = "";
i=1;
items = JSON.parse(JSON.stringify(bt.order.items));
items.forEach(function(product) {
    		var param = [];
            param.push("ITEM" + i + "=" + product.valuespecificsku);
    		param.push("AMT" + i + "=" + product.price * 0.965);
    		param.push("QTY" + i + "=" + product.quantity);
    		products += "&" + param.join("&");
    		i += 1;
  }); 
  return products;
  })();

4d825f4e-3647-4835-b4b1-6bb68f935967.png

 

Avatar

Correct answer by
Community Advisor

Double check that you're not using the IIFE method in your data element. It should just be the part between the (function { ... }()), i.e.

 var products = "";
i=1;
items = JSON.parse(JSON.stringify(bt.order.items));
items.forEach(function(product) {
    		var param = [];
            param.push("ITEM" + i + "=" + product.valuespecificsku);
    		param.push("AMT" + i + "=" + product.price * 0.965);
    		param.push("QTY" + i + "=" + product.quantity);
    		products += "&" + param.join("&");
    		i += 1;
  }); 
  return products;

Avatar

Level 4

Thank you.. That was super helpful regarding the IIFE method.