javascript GTM事件跟踪

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript GTM事件跟踪相关的知识,希望对你有一定的参考价值。

<script>
(function () {
  var gtmCreditRating = document.querySelectorAll('input[name="credit_rating"]'),
    gtmMilitary = document.querySelectorAll('input[name="opt_served_military"]'),
    gtmMilitary2 = document.querySelectorAll('input[name="military-selection"]'),
    gtmEstPropValue = document.querySelector('[name="est_property_value"]'),
    gtmEstPurPrice = document.querySelector('[name="est_purchase_price"]'),
    gtmPropType = document.querySelectorAll('input[name="property_type"]'),
    gtmMortgageAmount = document.querySelector('[name="mortgage_amount"]'),
    crChecked,
    milChecked,
    ptChecked;

  function runEventTracking(inputNameSelector, inputName, category, action) {
    for (var i = 0; i < inputNameSelector.length; i++) {
      inputNameSelector[i].addEventListener('click', function (e) {
        action = document.querySelector('[name="' + inputName + '"]:checked').value;
        ga.getAll()[0].send('event', category, action, inputName, 0);
        console.log(action);
      });
    }
  }

  function highPriceTracking(inputName, category) {
    var gtmEst = document.querySelector('[name="' + inputName + '"]').value;
    if (parseInt(gtmEst) > 125000) {
      ga.getAll()[0].send('event', category, 'yes', inputName, 0);
    } else {
      ga.getAll()[0].send('event', category, 'no', inputName, 0);
    }
  }

  if (gtmEstPropValue) {
    gtmEstPropValue.onchange = function () {
      highPriceTracking('est_property_value', 'high_price');
    };
  }
  if (gtmEstPurPrice) {
    gtmEstPurPrice.onchange = function () {
      highPriceTracking('est_purchase_price', 'high_price');
    };
  }
  if (gtmMortgageAmount) {
    gtmMortgageAmount.onchange = function () {
      highPriceTracking('mortgage_amount', 'high_price_mortgage');
    };
  }
  
  $('.btn-primary').on('click', function () {
    if ($('input[name="est_property_value"]').parent().is(':visible')) {
      highPriceTracking('est_property_value', 'high_price');
    }
    if ($('input[name="est_purchase_price"]').parent().is(':visible')) {
      highPriceTracking('est_purchase_price', 'high_price');
    }
    if ($('input[name="mortgage_amount"]').parent().is(':visible')) {
      highPriceTracking('mortgage_amount', 'high_price_mortgage');
    }

  })

  runEventTracking(gtmCreditRating, 'credit_rating', 'credit', crChecked);
  runEventTracking(gtmMilitary, 'opt_served_military', 'military', milChecked);
  runEventTracking(gtmMilitary2, 'military-selection', 'military', milChecked);
  runEventTracking(gtmPropType, 'property_type', 'property', ptChecked);
})();
</script>

以上是关于javascript GTM事件跟踪的主要内容,如果未能解决你的问题,请参考以下文章

javascript 使用GTM的客户端Javascript错误跟踪(谷歌分析)

javascript Google Analytics的设备方向检测。 Google跟踪代码管理器(GTM)的修改版本。最初来自https://www.themarketingtech

javascript 使用jQuery的GTM分析框架(事件委托,自定义事件)

谁有Prestashop集成GTM电子商务分析经验

如何在 GTM 中使用 javascript 替换 dataLayer 中的参数名称

React SPA - GTM Analytics React-Helmet 上一页标题