1

window.pageDataLayer.userFeedback.firstName = user ? (user.firstName ? user['firstName'] : '') : ''
window.pageDataLayer.userFeedback.lastName = user ? (user.lastName ? user['secondName'] : '') : ''
window.pageDataLayer.userFeedback.DOB = user ? (user.DOB ? user['DOB'] : '') : ''

× TypeError: Cannot read property 'userFeedback' of undefined

How to save the application parameters to window objects.

0

1 Answer 1

1

You'll have to create the pageDataLayer-Object first. Right now you are trying to access the object userFeedback of the object pageDataLayer - which does not exist.

Also, there is no difference between accessing the user properties with dot-notation or in square bracktes (user.firstName is the same as user['firstName']) so you can omit the duplicate ternary operator there.

Lastly, and only in case your user object is on the global scope, you will get an error with your syntax if it does not exist - so you should access it with window as well.

So all in all, this should do the trick:

window.pageDataLayer = {
  userFeedback: {
    firstName: window.user ? user.firstName : '',
    lastName: window.user ? user.lastName : '',
    DOB: window.user ? user.DOB : ''
  }
}

console.log(window.pageDataLayer);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.