JQuery 后台点击功能 - GTM

JQuery function on background click - GTM

我有一个内置于 GTM 中的自定义标签(Google 标签管理器)。我希望标签在用户关闭特定 bootstrap 模式时进行跟踪。为此,我构建了一个自定义函数:

$(function () {
      $('#popup').click(function() {
      var label = $("#popup .container h4").text();
      dataLayer.push({
         'event': 'GAEvent', 
         'eventCat': 'Event Engagement', 
         'eventAction': label + " Modal - Close", 
         'eventLabel': label,
         'gaNonInt': false
      });
   });
});

模态的HTML结构如下:

<div id="popup"> <!--The modal background/overlay is set here-->
   <div id="popup-block">Content nested here</div>
</div>

当用户点击背景层(#popup 的背景)时,模式关闭。但是,当我使用自定义函数定位#popup 时,单击嵌套在#popup div 中的任何元素时都会触发标记。有没有办法只针对 #popup div 元素的背景?提前致谢。

试试这个:

$(function () {
      $('#popup').click(function(e) {
      if(e.target != this) return; // probably a child was clicked.
      var label = $("#popup .container h4").text();
      dataLayer.push({
         'event': 'GAEvent', 
         'eventCat': 'Event Engagement', 
         'eventAction': label + " Modal - Close", 
         'eventLabel': label,
         'gaNonInt': false
      });
   });
});