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事件跟踪的主要内容,如果未能解决你的问题,请参考以下文章