Javascript - variable in object

185 Views Asked by At

Given that I have the following script, how can I append/use varible portVal in the object i.e "investment_value": portVal

  var portVal = 100000
  setTimeout(invalidForm, 2000);
    function invalidForm() {
      utag.link({
          "event_name": "invalid form submission",
          "investment_value": portVal,
          "page_title": "Calculator - Invalid form submission",
          "error_message": "Mandatory fields",
          "language":   "en",
          "country":    "Test",
          "segment":    "Test",
          "sub_segment":"Test"
      });
  }

How do I console.log that object's investment_value?

1

There are 1 best solutions below

0
On

You have a tealium tag there.

Tealium is a TMS. Which changes things.

Tealium has its own concept of variables. Data Layer variables. window.utag_data is the object containing the DL variables for tealium. Therefore, if the portVal is declared and set via Tealium ui, you're supposed to access it like so: window.utag_data.portVal

But all this can be easily checked in the console. No need to deploy Tealium's code and try debugging it before local console debugging.

There's another issue with TMSes. The timing. Let's assume the portVal is just a global var, so, window.portVal. But it may be declared later in the pageload/render process than it's being accessed from Tealium. This can (and should) be debugged through breakpoints, but really, you can just debug it in the tealium object using something like typeof portVal rather than portVal in case breakpoints feel intimidating.