hello!
Are we unable to leverage function() capabilities in custom code? Every script I have written in custom code with this function does not seem to capture the data element?
Is it best to use this style of logic when configuring data elements:
var x = example
return x
Thanks,
Ty
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hi @tyrese ,
can you provide a concrete example of what you want to achieve and what you have unsuccessfully tried yet?
You can of course write your own custom function in a data element, call it and return the result
function foo(){
return "bar";
}
return foo();
Views
Replies
Total Likes
hey,
Here is a real example of what I tried to implement, excuse any JS errors - is there anything glaringly wrong? Are we missing the return cashorbaltitle(); at the end?
function cashorbaltitle() {
var transc = document.querySelector('#tcontainer');
var baltitleElement = document.querySelector('anotherCSS');
var baltitle = baltitleElement ? baltitleElement.textContent : "Balance title not found";
if (transc) {
var transctitleElement = document.querySelector('#tcontainer > example');
var transctitle = transctitleElement ? transctitleElement.textContent : null;
return transctitle || baltitle;
}
return baltitle;
}
Thanks,
Ty
Views
Replies
Total Likes
Hey @tyrese
are you then also returning the result of the function? Or how exactly do you want to make it usable?
You can - I just had to test it, since I have frankly never used it before xD - actually return a function as Custom Code Data Element
return function(bar) {
_satellite.logger.debug(">>> BAR", bar);
};
And then call it from a Custom Code action.
let func = _satellite.getVar("DE Function");
if (func) {
func("foo");
func("bar");
func("hello");
}
Although as a best practice, I would rather go for a dedicated rule with helper functions placed in one or more Custom Code actions to not scatter this functionality across the Launch property.
So my recommended approach is typically one of these two
a) create a "Library Loaded" rule with Order "1" with a Custom Code action that declares a dedicated window scoped library object with its own namespace and place the functions in there
window.myLib = window.myLib || {
foo: function (bar) {
_satellite.logger.debug(">>> foo called with", bar);
},
hello: function (world) {
_satellite.logger.debug(">>> hello called with", world);
}
}
b) append these helper functions to the _satellite object, bearing in mind that in the future the library may end up getting its own function with the same name, so tread lightly there
But yes, technically it's possible to have function Data Elements
Thanks, this has been very helpful!
-Ty
Views
Replies
Total Likes