Добавлен тестовый режим метрик

pull/36/head
Your Name 4 weeks ago
parent 2bc9c1b0b7
commit 4098b3c7b9
  1. 123
      wp-content/themes/cosmopet/global-functions/multisite-functions.php

@ -78,12 +78,54 @@ function custom_canonical_url() {
/**
* Добавление событий контрибуции для FP Pixel
* только на боевом сайте АЕ
* */
if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') {
/* Передача просто по url */
*/
// if ($site_env->site_mode == 'production' && $site_env->site_region == 'ae') {
// Проверка на тестовый режим для метрик
function is_gp_test_mode() {
if (isset($_GET['gp-test']) && $_GET['gp-test'] == '1') {
return true;
}
if (is_user_logged_in() && current_user_can('administrator')) {
return true;
}
return false;
}
add_action('wp_footer', 'add_facebook_pixel_events');
function add_facebook_pixel_events() {
// 1. Событие AddToCart (Добавление в корзину)
if (is_gp_test_mode()) return;
global $product;
// 1. ViewContent
if (is_product() && $product && $product->get_price() > 0) {
?>
<script>
document.addEventListener('DOMContentLoaded', function() {
fbq('track', 'ViewContent', {
content_ids: ['<?php echo $product->get_id(); ?>'],
content_type: 'product',
value: <?php echo $product->get_price(); ?>,
currency: '<?php echo get_woocommerce_currency(); ?>'
});
});
</script>
<?php
}
// 2. InitiateCheckout
if (is_checkout() && !is_wc_endpoint_url('order-received') && WC()->cart && WC()->cart->get_cart_contents_count() > 0) {
?>
<script>
document.addEventListener('DOMContentLoaded', function() {
fbq('track', 'InitiateCheckout');
});
</script>
<?php
}
// 3. AddToCart
if (is_product() || is_shop() || is_cart()) {
?>
<script>
@ -95,9 +137,8 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') {
var productName = $button.data('product_sku') ||
$button.closest('.product').find('.woocommerce-loop-product__title').text().trim() || 'Unknown';
var priceElement = $button.closest('.product').find('.price .amount').text().replace(/[^0-9.]/g, '') || '0.00';
var currency = '<?php echo get_woocommerce_currency(); ?>'; // Динамическая валюта
var currency = '<?php echo get_woocommerce_currency(); ?>';
// Событие для Facebook Pixel
fbq('track', 'AddToCart', {
content_ids: [productId],
content_type: 'product',
@ -105,7 +146,6 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') {
currency: currency
});
// Событие для Google Analytics
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': 'add_to_cart',
@ -127,7 +167,32 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') {
<?php
}
// 2. Событие Purchase (Покупка)
// 4. AddPaymentInfo
if (is_checkout() && !is_wc_endpoint_url('order-received') && WC()->cart && WC()->cart->get_cart_contents_count() > 0) {
$currency = get_woocommerce_currency();
$cart_total = WC()->cart->get_total('edit');
?>
<script>
document.addEventListener('DOMContentLoaded', function() {
fbq('track', 'AddPaymentInfo', {
value: <?php echo $cart_total; ?>,
currency: '<?php echo $currency; ?>'
});
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': 'add_payment_info',
'ecommerce': {
'currency': '<?php echo $currency; ?>',
'value': <?php echo $cart_total; ?>
}
});
});
</script>
<?php
}
// 5. Purchase
if (is_wc_endpoint_url('order-received')) {
$order_id = absint(get_query_var('order-received'));
if (!$order_id) return;
@ -168,43 +233,16 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') {
</script>
<?php
}
// 3. Событие AddPaymentInfo
if (is_checkout() && !is_wc_endpoint_url('order-received')) {
$currency = get_woocommerce_currency();
$cart_total = WC()->cart ? WC()->cart->get_total('edit') : 0;
?>
<script>
document.addEventListener('DOMContentLoaded', function() {
fbq('track', 'AddPaymentInfo', {
value: <?php echo $cart_total; ?>,
currency: '<?php echo $currency; ?>'
});
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': 'add_payment_info',
'ecommerce': {
'currency': '<?php echo $currency; ?>',
'value': <?php echo $cart_total; ?>
}
});
});
</script>
<?php
}
}
/* Передача контрибуции со страницы "Спасибо" */
add_action('woocommerce_thankyou', 'send_purchase_to_metrika');
function send_purchase_to_metrika($order_id)
{
if (!$order_id)
return; // Проверка, что заказ существует
function send_purchase_to_metrika($order_id) {
if (is_gp_test_mode()) return;
if (!$order_id) return;
$order = wc_get_order($order_id);
if ($order->get_status() !== 'processing' && $order->get_status() !== 'completed')
return; // Отправляем только для оплаченных заказов
if ($order->get_status() !== 'processing' && $order->get_status() !== 'completed') return;
$items = [];
foreach ($order->get_items() as $item) {
@ -217,7 +255,6 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') {
];
}
// Получаем валюту заказа
$currency = $order->get_currency();
?>
<script>
@ -235,7 +272,6 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') {
}
});
// Яндекс.Метрика
yaCounter96481053.reachGoal('purchase', {
'order_id': '<?php echo $order_id; ?>',
'order_price': '<?php echo $order->get_total(); ?>',
@ -243,7 +279,6 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') {
'items': <?php echo json_encode($items); ?>
});
// Facebook Pixel
fbq('track', 'Purchase', {
value: <?php echo $order->get_total(); ?>,
currency: '<?php echo $currency; ?>',
@ -253,7 +288,7 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') {
</script>
<?php
}
}
// }
// Отключаем кэширование для страниц товаров
add_action('template_redirect', function() {

Loading…
Cancel
Save