Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cawa-93/108a2e632c1ab8258e5aba3c6159c350 to your computer and use it in GitHub Desktop.
Save cawa-93/108a2e632c1ab8258e5aba3c6159c350 to your computer and use it in GitHub Desktop.
List of javascript variables for Google Tag Manager. Each variable is an analogue of the corresponding built-in variable from the "Clicks" category. However, it applies only to buttons, regardless of the level of nesting.

Click Button variables for Google Tag Manager

List of javascript variables for Google Tag Manager. Each variable is an analogue of the corresponding built-in variable from the "Clicks" category. However, it applies only to buttons, regardless of the level of nesting.

Using these variables, you can work with buttons as easily as you can with links.

Examples

Normally, it completely repeats the behavior of built-in variables

<button> <-- {{Click Element}} and {{Click Button Element}}
  Some text
</button>

But in the case of nested variables, the result always points to the nearest button, regardless of the nesting level of the {{Click Element}}.

<button> <-- {{Click Button Element}}
  <span>Some text</span>  <-- {{Click Element}}
</button>
<button> <-- {{Click Button Element}}
  <span>
    <i class="icon"></i>   <-- {{Click Element}}
    <span>Some text</span>
  </span>
</button>

How Use

  1. Enable build-in {{Click Element}} variable.
  2. Create Custom JavaScript variable.
  3. Copy one of the codes below according to your needs and paste it into the body of the Custom JavaScript variable.
function () {
return {{Click Element}}.closest('button').className
}
function () {
return {{Click Element}}.closest('button') || undefined
}
function () {
return {{Click Element}}.closest('button').id
}
function () {
return {{Click Element}}.closest('button').name
}
function () {
return {{Click Element}}.closest('button').innerText
}
function () {
return {{Click Element}}.closest('button').type
}
function () {
return {{Click Element}}.closest('button').value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment