Google Analytics 在 Ajax 成功发送事件无效
Google Analytics Send Event on Ajax Success Not Working
我有一个 Process Transaction Knockout 方法,该方法 returns 一个交易状态,根据该状态,我希望发送 analytics.js 事件。我使用了 analytics-debug.js 并且在控制台中显示 "Send finished" 但该事件从未出现在 Google Analytics 中。
如果发送事件在 ajax 请求之前或之后有效,但如果它在成功或完成回调中则无效。
$.ajax({
url: '/ProcessTransaction',
type: 'post',
data: 'data',
contentType: 'application/json',
success: function (result) {
if (result.StatusCode == 1 || result.StatusCode == 4) {
var subtotal = 7.95;
var enrollmentFee = 25;
var total = subtotal + enrollmentFee;
ga('ec:addProduct', {
'id': 'P12345',
'name': 'Test Product 1',
'category': 'Products',
'brand': 'Brand',
'price': subtotal,
'quantity': 1
});
ga('ec:setAction', 'purchase', {
'id': 'T12345',
'affiliation': 'TestSite',
'revenue': total,
'tax': enrollmentFee,
});
ga('send', 'event', 'Review', 'transaction', 'approved', total);
$('#submitpaymentbtn').data('loading-text', 'Approved!').button('loading');
window.location.href = '@Url.Action("Confirmation", new { ParticipantId = Participant.ParticipantId })';
}
else {
$('#modal-error').modal();
}
}
});
发送事件不允许事件值使用小数。 Math.round(总计)解决了这个问题。即使分析调试显示事件已发送,它也永远不会被接受或记录,因为该值必须是整数。
ga('send', 'event', 'Review', 'transaction', 'approved', Math.round(total));
我有一个 Process Transaction Knockout 方法,该方法 returns 一个交易状态,根据该状态,我希望发送 analytics.js 事件。我使用了 analytics-debug.js 并且在控制台中显示 "Send finished" 但该事件从未出现在 Google Analytics 中。
如果发送事件在 ajax 请求之前或之后有效,但如果它在成功或完成回调中则无效。
$.ajax({
url: '/ProcessTransaction',
type: 'post',
data: 'data',
contentType: 'application/json',
success: function (result) {
if (result.StatusCode == 1 || result.StatusCode == 4) {
var subtotal = 7.95;
var enrollmentFee = 25;
var total = subtotal + enrollmentFee;
ga('ec:addProduct', {
'id': 'P12345',
'name': 'Test Product 1',
'category': 'Products',
'brand': 'Brand',
'price': subtotal,
'quantity': 1
});
ga('ec:setAction', 'purchase', {
'id': 'T12345',
'affiliation': 'TestSite',
'revenue': total,
'tax': enrollmentFee,
});
ga('send', 'event', 'Review', 'transaction', 'approved', total);
$('#submitpaymentbtn').data('loading-text', 'Approved!').button('loading');
window.location.href = '@Url.Action("Confirmation", new { ParticipantId = Participant.ParticipantId })';
}
else {
$('#modal-error').modal();
}
}
});
发送事件不允许事件值使用小数。 Math.round(总计)解决了这个问题。即使分析调试显示事件已发送,它也永远不会被接受或记录,因为该值必须是整数。
ga('send', 'event', 'Review', 'transaction', 'approved', Math.round(total));