Nรฅr en EAN ordre oprettes er der ikke en ekstern betalingsgateway da alt hรฅndtering foregรฅr pรฅ selve webshoppen, derfor sendes der ikke transaktions ID retur og derfor skal tracking af kรธb foretages pรฅ fรธlgende mรฅde
Bemรฆrk nedenstรฅende er et eksempel du skal selv tilpasse koden sรฅ den passer med jeres opsรฆtning
<?php
add_action('woocommerce_thankyou', 'yanco_cvr_payment_track_google_analytics_4_orders', 12, 1);
function yanco_cvr_payment_track_google_analytics_4_orders($order_id)
{
$order = wc_get_order($order_id);
$total = $order->get_total();
$total_tax = $order->get_total_tax();
$total_shipping = $order->get_shipping_total();
$order_items = $order->get_items();
$order_email = $order->get_billing_email();
$order_phone = $order->get_billing_phone();
$google_analytics_conversion_id = 'AW-123456789/XXXXXXX-YYYYYYY'; // IMPORTANT: Modify this ID to the correct ID
$trasaction_id = $order_id; // Defaults to the order ID
$transaction_affiliation = 'YOUR COMPANY NAME HERE'; // IMPORTANT: Modify this
$script = "<!-- Event snippet for Google Ads sporing conversion page -->";
$script .= "<script>";
$script .= "gtag('event', 'conversion', {";
$script .= "'send_to': '".$google_analytics_conversion_id."',";
$script .= "'value': " . $total . ",";
$script .= "'currency': 'DKK',";
$script .= "'transaction_id': '" . $order_id . "'";
$script .= "});";
$script .= "</script>";
$script .= "<script>
dataLayer.push({";
$script .= "'event': 'itmrevenue',";
$script .= "'transactionId': '" . $order_id . "',";
$script .= "'transactionAffiliation': '".$transaction_affiliation."',";
$script .= "'transactionTotal': " . $total . ",";
$script .= "'transactionTax': " . $total_tax . ",";
$script .= "'transactionShipping': " . $total_shipping . ",";
$script .= "'transactionEmail': '" . $order_email . "',";
$script .= "'transactionPhone': '" . $order_phone . "',";
if (count($order_items) > 0) {
$number_items = count($order_items);
$current_count = 1;
$script .= "'transactionProducts': [";
foreach ($order_items as $item_id => $order_item) {
$product = $order_item->get_product();
if ($product) {
$sku = $product->get_sku();
if (empty($sku)) {
$sku = $product->get_id();
}
$categories = '';
$wc_categories = wc_get_product_category_list($product->get_id(), ',', '', '');
if (!empty($wc_categories)) {
$categories = strip_tags($wc_categories);
}
$script .= "{";
$script .= "'sku': '" . $sku . "',";
$script .= "'name': '" . $order_item->get_name() . "',";
$script .= "'category': '" . $categories . "',";
$script .= "'price': " . $order_item->get_total() . ",";
$script .= "'quantity': " . $order_item->get_quantity();
$script .= "}";
if ($current_count != $number_items) {
$script .= ",";
}
}
$current_count++;
}
$script .= "]";
}
$script .= "});";
$script .= "</script>";
// Set meta_data to prevent double tracking if the browser gets reloaded for the order received page
$cvr_order_has_been_custom_tracked = get_post_meta($order_id, 'cvr_order_has_been_custom_tracked', true);
if ($cvr_order_has_been_custom_tracked == false ) {
echo $script;
update_post_meta($order_id, 'cvr_order_has_been_custom_tracked', true);
}
}