Expand my Community achievements bar.

Join us on September 25th for a must-attend webinar featuring Adobe Experience Maker winner Anish Raul. Discover how leading enterprises are adopting AI into their workflows securely, responsibly, and at scale.

Mark Solution

This conversation has been locked due to inactivity. Please create a new post.

SOLVED

Custom Code cannot trigger

Avatar

Level 1

Hi guys , I have a problem that My custom code didn't work in data collection and I don't know why 

 

Here is the code :

var ArticleCheckBox = document.querySelectorAll('[id^="article"]');


ArticleCheckBox.forEach(function(element){
                 element.addEventListener('click',function(){
                             var event = {eVar71: element.getAttribute("name"),};
                             trigger(event);
                 });
});

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

I believe you're doing it wrongly. Here's my suggestion to update the Rule:

  1. Remove this Custom Code event.
  2. Add a Click event, using [id^="article"] as your selector.
  3. Add an Adobe Analytics "Set Variables" action. In there set eVar71 to %this.getAttribute(name)%.
    1. Using %this.name% might work too, but I haven't tried it myself.

So what's happening here is that you're letting Launch detect the click for you, and when it does, you're instructing it to set eVar71 to the clicked element's "name" attribute.

Hope that helps! 😀

View solution in original post

3 Replies

Avatar

Community Advisor

Hello @Wasai 

Are you getting any errors in the console or the event isn't triggering?

 

A good way to troubleshoot is by writing logs with the console.log in the custom code and checking at which line it is breaking.

 

 


     Manoj
     Find me on LinkedIn

Avatar

Level 1

I add console.log in the code but the console didn't show anything or warning I think because of my code didn't trigger so it may not show message on the console

 

var ArticleCheckBox = document.querySelectorAll('[id^="article"]');
console.log("123")

ArticleCheckBox.forEach(function(element){
console.log("456")
element.addEventListener('click',function(){
var event = {eVar71: element.getAttribute("name"),};
trigger(event);
});
});

Avatar

Correct answer by
Community Advisor

I believe you're doing it wrongly. Here's my suggestion to update the Rule:

  1. Remove this Custom Code event.
  2. Add a Click event, using [id^="article"] as your selector.
  3. Add an Adobe Analytics "Set Variables" action. In there set eVar71 to %this.getAttribute(name)%.
    1. Using %this.name% might work too, but I haven't tried it myself.

So what's happening here is that you're letting Launch detect the click for you, and when it does, you're instructing it to set eVar71 to the clicked element's "name" attribute.

Hope that helps! 😀