title: GTM Custom Javascript Undefined Variable
body: <p>I am working on a site with a mix of buttons and images/text labels wrapped in anchor tags, the anchor tags contain the href which I need to extract to Google Analytics via Google Tag Manager.</p>
  <p>One Example</p>
  <pre><code>&lt;a style=&quot;touch-action: manipulation; -ms-touch-action: manipulation; cursor: pointer;&quot; href=&quot;/company_details/documents&quot; id=&quot;ember11&quot; class=&quot;ember-view&quot;&gt;
          &lt;i aria-hidden=&quot;true&quot; class=&quot;material-icons message-icon&quot;&gt;
            email
          &lt;/i&gt;
          &lt;span class=&quot;nav-masterhead-label&quot;&gt;
            Letters
          &lt;/span&gt;
        &lt;/a&gt;
  </code></pre>
  <p>I have created a custom JavaScript variable but it results in undefined when the tag is triggered.  The click Element can also be a button on the site so I created an if statement to allow for that, without the if statement the variable is undefined as well.</p>
  <pre><code>function (){
    var $clickElement = $({{Click Element}});
    if (typeof $clickElement.closest('a').href !== 'undefined'){
      return $clickElement.closest('a').href;
    } else {
      return $clickElement.href;
    }
  }
  </code></pre>