Expand my Community achievements bar.

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!