# Tag manager

**URL:** <https://community.elfsight.com/t/tag-manager/61660>\
**Category:** General Questions\
**Created:** [July 16, 2024, 10:42pm UTC](https://community.elfsight.com/t/tag-manager/61660 "2024-07-16T22:42:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![user13629](https://sea2.discourse-cdn.com/flex020/user_avatar/community.elfsight.com/user13629/32/31618_2.png) [@user13629](https://community.elfsight.com/u/user13629)\
**Post date:** [July 16, 2024, 10:42pm UTC](https://community.elfsight.com/t/tag-manager/61660/1 "2024-07-16T22:42:10Z")

</div>

Ability to track clicks with tag manager

---

<div class="post-metadata">

**Author:** ![Max](https://sea2.discourse-cdn.com/flex020/user_avatar/community.elfsight.com/max/32/710_2.png) [@Max](https://community.elfsight.com/u/Max)\
**Post date:** [July 17, 2024, 11:26am UTC](https://community.elfsight.com/t/tag-manager/61660/3 "2024-07-17T11:26:04Z")

</div>

Hi there @user13629 👋

Great news - we’ve got a solution! To track clicks on WhatsApp Chat widget via GTM, please add this code to the Custom JS field on the Appearance tab of your widget’s settings:

```auto

function sendAnalyticsEvent(config) {
  if (typeof ga !== "undefined") {
    ga('send', 'event', {
      eventAction: config.action,
      eventCategory: config.category,
      eventLabel: config.label
    });
  }
  if (typeof gtag !== "undefined") {
    gtag('event', config.action, {
      'event_category': config.category,
      'event_label': config.label
    });
  }
}
function rafAsync() {
  return new Promise(resolve => {
    requestAnimationFrame(resolve);
  });
}
function checkElement(selector) {
  if (document.querySelector(selector) === null) {
    return rafAsync().then(() => checkElement(selector));
  } else {
    return Promise.resolve(true);
  }
}

checkElement('[class*="Bubble__BubbleComponent"]')
  .then(() => {
    const addClickEvent = (el, label) => el.addEventListener('click', () => {
        sendAnalyticsEvent({
          action: 'click',
          category: 'Elfsight Chat Widget',
          label: label
        });
      });
    const clicker = (el, label, all = false) => {
      const addClickWithLabel = (el) => addClickEvent(el, label);
      
      if (all) {
        [...document.querySelectorAll(el)].forEach(addClickWithLabel);
      } else {
        addClickWithLabel(document.querySelector(el));
      }
    };

    clicker('[class*="Bubble__BubbleComponent"]', 'Open/Close Chat Widget');
    clicker('[class*="DefaultButton__DefaultButtonComponent-sc"]', 'Click Start Chat Button', true);
  });

```

  

Check it out and let me know how it worked 😉
