fix(ga4): stabilize checkout funnel events

This commit is contained in:
2026-05-11 15:54:42 +02:00
parent 80a3cb3814
commit a9fce98b01
8 changed files with 597 additions and 106 deletions

View File

@@ -230,7 +230,7 @@ class PdGoogleAnalytycs4ProAjaxModuleFrontController extends ModuleFrontControll
$cp['content_ids'] = $module->getProductIdStringByType($cp);
$cp['content_coupon'] = ($cart_rules & is_array($cart_rules)) ? $cart_rules['name'].' - '.$cart_rules['code'] : '';
$cp['content_coupon'] = (is_array($cart_rules) && sizeof($cart_rules)) ? $cart_rules['name'].' - '.$cart_rules['code'] : '';
$discount = 0;
$discount = $cp['price_without_reduction'] - $cp['price_wt'];
if ($discount_cart > 0) {
@@ -261,7 +261,7 @@ class PdGoogleAnalytycs4ProAjaxModuleFrontController extends ModuleFrontControll
$carrier_name = '';
if ($id_carrier = Tools::getValue('id_carrier')) {
$carriers = $module->getCarriersArray();
$carrier_name = $carriers[$id_carrier];
$carrier_name = isset($carriers[$id_carrier]) ? $carriers[$id_carrier] : '';
}
$this->context->smarty->assign(array(
@@ -302,7 +302,7 @@ class PdGoogleAnalytycs4ProAjaxModuleFrontController extends ModuleFrontControll
$cp['content_category4'] = isset($content_category[3]) ? addslashes($content_category[3]) : '';
$cp['content_category5'] = isset($content_category[4]) ? addslashes($content_category[4]) : '';
$cp['content_ids'] = $module->getProductIdStringByType($cp);
$cp['content_coupon'] = ($cart_rules & is_array($cart_rules)) ? $cart_rules['name'].' - '.$cart_rules['code'] : '';
$cp['content_coupon'] = (is_array($cart_rules) && sizeof($cart_rules)) ? $cart_rules['name'].' - '.$cart_rules['code'] : '';
$discount = 0;
$discount = $cp['price_without_reduction'] - $cp['price_wt'];
if ($discount_cart > 0) {

View File

@@ -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;
}
});
});

View File

@@ -78,18 +78,7 @@
value: {$content_value|escape:'htmlall':'UTF-8'}
});
{else if ($tagType === 'order' || $tagType === 'order-opc')}
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': {$content_value|escape:'htmlall':'UTF-8'},
'currency': 'PLN'
});
console.log( 'Fired up event GADS begin_checkout conversion lulandia.pl' );
}
gtag('event', 'begin_checkout', {
window.pdGa4BeginCheckoutPayload = {
coupon: '{$content_coupon|escape:'htmlall':'UTF-8'}',
currency: '{$currency|escape:'htmlall':'UTF-8'}',
items: [
@@ -117,7 +106,61 @@
{/foreach}
],
value: {$content_value|escape:'htmlall':'UTF-8'}
});
};
{else if ($tagType === 'order' || $tagType === 'order-opc')}
(function() {
var payload = {
coupon: '{$content_coupon|escape:'htmlall':'UTF-8'}',
currency: '{$currency|escape:'htmlall':'UTF-8'}',
items: [
{foreach from=$content_products item=product name=products}
{
item_id: '{$product.content_ids|escape:'htmlall':'UTF-8'}',
item_name: '{$product.name|escape:'htmlall':'UTF-8'}',
coupon: '{$product.content_coupon|escape:'htmlall':'UTF-8'}',
discount: {$product.discount|escape:'htmlall':'UTF-8'},
index: {$smarty.foreach.products.index},
item_list_name: 'Cart products',
item_list_id: 1,
affiliation: '{$http_referer|escape:'htmlall':'UTF-8'}',
item_brand: '{$product.manufacturer_name|escape:'htmlall':'UTF-8'}',
item_category: '{$product.content_category|escape:'htmlall':'UTF-8'}',
{if !empty($product.content_category2)}item_category2: '{$product.content_category2|escape:'htmlall':'UTF-8'}',{/if}
{if !empty($product.content_category3)}item_category3: '{$product.content_category3|escape:'htmlall':'UTF-8'}',{/if}
{if !empty($product.content_category4)}item_category4: '{$product.content_category4|escape:'htmlall':'UTF-8'}',{/if}
{if !empty($product.content_category5)}item_category5: '{$product.content_category5|escape:'htmlall':'UTF-8'}',{/if}
item_variant: '{if isset($product.variant)}{$product.variant}{/if}',
price: {$product.price|escape:'htmlall':'UTF-8'},
currency: '{$currency|escape:'htmlall':'UTF-8'}',
quantity: '{$product.cart_quantity|escape:'htmlall':'UTF-8'}'
},
{/foreach}
],
value: {$content_value|escape:'htmlall':'UTF-8'}
};
var eventKey = 'pdga4_begin_checkout_v2_' + payload.items.map(function(item) {
return [item.item_id, item.quantity, item.price].join(':');
}).join('|') + ':' + payload.value;
try {
if (sessionStorage.getItem(eventKey) === '1') {
return;
}
sessionStorage.setItem(eventKey, '1');
} catch (e) {}
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);
})();
{else if ($tagType === 'category')}
console.log('Fired up event GA4: view_item_list > Category products list page');
@@ -311,4 +354,4 @@
</script>
<!-- PD Google Analytycs 4 Pro - EVENTS CODE FOOTER -->
{/if}
{/if}