2

I have two groups of links all with the same class names, the only difference is the text in the . I need to get the text for the clicked link and pass it to GA through GTM.

<div class="item-set">
  <header>Section Title One</header>
  <section class="products">
    <div class="list">
      <a href="/Product/60216935"><img src="/ProductImages1.jpg"></a>
    </div>
    <div class="list">
      <a href="/Product/6021693x"><img src="/ProductImages2.jpg"></a>
    </div>
    <div class="list">
      <a href="/Product/6021693y"><img src="/ProductImages3.jpg"></a>
    </div>
  </section>
</div>
<div class="item-set">
  <header>Section Title Two</header>
  <section class="products">
    <div class="list">
      <a href="/Product/60216935"><img src="/ProductImages1.jpg"></a>
    </div>
    <div class="list">
      <a href="/Product/6021693x"><img src="/ProductImages2.jpg"></a>
    </div>
    <div class="list">
      <a href="/Product/6021693y"><img src="/ProductImages3.jpg"></a>
    </div>
  </section>
</div>

I have created a custom javascript variable

function() {
  $('section.products div.list').click(function() {
    return $(this).closest('.item-set').find('header').text();
  });
}

But the bleep thing isn't working as I expect (or at all). It returns "undefined".

Any assistance is greatly appreciated.

2 Answers 2

7

You shouldn't bind to click event in JS variable. You should use JS variables in GTM only for receiving values

The correct way to achieve your goal is:

1) Enable built-in variable Click Element (if you already have it, you can skip this step) enter image description here

2) Create trigger, which will fire when you clicking on your links enter image description here CSS selector on the screenshot is .item-set .list a

3) Create JS variable enter image description here Code is: function() { return $({{Click Element}}.closest('.item-set')).find('header').text(); } 3) Create a tag, which will send data to GA enter image description here Here you can use your variable form step 3 {{Click List Header}}

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

2 Comments

Thanks heaps. Worked like a charm! I was pretty close to the answer. Just a little off.
The jquery syntax used here doesn't work with the most recent version of GTM as of July 2019. The correct syntax would be function() { return {{Click Element}}.closest('.item-set').find('header').text(); }
0

Maybe you are not capturing/storing the return $(this).closest('.item-set').find('header').text(); from the function?

When you for example immediately invoke your function and click a div, the text of the <header> will be logged to the console.

(function() {
    $('section.products div.list').click(function(e) {
        e.preventDefault(); // to prevent the default action of the click
        console.log($(this).closest('.item-set').find('header').text());
        return $(this).closest('.item-set').find('header').text();
    });
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="item-set">
    <header>Section Title One</header>
    <section class="products">
        <div class="list">
            <a href="/Product/60216935"><img src="/ProductImages1.jpg"></a>
        </div>
        <div class="list">
            <a href="/Product/6021693x"><img src="/ProductImages2.jpg"></a>
        </div>
        <div class="list">
            <a href="/Product/6021693y"><img src="/ProductImages3.jpg"></a>
        </div>
    </section>
</div>
<div class="item-set">
    <header>Section Title Two</header>
    <section class="products">
        <div class="list">
            <a href="/Product/60216935"><img src="/ProductImages1.jpg"></a>
        </div>
        <div class="list">
            <a href="/Product/6021693x"><img src="/ProductImages2.jpg"></a>
        </div>
        <div class="list">
            <a href="/Product/6021693y"><img src="/ProductImages3.jpg"></a>
        </div>
    </section>
</div>

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.