131 lines
3.8 KiB
Plaintext
131 lines
3.8 KiB
Plaintext
|
|
|
|
//INF: Google Analytics 4
|
|
function my_custom_ga4_code() {
|
|
?>
|
|
<!-- Global site tag (gtag.js) - Google Analytics -->
|
|
<script async src="https://www.googletagmanager.com/gtag/js?id=G-GEED4JX54T"></script>
|
|
<script>
|
|
window.dataLayer = window.dataLayer || [];
|
|
function gtag(){dataLayer.push(arguments);}
|
|
gtag('js', new Date());
|
|
gtag('config', 'G-GEED4JX54T');
|
|
gtag('config', 'AW-11210370143');
|
|
</script>
|
|
<?php
|
|
}
|
|
|
|
//INF: Google Ads Conversion Tracking
|
|
add_action( 'woocommerce_thankyou', 'google_ads_conversion_tracking' );
|
|
|
|
function google_ads_conversion_tracking( $order_id ) {
|
|
// Rozpocznij sesję, jeżeli jeszcze nie jest rozpoczęta
|
|
if (!session_id()) {
|
|
session_start();
|
|
}
|
|
// Zdobądź dane zamówienia
|
|
$order = wc_get_order( $order_id );
|
|
|
|
// Sprawdź, czy zamówienie istnieje
|
|
if ( ! $order ) {
|
|
return;
|
|
}
|
|
|
|
// Sprawdź, czy ta transakcja została już zarejestrowana
|
|
if (isset($_SESSION['tracked_order_ids']) && in_array($order_id, $_SESSION['tracked_order_ids'])) {
|
|
// Ta transakcja została już zarejestrowana, nie rób nic
|
|
return;
|
|
} else {
|
|
// Rejestruj transakcję
|
|
$_SESSION['tracked_order_ids'][] = $order_id;
|
|
|
|
// Twój kod śledzenia Google Ads tutaj, zastąp 'CONVERSION_ID' i 'CONVERSION_LABEL' swoim prawdziwym ID konwersji i etykietą
|
|
?>
|
|
<script>
|
|
gtag('event', 'conversion', {'send_to': 'AW-112103701413/ZSGuCLvSqKsYEN_YwuEp',
|
|
'value': <?php echo $order->get_total(); ?>,
|
|
'currency': 'PLN',
|
|
'transaction_id': '<?php echo $order_id; ?>'
|
|
});
|
|
</script>
|
|
<?php
|
|
}
|
|
}
|
|
|
|
//INF:: Google Analytics 4 Conversion Tracking
|
|
add_action( 'woocommerce_thankyou', 'ga4_conversion_tracking' );
|
|
|
|
function ga4_conversion_tracking( $order_id ) {
|
|
if (!session_id()) {
|
|
session_start();
|
|
}
|
|
// Pobierz dane zamówienia
|
|
$order = wc_get_order( $order_id );
|
|
|
|
// Sprawdź, czy zamówienie istnieje
|
|
if ( ! $order ) {
|
|
return;
|
|
}
|
|
|
|
// Sprawdź, czy ta transakcja została już zarejestrowana
|
|
if (isset($_SESSION['ga4_tracked_order_ids']) && in_array($order_id, $_SESSION['ga4_tracked_order_ids'])) {
|
|
// Ta transakcja została już zarejestrowana, nie rób nic
|
|
return;
|
|
} else {
|
|
// Rejestruj transakcję
|
|
$_SESSION['ga4_tracked_order_ids'][] = $order_id;
|
|
}
|
|
|
|
// Przygotuj dane transakcji
|
|
$transactionData = array(
|
|
'transaction_id' => $order_id,
|
|
'affiliation' => get_bloginfo( 'name' ),
|
|
'value' => $order->get_total(),
|
|
'currency' => get_woocommerce_currency(),
|
|
'tax' => $order->get_total_tax(),
|
|
'shipping' => $order->get_shipping_total(),
|
|
'items' => array()
|
|
);
|
|
|
|
// Pobierz dane o produktach
|
|
foreach ($order->get_items() as $item_id => $item) {
|
|
$product = $item->get_product();
|
|
|
|
$transactionData['items'][] = array(
|
|
'item_id' => $product->get_id(),
|
|
'item_name' => $product->get_name(),
|
|
'item_category' => strip_tags(wc_get_product_category_list($product->get_id())),
|
|
'item_variant' => $product->get_sku(),
|
|
'price' => $product->get_price(),
|
|
'quantity' => $item->get_quantity()
|
|
);
|
|
}
|
|
|
|
// Wydrukuj dane jako skrypt JavaScript
|
|
?>
|
|
<script>
|
|
gtag('event', 'purchase', <?php echo json_encode($transactionData); ?>);
|
|
</script>
|
|
<?php
|
|
}
|
|
|
|
|
|
//INF: Google Analytics 4 Product View Tracking
|
|
add_action( 'woocommerce_after_single_product', 'ga4_product_view_tracking' );
|
|
|
|
function ga4_product_view_tracking() {
|
|
global $product;
|
|
|
|
$productData = array(
|
|
'item_id' => $product->get_id(),
|
|
'item_name' => $product->get_name(),
|
|
'item_category' => strip_tags(wc_get_product_category_list($product->get_id())),
|
|
'item_variant' => $product->get_sku(),
|
|
'price' => $product->get_price()
|
|
);
|
|
?>
|
|
<script>
|
|
gtag('event', 'view_item', <?php echo json_encode($productData); ?>);
|
|
</script>
|
|
<?php
|
|
} |