Google Tag Manager - Get value of an changing Element

Viewed 130

We are using GTM for a website to get Conversions and to track clicks on a map (google map). On this map are different Markers and if you click such a marker, on the right side a box (concerning to this marker) opens and gives you more infos. Now with GTM we want to get the value of the opened Container to realise which marker is clicked. For this we need to enter Custom JS as a variable in GTM. (I'm sorry, but we are not allowed to share the website here)

But on the website the construced of the map is like this:

<div class="map-container">
  <div class="left-containers">
   <div class="JS-retailer-card">
      <div class="RetailerResultsCard__name">Name 1</div>
   </div>
   <div class="JS-retailer-card">
      <div class="RetailerResultsCard__name">Name 2</div>
   </div>
   <div class="JS-retailer-card">
      <div class="RetailerResultsCard__name">Name 3</div>
   </div>
   ...
  </div>
</div>

Everytime I click on one of the markers, one of this JS-retailer-card gets the extra class "openend" and shows me all infos. So what I need is the name which is written there as simple text. Unfortunally the name or any other data is written at the marker I click.

We do this at the moment with this JS - which runs, if I click an Marker (The Click is realised by GTM):

function() {
  var a = $('.JS-retailer-card.opened .RetailerResultsCard__name');
  var text = $(a).text();
  console.log(text);
  return text;
}

We enter this code in Google Tag Manager. It works fine - but only on the second click and then with the wrong value. Because at the first click we get an "undefined" value back. And at the second click we get back the value from the first click ... and so on. I think (or know), that this is happing, because there is no element with "openend" at the beginning. This class is added only after my first click.

What we already tried, is to add a setTimeout, but then always the return is "undefined". Do you have an idea how to do this with GTM?

0 Answers
Related