Implementing custom code using Adobe Launch - Part 2 | Community
Skip to main content
Level 4
May 25, 2022
Solved

Implementing custom code using Adobe Launch - Part 2

  • May 25, 2022
  • 2 replies
  • 1767 views
<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?

 

 

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by yuhuisg

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;

2 replies

yuhuisg
Community Advisor
Community Advisor
May 26, 2022

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

Alexis_Cazes_
Level 10
May 26, 2022

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

 

 

xcode2295Author
Level 4
June 8, 2022

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;
  })();

 

yuhuisg
Community Advisor
yuhuisgCommunity AdvisorAccepted solution
Community Advisor
June 9, 2022

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;