Search code examples
css-selectorsgoogle-tag-manager

Tag Manager Variable Button Text based on Content


On every page (each page is a course) there are 3-4 buttons with:

<a href="..."><div class="btn btn--small"> some text </div></a>

and one

<a href="...n-credits/"><div class="btn btn--small"> n credits</div></a>

I want to create a variable of "n credits".

I tried the following: dom element, css selector a[href*="credits"] but the variable returns null.


Solution

  • Just make a CJS variable and in it:

    function(){
      return document.querySelector("a[href*='credits']>div.btn--small").innerText;
    }

    That returns the inner text of the first div that has the btn--small class and lives in an a that has credits in it.