Search code examples
javascriptgoogle-tag-manager

Parse error. '}' expected when using dot notation with data layer variable data


I have an object that I have added to the datalayer that has an array in it. In a custom html tag (javascript), I can pull the data out just fine. And it is still a well formed object. But when I use the dot notation to access the array, Tag Manager gives the error

Error at line 10, character 39: Parse error. '}' expected

Line 10 is the authData line below. And this happened when I click the Preview button to test the changes.

<script>
    var dlv = {{dlv - My-Variable}};
    var authData = { email: dlv.Items.0.Shipment.Address };
</script>

I have followed the steps in this post. https://www.simoahava.com/gtm-tips/access-array-members-in-the-data-layer/


Solution

  • An element in an index-dased array should be accessed using square bracket notation, like someArray[5]

     var authData = { email: dlv.Items[0].Shipment.Address };
    

    And a variable can't have "-" in its' name, because it's confusing JavaScript, wheteher "-" should be treated as arithmetical operator or as a part of a variable name

     My-Variable
    

    And the interpolation braces are used in JSX, not in JavaScript

     {{ }}