[英]Google Analytics - OnClick Event Tracking with Javascript & CSS Class
Due to a theme limitation in WordPress where I can't inline the onclick event, I'm passing attributes into the link and attempting to pick this backup in Javascript to send the Event tracking to Google Analytics. 由于WordPress中的主题限制,我无法内联onclick事件,因此我将属性传递到链接中,并尝试在Javascript中选择此备份以将事件跟踪发送到Google Analytics(分析)。 It's not firing and I can't figure out why. 这不是开枪,我不知道为什么。
The script was posted on another website and I'm working to adapt it for my use. 该脚本已发布在另一个网站上,我正在努力对其进行调整以供使用。 I've tried escaping the variables /' and \\x27 on the ga(send line but nothing is working so far. I've also included jQuery as a include to see if that was the issue as well. 我尝试在ga(send行上转义变量/'和\\ x27,但到目前为止没有任何操作。我还包括jQuery作为包含项,以查看是否也是问题所在。
<a href="#" class="ga-track" data-category='Schedule Appointment' data-action='click' data-label='Website'>Link</a>
<script type="text/javascript">
var trackEl = document.querySelectorAll('.ga-track');
Array.prototype.forEach.call(trackEl, function(el) {
el.onclick = function() {
var elCategory = this.getAttribute('data-category');
var elAction = this.getAttribute('data-action');
var elLabel = this.getAttribute('data-label');
ga('send', 'event', elCategory, elAction, elLabel);
};
});
</script>
No errors are being triggered. 没有错误被触发。 My hope is that this will pass an Event into Google Analytics for conversion tracking. 我希望这会将事件传递到Google Analytics(分析)中以进行转化跟踪。
I was able to get it working with the following code: 我能够通过以下代码使用它:
<a href="#" class="ga-track" data-category='Schedule Appointment' data-action='click' data-label='Website'>Link</a>
<script>
jQuery(document).ready(function() {
jQuery(‘.ga-track’).on(‘click’, function(e) {
e.preventDefault();
var elCategory = this.getAttribute(‘data-category’);
var elAction = this.getAttribute(‘data-action’);
var elLabel = this.getAttribute(‘data-label’);
gtag(‘event’, elAction, { ‘event_category’: elCategory, ‘event_label’: elLabel });
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.