You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
332 lines
16 KiB
332 lines
16 KiB
{# templates/head-pixel-functions.twig #}
|
|
|
|
{# PageView - просмотр любой страницы #}
|
|
{% if pixel_event_type == 'PageView' %}
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Facebook Pixel - PageView
|
|
if (typeof fbq !== 'undefined') {
|
|
fbq('track', 'PageView');
|
|
console.log('Facebook PageView sent for page:', '{{ page_data.page_type }}');
|
|
}
|
|
|
|
// 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>
|
|
{% endif %}
|
|
|
|
{# ViewContent - просмотр товара #}
|
|
{% if pixel_event_type == 'ViewContent' and product %}
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Facebook Pixel - ViewContent
|
|
if (typeof fbq !== 'undefined') {
|
|
fbq('track', 'ViewContent', {
|
|
content_ids: ['{{ product.id }}'],
|
|
content_type: 'product',
|
|
content_name: '{{ product.name|escape('js') }}',
|
|
content_category: '{{ product.category|escape('js') }}',
|
|
value: {{ product.price|number_format(2, '.', '') }},
|
|
currency: '{{ currency }}'
|
|
});
|
|
console.log('Facebook ViewContent sent for product:', '{{ product.id }}', 'value:', {{ product.price|number_format(2, '.', '') }});
|
|
}
|
|
|
|
// GA4 - view_item
|
|
if (typeof gtag !== 'undefined') {
|
|
gtag('event', 'view_item', {
|
|
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
|
|
}]
|
|
});
|
|
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>
|
|
{% endif %}
|
|
|
|
{# InitiateCheckout - начало оформления заказа #}
|
|
{% if pixel_event_type == 'InitiateCheckout' %}
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Facebook Pixel - InitiateCheckout
|
|
if (typeof fbq !== 'undefined') {
|
|
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.push({
|
|
'event': 'begin_checkout',
|
|
'ecommerce': {
|
|
'currency': '{{ currency }}',
|
|
'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>
|
|
{% endif %}
|
|
|
|
{# Purchase - завершение покупки #}
|
|
{% if pixel_event_type == 'Purchase' and order %}
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Facebook Pixel - Purchase
|
|
if (typeof fbq !== 'undefined') {
|
|
fbq('track', 'Purchase', {
|
|
value: {{ order.total|number_format(2, '.', '') }},
|
|
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.push({
|
|
'event': 'purchase',
|
|
'ecommerce': {
|
|
'transaction_id': '{{ order.id }}',
|
|
'currency': '{{ order.currency }}',
|
|
'value': {{ order.total|number_format(2, '.', '') }},
|
|
'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>
|
|
{% endif %}
|
|
|
|
{# AddToCart - добавление в корзину (работает на всех страницах где включен) #}
|
|
{% if enable_addtocart or pixel_event_type == 'PageView' %}
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Обработчик для ваших кнопок добавления в корзину
|
|
if (typeof jQuery !== 'undefined') {
|
|
jQuery(function($) {
|
|
// Обработчик клика по кнопкам добавления в корзину
|
|
$(document).on('click', '.single_add_to_cart_button, .ajax_add_to_cart', function(e) {
|
|
var $button = $(this);
|
|
|
|
// Небольшая задержка, чтобы товар успел добавиться
|
|
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);
|
|
}
|
|
|
|
// GA4 - add_to_cart
|
|
if (typeof gtag !== 'undefined') {
|
|
gtag('event', 'add_to_cart', {
|
|
currency: currency,
|
|
value: totalValue,
|
|
items: [{
|
|
item_id: productId.toString(),
|
|
item_name: productName,
|
|
price: price,
|
|
quantity: quantity
|
|
}]
|
|
});
|
|
console.log('GA4 add_to_cart sent for product:', productId);
|
|
}
|
|
|
|
// DataLayer для GTM
|
|
window.dataLayer = window.dataLayer || [];
|
|
window.dataLayer.push({
|
|
'event': 'add_to_cart',
|
|
'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>
|
|
{% endif %} |