fix(ga4): stabilize checkout funnel events
This commit is contained in:
@@ -19,98 +19,187 @@
|
||||
|
||||
$(document).ready(function() {
|
||||
|
||||
$("body#module-thecheckout-order").on("click", "input[type=radio][name^=delivery_option]:checked", function() {
|
||||
let id_carrier = parseInt(this.value);
|
||||
function pdGa4StorageKey(eventName, eventKey) {
|
||||
return 'pdga4_' + eventName + '_' + eventKey;
|
||||
}
|
||||
|
||||
function pdGa4WasSent(eventName, eventKey) {
|
||||
try {
|
||||
return sessionStorage.getItem(pdGa4StorageKey(eventName, eventKey)) === '1';
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function pdGa4MarkSent(eventName, eventKey) {
|
||||
try {
|
||||
sessionStorage.setItem(pdGa4StorageKey(eventName, eventKey), '1');
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
function pdGa4PayloadSignature(payload) {
|
||||
if (!payload || !payload.items) {
|
||||
return 'empty';
|
||||
}
|
||||
|
||||
return $.map(payload.items, function(item) {
|
||||
return [item.item_id, item.quantity, item.price].join(':');
|
||||
}).join('|') + ':' + payload.value;
|
||||
}
|
||||
|
||||
function pdGa4CurrentCartSignature() {
|
||||
if (typeof prestashop === 'undefined' || !prestashop.cart || !prestashop.cart.products) {
|
||||
return 'cart';
|
||||
}
|
||||
|
||||
return $.map(prestashop.cart.products, function(product) {
|
||||
return [product.id_product, product.id_product_attribute, product.quantity].join(':');
|
||||
}).join('|') || 'cart';
|
||||
}
|
||||
|
||||
function pdGa4InsertEventHtml(data) {
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
let eventContainer = $('#hook-display-before-carrier');
|
||||
if (!eventContainer.length) {
|
||||
eventContainer = $('#pdga4-checkout-event-container');
|
||||
}
|
||||
if (!eventContainer.length) {
|
||||
eventContainer = $('<div id="pdga4-checkout-event-container"></div>');
|
||||
$('body').append(eventContainer);
|
||||
}
|
||||
|
||||
eventContainer.empty();
|
||||
eventContainer[0].innerHTML = data;
|
||||
eventContainer.find('script').each(function() {
|
||||
if (this.src) {
|
||||
$.getScript(this.src);
|
||||
} else {
|
||||
$.globalEval(this.text || this.textContent || this.innerHTML || '');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function pdGa4GetCarrierId(deliveryOption) {
|
||||
if (typeof deliveryOption === 'undefined' || deliveryOption === null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return parseInt(deliveryOption, 10);
|
||||
}
|
||||
|
||||
function pdGa4SendDeliveryInfo(idCarrier) {
|
||||
idCarrier = pdGa4GetCarrierId(idCarrier);
|
||||
if (!idCarrier) {
|
||||
return;
|
||||
}
|
||||
|
||||
let eventKey = pdGa4CurrentCartSignature() + '_' + idCarrier;
|
||||
if (pdGa4WasSent('add_shipping_info', eventKey)) {
|
||||
return;
|
||||
}
|
||||
pdGa4MarkSent('add_shipping_info', eventKey);
|
||||
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: pdgoogleanalytycs4pro_ajax_link,
|
||||
data: {'action': 'addDeliveryInfo', 'id_carrier': id_carrier, 'secure_key': pdgoogleanalytycs4pro_secure_key, ajax: true},
|
||||
data: {'action': 'addDeliveryInfo', 'id_carrier': idCarrier, 'secure_key': pdgoogleanalytycs4pro_secure_key, ajax: true},
|
||||
dataType: "json",
|
||||
success: function(data) {
|
||||
if (data) {
|
||||
$('#hook-display-before-carrier').html(data);
|
||||
}
|
||||
pdGa4InsertEventHtml(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function pdGa4SendPaymentInfo(paymentModule) {
|
||||
if (!paymentModule) {
|
||||
return;
|
||||
}
|
||||
|
||||
let eventKey = pdGa4CurrentCartSignature() + '_' + paymentModule;
|
||||
if (pdGa4WasSent('add_payment_info', eventKey)) {
|
||||
return;
|
||||
}
|
||||
pdGa4MarkSent('add_payment_info', eventKey);
|
||||
|
||||
$("body#module-thecheckout-order").on("click", "input[name=payment-option]:checked", function() {
|
||||
let payment_module = $(this).data('module-name');
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: pdgoogleanalytycs4pro_ajax_link,
|
||||
data: {'action': 'addPaymentInfo', 'payment_module': payment_module, 'secure_key': pdgoogleanalytycs4pro_secure_key, ajax: true},
|
||||
data: {'action': 'addPaymentInfo', 'payment_module': paymentModule, 'secure_key': pdgoogleanalytycs4pro_secure_key, ajax: true},
|
||||
dataType: "json",
|
||||
success: function(data) {
|
||||
if (data) {
|
||||
$('#hook-display-before-carrier').html(data);
|
||||
}
|
||||
pdGa4InsertEventHtml(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function pdGa4SendSelectedDeliveryInfo() {
|
||||
let selectedDeliveryOption = $('.delivery-options input[type="radio"]:checked, input[type=radio][name^=delivery_option]:checked').first();
|
||||
if (!selectedDeliveryOption.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
pdGa4SendDeliveryInfo(selectedDeliveryOption.val());
|
||||
}
|
||||
|
||||
function pdGa4SendBeginCheckout() {
|
||||
if (typeof window.pdGa4BeginCheckoutPayload === 'undefined' || typeof gtag === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
let payload = window.pdGa4BeginCheckoutPayload,
|
||||
eventKey = pdGa4PayloadSignature(payload);
|
||||
|
||||
if (pdGa4WasSent('begin_checkout', eventKey)) {
|
||||
return;
|
||||
}
|
||||
pdGa4MarkSent('begin_checkout', eventKey);
|
||||
|
||||
console.log('Fired up event GA4: begin_checkout');
|
||||
if (window.location.hostname === "lulandia.pl") {
|
||||
gtag('event', 'conversion', {
|
||||
'send_to': 'AW-11243281264/Gs-KCNqSsesYEPC2m_Ep',
|
||||
'value': payload.value,
|
||||
'currency': 'PLN'
|
||||
});
|
||||
console.log('Fired up event GADS begin_checkout conversion lulandia.pl');
|
||||
}
|
||||
gtag('event', 'begin_checkout', payload);
|
||||
}
|
||||
|
||||
$('body#cart').on('click', '.cart-detailed-actions a[href], a[href*="order"]', function() {
|
||||
window.pdGa4CheckoutIntent = true;
|
||||
});
|
||||
|
||||
$("body#checkout").on("click", '.delivery-options input[type="radio"]:checked', function() {
|
||||
let id_carrier = parseInt(this.value);
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: pdgoogleanalytycs4pro_ajax_link,
|
||||
data: {'action': 'addDeliveryInfo', 'id_carrier': id_carrier, 'secure_key': pdgoogleanalytycs4pro_secure_key, ajax: true},
|
||||
dataType: "json",
|
||||
success: function(data) {
|
||||
if (data) {
|
||||
$('#hook-display-before-carrier').html(data);
|
||||
}
|
||||
$("body#module-thecheckout-order, body#checkout, body.module-steasycheckout-default").on("change click", "input[type=radio][name^=delivery_option]:checked, .delivery-options input[type='radio']:checked", function() {
|
||||
pdGa4SendDeliveryInfo($(this).val());
|
||||
});
|
||||
|
||||
$("body#checkout").on("submit", "#checkout-addresses-step form, form:has(button[name='confirm-addresses'])", function() {
|
||||
window.setTimeout(pdGa4SendSelectedDeliveryInfo, 1200);
|
||||
});
|
||||
|
||||
$("body#checkout").on("click", "button[name='confirmDeliveryOption'], #checkout-delivery-step button.continue", function() {
|
||||
window.setTimeout(pdGa4SendSelectedDeliveryInfo, 50);
|
||||
});
|
||||
|
||||
$("body#module-thecheckout-order, body#checkout, body.module-steasycheckout-default").on("change click", 'input[name="payment-option"]:checked', function() {
|
||||
let paymentModule = $(this).data('module-name');
|
||||
pdGa4SendPaymentInfo(paymentModule);
|
||||
});
|
||||
|
||||
if (typeof(prestashop) !== 'undefined') {
|
||||
prestashop.on('updatedDeliveryForm', function() {
|
||||
window.setTimeout(pdGa4SendSelectedDeliveryInfo, 200);
|
||||
});
|
||||
prestashop.on('changedCheckoutStep', function(params) {
|
||||
if (params && params.event && params.event.currentTarget && params.event.currentTarget.id === 'checkout-delivery-step') {
|
||||
window.setTimeout(pdGa4SendSelectedDeliveryInfo, 200);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$("body#checkout").on("click", 'input[name="payment-option"]:checked', function() {
|
||||
let payment_module = $(this).data('module-name');
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: pdgoogleanalytycs4pro_ajax_link,
|
||||
data: {'action': 'addPaymentInfo', 'payment_module': payment_module, 'secure_key': pdgoogleanalytycs4pro_secure_key, ajax: true},
|
||||
dataType: "json",
|
||||
success: function(data) {
|
||||
if (data) {
|
||||
$('#hook-display-before-carrier').html(data);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// opc steasycheckout
|
||||
$("body.module-steasycheckout-default").on("click", 'input[type=radio][name^=delivery_option]:checked', function() {
|
||||
let id_carrier = parseInt(this.value);
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: pdgoogleanalytycs4pro_ajax_link,
|
||||
data: {'action': 'addDeliveryInfo', 'id_carrier': id_carrier, 'secure_key': pdgoogleanalytycs4pro_secure_key, ajax: true},
|
||||
dataType: "json",
|
||||
success: function(data) {
|
||||
if (data) {
|
||||
$('#hook-display-before-carrier').html(data);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// opc steasycheckout
|
||||
$("body.module-steasycheckout-default").on("click", 'input[name="payment-option"]:checked', function() {
|
||||
let payment_module = $(this).data('module-name');
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: pdgoogleanalytycs4pro_ajax_link,
|
||||
data: {'action': 'addPaymentInfo', 'payment_module': payment_module, 'secure_key': pdgoogleanalytycs4pro_secure_key, ajax: true},
|
||||
dataType: "json",
|
||||
success: function(data) {
|
||||
if (data) {
|
||||
$('#hook-display-before-carrier').html(data);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
if (typeof(prestashop) !== 'undefined') {
|
||||
@@ -609,4 +698,4 @@ $(document).ready(function() {
|
||||
return query;
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user