Custom Code cannot trigger | Community
Skip to main content
June 24, 2022
Solved

Custom Code cannot trigger

  • June 24, 2022
  • 2 replies
  • 1211 views

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

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

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!  😀

2 replies

Manoj_Kumar
Community Advisor
Community Advisor
June 24, 2022

Hello @wasai-1 

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  | https://themartech.pro
Wasai-1Author
June 24, 2022

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

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

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!  😀