Search code examples
javascriptif-statementgoogle-tag-manager

Cant get "if statement" to work in in google tag manager


Trying to add an if statement to my custom javascript variable in google tag manager to retrieve a value from the checkout cart.

Depending on if the customer puts in a promo code or not, the scraping position of the value appears on two different locations. I'm trying to sort those out with an if function, but it doesn't work.

Anyone finding a solution?

This is the code:

    function() {
  var MBA = document.querySelectorAll(".xc-base>main>.add-subscription-page>.xc-form.add-subscription-page>.receipt-container>div>span")[3].innerText.match(/^.{1}(.*).{0}/i)[1].trim();
  
  var YB = document.querySelectorAll(".xc-base>main>.add-subscription-page>.xc-form.add-subscription-page>.receipt-container>div>span")[5].innerText.match(/^.{1}(.*).{0}/i)[1].trim();
  
  var YA = document.querySelectorAll(".xc-base>main>.add-subscription-page>.xc-form.add-subscription-page>.receipt-container>div>span")[3].innerText.match(/^.{1}(.*).{0}/i)[1].trim();
      
if (MBA = "0.00/month") {
    return YB;
  } else {
    return YA;
  }   
}

And just to clarify, when I du put in this var in the console:

document.querySelectorAll(".xc-base>main>.add-subscription-page>.xc-form.add-subscription-page>.receipt-container>div>span")[3].innerText.match(/^.{1}(.*).{0}/i)[1].trim()

It does return "0.00/month".


Solution

  • You can use comparison operator instead of assignment:

    (MBA === "0.00/month") or (MBA == "0.00/month") if you dont need strict equality check. Right now you do assign operation, instead of comparison. (MBA = "0.00/month")