Search code examples
cssgoogle-tag-manager

GTM - CSS Element


I'm wanting to create a variable to grab the username from a landing page (assuming it's possible). In my attached examples, I'd like to grab the text "Landing_page_test".

I'm just learning CSS so I'm not able to single out just that text.

Any thoughts/suggestions would be much appreciated! enter image description here

Console

Elements Pane of Landing Page


Solution

  • document.querySelector returns an element, not the text. Add .innerText

    Try it out on this page: document.querySelector("a.question-hyperlink").innerText to get the name of the question.

    But you probably don't want to do it as custom html tag. You probably want to do it on click. in that case, you have {{Clicked Element}} variable in GTM, of which you can also get .innerText, or get its .parentElement and further navigate DOM from the clicked element as you wish and get whatever you need.