parent
56c91ed705
commit
a482be1ebd
@ -1,135 +1,332 @@ |
|||||||
{# header_scripts_pixel.twig #} |
{# templates/head-pixel-functions.twig #} |
||||||
|
|
||||||
{# Проверка переменных перед рендерингом #} |
{# PageView - просмотр любой страницы #} |
||||||
{% if pixel_event_type == 'ViewContent' and product %} |
{% if pixel_event_type == 'PageView' %} |
||||||
<script> |
<script> |
||||||
document.addEventListener('DOMContentLoaded', function() { |
document.addEventListener('DOMContentLoaded', function() { |
||||||
fbq('track', 'ViewContent', { |
// Facebook Pixel - PageView |
||||||
content_ids: ['{{ product.id }}'], |
if (typeof fbq !== 'undefined') { |
||||||
content_type: 'product', |
fbq('track', 'PageView'); |
||||||
value: {{ product.price|number_format(2, '.', '') }}, |
console.log('Facebook PageView sent for page:', '{{ page_data.page_type }}'); |
||||||
currency: '{{ currency }}' |
} |
||||||
|
|
||||||
|
// GA4 - page_view (автоматически отправляется, но можем добавить кастомные параметры) |
||||||
|
if (typeof gtag !== 'undefined') { |
||||||
|
gtag('event', 'page_view', { |
||||||
|
'page_title': '{{ page_data.page_title|escape('js') }}', |
||||||
|
'page_location': '{{ page_data.page_url|escape('js') }}', |
||||||
|
'page_type': '{{ page_data.page_type }}' |
||||||
|
}); |
||||||
|
console.log('GA4 page_view sent for page:', '{{ page_data.page_type }}'); |
||||||
|
} |
||||||
|
|
||||||
|
// DataLayer для GTM |
||||||
|
window.dataLayer = window.dataLayer || []; |
||||||
|
window.dataLayer.push({ |
||||||
|
'event': 'page_view', |
||||||
|
'page_title': '{{ page_data.page_title|escape('js') }}', |
||||||
|
'page_type': '{{ page_data.page_type }}', |
||||||
|
'page_url': '{{ page_data.page_url|escape('js') }}' |
||||||
}); |
}); |
||||||
}); |
}); |
||||||
</script> |
</script> |
||||||
{% endif %} |
{% endif %} |
||||||
|
|
||||||
{% if pixel_event_type == 'InitiateCheckout' %} |
{# ViewContent - просмотр товара #} |
||||||
<script> |
{% if pixel_event_type == 'ViewContent' and product %} |
||||||
document.addEventListener('DOMContentLoaded', function() { |
|
||||||
fbq('track', 'InitiateCheckout'); |
|
||||||
}); |
|
||||||
</script> |
|
||||||
{% endif %} |
|
||||||
|
|
||||||
{% if pixel_event_type == 'AddToCart' %} |
|
||||||
<script> |
<script> |
||||||
document.addEventListener('DOMContentLoaded', function() { |
document.addEventListener('DOMContentLoaded', function() { |
||||||
jQuery(function($) { |
// Facebook Pixel - ViewContent |
||||||
$(document.body).on('added_to_cart', function(event, fragments, cart_hash, $button) { |
if (typeof fbq !== 'undefined') { |
||||||
var productId = $button.data('product_id') || ''; |
fbq('track', 'ViewContent', { |
||||||
var quantity = $button.data('quantity') || 1; |
content_ids: ['{{ product.id }}'], |
||||||
var productName = $button.data('product_sku') || |
content_type: 'product', |
||||||
$button.closest('.product').find('.woocommerce-loop-product__title').text().trim() || 'Unknown'; |
content_name: '{{ product.name|escape('js') }}', |
||||||
var priceElement = $button.closest('.product').find('.price .amount').text().replace(/[^0-9.]/g, '') || '0.00'; |
content_category: '{{ product.category|escape('js') }}', |
||||||
var currency = '{{ currency }}'; |
value: {{ product.price|number_format(2, '.', '') }}, |
||||||
|
currency: '{{ currency }}' |
||||||
fbq('track', 'AddToCart', { |
}); |
||||||
content_ids: [productId], |
console.log('Facebook ViewContent sent for product:', '{{ product.id }}', 'value:', {{ product.price|number_format(2, '.', '') }}); |
||||||
content_type: 'product', |
} |
||||||
value: parseFloat(priceElement) * quantity, |
|
||||||
currency: currency |
// GA4 - view_item |
||||||
}); |
if (typeof gtag !== 'undefined') { |
||||||
|
gtag('event', 'view_item', { |
||||||
window.dataLayer = window.dataLayer || []; |
currency: '{{ currency }}', |
||||||
window.dataLayer.push({ |
value: {{ product.price|number_format(2, '.', '') }}, |
||||||
'event': 'add_to_cart', |
items: [{ |
||||||
'ecommerce': { |
item_id: '{{ product.id }}', |
||||||
'currency': currency, |
item_name: '{{ product.name|escape('js') }}', |
||||||
'value': parseFloat(priceElement) * quantity, |
item_category: '{{ product.category|escape('js') }}', |
||||||
'items': [{ |
price: {{ product.price|number_format(2, '.', '') }}, |
||||||
'item_id': productId, |
quantity: 1 |
||||||
'item_name': productName, |
}] |
||||||
'price': parseFloat(priceElement), |
|
||||||
'quantity': quantity |
|
||||||
}] |
|
||||||
} |
|
||||||
}); |
|
||||||
}); |
}); |
||||||
|
console.log('GA4 view_item sent for product:', '{{ product.id }}'); |
||||||
|
} |
||||||
|
|
||||||
|
// DataLayer для GTM |
||||||
|
window.dataLayer = window.dataLayer || []; |
||||||
|
window.dataLayer.push({ |
||||||
|
'event': 'view_item', |
||||||
|
'ecommerce': { |
||||||
|
'currency': '{{ currency }}', |
||||||
|
'value': {{ product.price|number_format(2, '.', '') }}, |
||||||
|
'items': [{ |
||||||
|
'item_id': '{{ product.id }}', |
||||||
|
'item_name': '{{ product.name|escape('js') }}', |
||||||
|
'item_category': '{{ product.category|escape('js') }}', |
||||||
|
'price': {{ product.price|number_format(2, '.', '') }}, |
||||||
|
'quantity': 1 |
||||||
|
}] |
||||||
|
} |
||||||
}); |
}); |
||||||
|
|
||||||
|
// Яндекс.Метрика |
||||||
|
if (typeof yaCounter96481053 !== 'undefined') { |
||||||
|
yaCounter96481053.reachGoal('view_item', { |
||||||
|
'product_id': '{{ product.id }}', |
||||||
|
'product_name': '{{ product.name|escape('js') }}', |
||||||
|
'price': {{ product.price|number_format(2, '.', '') }}, |
||||||
|
'currency': '{{ currency }}' |
||||||
|
}); |
||||||
|
console.log('Yandex view_item goal sent for product:', '{{ product.id }}'); |
||||||
|
} |
||||||
}); |
}); |
||||||
</script> |
</script> |
||||||
{% endif %} |
{% endif %} |
||||||
|
|
||||||
{% if pixel_event_type == 'AddPaymentInfo' %} |
{# InitiateCheckout - начало оформления заказа #} |
||||||
|
{% if pixel_event_type == 'InitiateCheckout' %} |
||||||
<script> |
<script> |
||||||
document.addEventListener('DOMContentLoaded', function() { |
document.addEventListener('DOMContentLoaded', function() { |
||||||
fbq('track', 'AddPaymentInfo', { |
// Facebook Pixel - InitiateCheckout |
||||||
value: {{ cart_total|number_format(2, '.', '') }}, |
if (typeof fbq !== 'undefined') { |
||||||
currency: '{{ currency }}' |
fbq('track', 'InitiateCheckout', { |
||||||
}); |
{% if cart_total %}value: {{ cart_total|number_format(2, '.', '') }}, |
||||||
|
currency: '{{ currency }}',{% endif %} |
||||||
|
{% if content_ids %}content_ids: [{{ content_ids|map(id => '"' ~ id ~ '"')|join(',') }}],{% endif %} |
||||||
|
content_type: 'product', |
||||||
|
num_items: {{ cart_items|length|default(0) }} |
||||||
|
}); |
||||||
|
console.log('Facebook InitiateCheckout sent, value:', {{ cart_total|default(0)|number_format(2, '.', '') }}); |
||||||
|
} |
||||||
|
|
||||||
|
// GA4 - begin_checkout |
||||||
|
if (typeof gtag !== 'undefined') { |
||||||
|
gtag('event', 'begin_checkout', { |
||||||
|
currency: '{{ currency }}', |
||||||
|
value: {{ cart_total|default(0)|number_format(2, '.', '') }}{% if cart_items %}, |
||||||
|
items: {{ cart_items|json_encode()|raw }}{% endif %} |
||||||
|
}); |
||||||
|
console.log('GA4 begin_checkout sent, value:', {{ cart_total|default(0)|number_format(2, '.', '') }}); |
||||||
|
} |
||||||
|
|
||||||
|
// DataLayer для GTM |
||||||
window.dataLayer = window.dataLayer || []; |
window.dataLayer = window.dataLayer || []; |
||||||
window.dataLayer.push({ |
window.dataLayer.push({ |
||||||
'event': 'add_payment_info', |
'event': 'begin_checkout', |
||||||
'ecommerce': { |
'ecommerce': { |
||||||
'currency': '{{ currency }}', |
'currency': '{{ currency }}', |
||||||
'value': {{ cart_total|number_format(2, '.', '') }} |
'value': {{ cart_total|default(0)|number_format(2, '.', '') }}{% if cart_items %}, |
||||||
|
'items': {{ cart_items|json_encode()|raw }}{% endif %} |
||||||
} |
} |
||||||
}); |
}); |
||||||
|
|
||||||
|
// Яндекс.Метрика |
||||||
|
if (typeof yaCounter96481053 !== 'undefined') { |
||||||
|
yaCounter96481053.reachGoal('begin_checkout', { |
||||||
|
'cart_total': {{ cart_total|default(0)|number_format(2, '.', '') }}, |
||||||
|
'currency': '{{ currency }}', |
||||||
|
'items_count': {{ cart_items|length|default(0) }} |
||||||
|
}); |
||||||
|
console.log('Yandex begin_checkout goal sent'); |
||||||
|
} |
||||||
}); |
}); |
||||||
</script> |
</script> |
||||||
{% endif %} |
{% endif %} |
||||||
|
|
||||||
|
{# Purchase - завершение покупки #} |
||||||
{% if pixel_event_type == 'Purchase' and order %} |
{% if pixel_event_type == 'Purchase' and order %} |
||||||
<script> |
<script> |
||||||
document.addEventListener('DOMContentLoaded', function() { |
document.addEventListener('DOMContentLoaded', function() { |
||||||
fbq('track', 'Purchase', { |
// Facebook Pixel - Purchase |
||||||
value: {{ order.total|number_format(2, '.', '') }}, |
if (typeof fbq !== 'undefined') { |
||||||
currency: '{{ order.currency }}', |
fbq('track', 'Purchase', { |
||||||
content_ids: [{{ order.content_ids|join(',') }}], |
value: {{ order.total|number_format(2, '.', '') }}, |
||||||
content_type: 'product' |
currency: '{{ order.currency }}', |
||||||
}); |
content_ids: [{{ order.content_ids|map(id => '"' ~ id ~ '"')|join(',') }}], |
||||||
|
content_type: 'product', |
||||||
|
num_items: {{ order.items|length }} |
||||||
|
}); |
||||||
|
console.log('Facebook Purchase sent, order:', '{{ order.id }}', 'value:', {{ order.total|number_format(2, '.', '') }}); |
||||||
|
} |
||||||
|
|
||||||
|
// GA4 - purchase |
||||||
|
if (typeof gtag !== 'undefined') { |
||||||
|
gtag('event', 'purchase', { |
||||||
|
transaction_id: '{{ order.id }}', |
||||||
|
value: {{ order.total|number_format(2, '.', '') }}, |
||||||
|
currency: '{{ order.currency }}', |
||||||
|
items: {{ order.items|json_encode()|raw }} |
||||||
|
}); |
||||||
|
console.log('GA4 purchase sent for order:', '{{ order.id }}'); |
||||||
|
} |
||||||
|
|
||||||
|
// DataLayer для GTM |
||||||
window.dataLayer = window.dataLayer || []; |
window.dataLayer = window.dataLayer || []; |
||||||
window.dataLayer.push({ |
window.dataLayer.push({ |
||||||
'event': 'purchase', |
'event': 'purchase', |
||||||
'ecommerce': { |
'ecommerce': { |
||||||
|
'transaction_id': '{{ order.id }}', |
||||||
'currency': '{{ order.currency }}', |
'currency': '{{ order.currency }}', |
||||||
'value': {{ order.total|number_format(2, '.', '') }}, |
'value': {{ order.total|number_format(2, '.', '') }}, |
||||||
'items': {{ order.items|json_encode()|raw }} |
'items': {{ order.items|json_encode()|raw }} |
||||||
} |
} |
||||||
}); |
}); |
||||||
|
|
||||||
|
// Яндекс.Метрика |
||||||
|
if (typeof yaCounter96481053 !== 'undefined') { |
||||||
|
yaCounter96481053.reachGoal('purchase', { |
||||||
|
'order_id': '{{ order.id }}', |
||||||
|
'order_price': {{ order.total|number_format(2, '.', '') }}, |
||||||
|
'currency': '{{ order.currency }}', |
||||||
|
'items': {{ order.items|json_encode()|raw }} |
||||||
|
}); |
||||||
|
console.log('Yandex purchase goal sent for order:', '{{ order.id }}'); |
||||||
|
} |
||||||
}); |
}); |
||||||
</script> |
</script> |
||||||
|
{% endif %} |
||||||
|
|
||||||
|
{# AddToCart - добавление в корзину (работает на всех страницах где включен) #} |
||||||
|
{% if enable_addtocart or pixel_event_type == 'PageView' %} |
||||||
<script> |
<script> |
||||||
window.dataLayer = window.dataLayer || []; |
document.addEventListener('DOMContentLoaded', function() { |
||||||
dataLayer.push({ |
// Обработчик для ваших кнопок добавления в корзину |
||||||
'ecommerce': { |
if (typeof jQuery !== 'undefined') { |
||||||
'purchase': { |
jQuery(function($) { |
||||||
'actionField': { |
// Обработчик клика по кнопкам добавления в корзину |
||||||
'id': '{{ order.id }}', |
$(document).on('click', '.single_add_to_cart_button, .ajax_add_to_cart', function(e) { |
||||||
'revenue': '{{ order.total|number_format(2, '.', '') }}', |
var $button = $(this); |
||||||
'currency': '{{ order.currency }}' |
|
||||||
}, |
// Небольшая задержка, чтобы товар успел добавиться |
||||||
'products': {{ order.items|json_encode()|raw }} |
setTimeout(function() { |
||||||
} |
sendAddToCartEvent($button); |
||||||
} |
}, 500); |
||||||
}); |
}); |
||||||
|
|
||||||
|
// Также оставляем стандартный обработчик WooCommerce на всякий случай |
||||||
|
$(document.body).on('added_to_cart', function(event, fragments, cart_hash, $button) { |
||||||
|
sendAddToCartEvent($button); |
||||||
|
}); |
||||||
|
|
||||||
|
// Функция отправки события AddToCart |
||||||
|
function sendAddToCartEvent($button) { |
||||||
|
// Получаем данные о товаре |
||||||
|
var productId = $button.data('product_id') || $button.attr('data-product_id') || ''; |
||||||
|
var quantity = parseInt($button.data('quantity') || $button.attr('data-quantity') || 1); |
||||||
|
|
||||||
|
// Пытаемся получить название товара разными способами |
||||||
|
var productName = $button.data('product_sku') || |
||||||
|
$button.closest('.product').find('.woocommerce-loop-product__title').text().trim() || |
||||||
|
$button.closest('.product').find('h2 a').text().trim() || |
||||||
|
$button.closest('.product').find('h3 a').text().trim() || |
||||||
|
$button.closest('.summary').find('.product_title').text().trim() || |
||||||
|
'Unknown Product'; |
||||||
|
|
||||||
|
// Получаем цену товара |
||||||
|
var priceText = ''; |
||||||
|
var $product = $button.closest('.product, .summary'); |
||||||
|
|
||||||
|
// Ищем цену в разных местах |
||||||
|
var $priceElement = $product.find('.price .woocommerce-Price-amount').last(); |
||||||
|
if ($priceElement.length === 0) { |
||||||
|
$priceElement = $product.find('.price .amount').last(); |
||||||
|
} |
||||||
|
if ($priceElement.length === 0) { |
||||||
|
$priceElement = $product.find('.price').last(); |
||||||
|
} |
||||||
|
|
||||||
|
if ($priceElement.length > 0) { |
||||||
|
priceText = $priceElement.text() || $priceElement.html() || ''; |
||||||
|
} |
||||||
|
|
||||||
|
// Очищаем цену от всего кроме цифр и точки |
||||||
|
var price = parseFloat(priceText.replace(/[^0-9.,]/g, '').replace(',', '.')) || 0; |
||||||
|
var totalValue = price * quantity; |
||||||
|
var currency = '{{ currency }}'; |
||||||
|
|
||||||
|
console.log('AddToCart data:', { |
||||||
|
productId: productId, |
||||||
|
productName: productName, |
||||||
|
price: price, |
||||||
|
quantity: quantity, |
||||||
|
totalValue: totalValue |
||||||
|
}); |
||||||
|
|
||||||
|
if (productId && totalValue > 0) { |
||||||
|
// Facebook Pixel - AddToCart |
||||||
|
if (typeof fbq !== 'undefined') { |
||||||
|
fbq('track', 'AddToCart', { |
||||||
|
content_ids: [productId.toString()], |
||||||
|
content_type: 'product', |
||||||
|
content_name: productName, |
||||||
|
value: totalValue, |
||||||
|
currency: currency |
||||||
|
}); |
||||||
|
console.log('Facebook AddToCart sent for product:', productId, 'value:', totalValue); |
||||||
|
} |
||||||
|
|
||||||
yaCounter96481053.reachGoal('purchase', { |
// GA4 - add_to_cart |
||||||
'order_id': '{{ order.id }}', |
if (typeof gtag !== 'undefined') { |
||||||
'order_price': '{{ order.total|number_format(2, '.', '') }}', |
gtag('event', 'add_to_cart', { |
||||||
'currency': '{{ order.currency }}', |
currency: currency, |
||||||
'items': {{ order.items|json_encode()|raw }} |
value: totalValue, |
||||||
}); |
items: [{ |
||||||
|
item_id: productId.toString(), |
||||||
|
item_name: productName, |
||||||
|
price: price, |
||||||
|
quantity: quantity |
||||||
|
}] |
||||||
|
}); |
||||||
|
console.log('GA4 add_to_cart sent for product:', productId); |
||||||
|
} |
||||||
|
|
||||||
fbq('track', 'Purchase', { |
// DataLayer для GTM |
||||||
value: {{ order.total|number_format(2, '.', '') }}, |
window.dataLayer = window.dataLayer || []; |
||||||
currency: '{{ order.currency }}', |
window.dataLayer.push({ |
||||||
content_ids: [{{ order.content_ids|join(',') }}], |
'event': 'add_to_cart', |
||||||
content_type: 'product' |
'ecommerce': { |
||||||
|
'currency': currency, |
||||||
|
'value': totalValue, |
||||||
|
'items': [{ |
||||||
|
'item_id': productId.toString(), |
||||||
|
'item_name': productName, |
||||||
|
'price': price, |
||||||
|
'quantity': quantity |
||||||
|
}] |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
// Яндекс.Метрика |
||||||
|
if (typeof yaCounter96481053 !== 'undefined') { |
||||||
|
yaCounter96481053.reachGoal('add_to_cart', { |
||||||
|
'product_id': productId.toString(), |
||||||
|
'product_name': productName, |
||||||
|
'price': price, |
||||||
|
'quantity': quantity, |
||||||
|
'value': totalValue, |
||||||
|
'currency': currency |
||||||
|
}); |
||||||
|
console.log('Yandex add_to_cart goal sent for product:', productId); |
||||||
|
} |
||||||
|
} else { |
||||||
|
console.warn('AddToCart: Missing product data', {productId, totalValue}); |
||||||
|
} |
||||||
|
}); |
||||||
|
}); |
||||||
|
} |
||||||
}); |
}); |
||||||
</script> |
</script> |
||||||
{% endif %} |
{% endif %} |
Loading…
Reference in new issue