Ssalustri
Ssalustri

Reputation: 1

Google Tag Manager Custom Variable return Undefined

I'm working with Google Tag Manager using a Custom JS Variable to store data about user's privacy consent.

I tried the function below with a variable for privacy that is set to true only if the user clicks on a specific button.

function controlloConsenso() {
  var privacyAccettata = false;
  var checkPrivacy = document.querySelector(".ginger_btn_accept_all");
  checkPrivacy.addEventListener("click", function() {
    privacyAccettata = true;
  });
  return privacyAccettata;
}

The problem is that the variable result is undefined within the GTM Debugger.

When trying the script outside the first function (that GTM doesn't accept) the code works fine (I've tested it in Chrome Console).

var privacyAccettata = false;
var checkPrivacy = document.querySelector(".ginger_btn_accept_all");
checkPrivacy.addEventListener("click", function() {
    privacyAccettata = true;
});

Upvotes: 0

Views: 1015

Answers (1)

vinoaj
vinoaj

Reputation: 1674

Omit the name from your function and it should work. That is, change your custom JavaScript tag to:

// function controlloConsenso() {
function(){
  var privacyAccettata = false;
  var checkPrivacy = document.querySelector(".ginger_btn_accept_all");
  checkPrivacy.addEventListener("click", function() {
      privacyAccettata = true;
    });
  return privacyAccettata;
}

Upvotes: 2

Related Questions