0

Ich versuche, eine DataLayer für WooCommerce Thank you Seite einzurichten. Sehen Sie hier, was ich bisher getan (die Variablen nicht vollständig aufgebaut noch):Enhanced E-Commerce dataLayer WooCommerce

function dataLayer_google_gtm($order_id) { 

    // Lets grab the order 
    $order = wc_get_order($order_id); 

    // Products 
    $products = $order->get_items(); 
    ?> 

    <script> 
     dataLayer.push({ 
     'ecommerce': { 
      'purchase': { 
       'actionField':{ 
        'id':'<?php echo $order->get_order_number(); ?>', 
        'affiliation':'CHACAFOODS', 
        'revenue':'<?php echo $order->get_order_total(); ?>', 
        'tax':'<?php echo $order->get_total_tax(); ?>', 
        'shipping':'<?php echo $order->get_shipping(); ?>', 
        'coupon':'<?php echo $order->get_order_discount_total(); ?>', 
       }, 
       window['service'].push('products':,[ 
       <?php 
        $count = 0; 
        foreach($products as $item_id => $item) { 
         $count++; 
         $product = $order->get_product_from_item($item); ?> 
         { 
          'name':<?php echo $item['name']; ?>', 
          'id': 
          'price': '<?php echo $order->get_line_subtotal($item); ?>', 
          'brand': 
          'category': 
          'variant': 
          'quantity': '<?php echo $item['qty']; ?>' 
         } 
       <?php if (count($order->get_items()) > $count) { echo ","; } ?> 
       <?php } ?> 
       ]); 
      } 
     } 
     }); 
    </script> 
    <?php 
} 
add_action('woocommerce_order_status_completed', 'prefix_service_conversion_tracking'); 

Kann mir jemand sagen, ob meine Struktur oben, wie die erweiterten E-Commerce-Struktur unten Ergebnis aussehen wird?

google data Struktur:

<script> 
// Send transaction data with a pageview if available 
// when the page loads. Otherwise, use an event when the transaction 
// data becomes available. 
dataLayer.push({ 
    'ecommerce': { 
    'purchase': { 
     'actionField': { 
     'id': 'T12345',       // Transaction ID. Required for purchases and refunds. 
     'affiliation': 'Online Store', 
     'revenue': '35.43',      // Total transaction value (incl. tax and shipping) 
     'tax':'4.90', 
     'shipping': '5.99', 
     'coupon': 'SUMMER_SALE' 
     }, 
     'products': [{       // List of productFieldObjects. 
     'name': 'Triblend Android T-Shirt',  // Name or ID is required. 
     'id': '12345', 
     'price': '15.25', 
     'brand': 'Google', 
     'category': 'Apparel', 
     'variant': 'Gray', 
     'quantity': 1, 
     'coupon': ''       // Optional fields may be omitted or set to empty string. 
     }, 
     { 
     'name': 'Donut Friday Scented T-Shirt', 
     'id': '67890', 
     'price': '33.75', 
     'brand': 'Google', 
     'category': 'Apparel', 
     'variant': 'Black', 
     'quantity': 1 
     }] 
    } 
    } 
}); 
</script> 

Vielen Dank für Ihre Hilfe!

Mit freundlichen Grüßen, Anton

Antwort

3

Sie können so etwas wie dies versuchen:

<script> 
     dataLayer.push({ 
      'ecommerce': { 
      'currencyCode': '<?php echo $order->get_order_currency(); ?>', 
      'purchase': { 
       'actionField':{ 
       'id': '<?php echo $order->get_order_number(); ?>', 
       'affiliation': 'WooCommerce', 
       'revenue': <?php echo number_format($order->get_subtotal(), 2, ".", ""); ?>, 
       'tax': <?php echo number_format($order->get_total_tax(), 2, ".", ""); ?>, 
       'shipping': <?php echo number_format($order->calculate_shipping(), 2, ".", ""); ?>, 
       <?php if($order->get_used_coupons()): ?> 
        'coupon': '<?php echo implode("-", $order->get_used_coupons()); ?>' 
       <?php endif; ?> 
       }, 
       'products': [ 
        <?php 
        foreach($order->get_items() as $key => $item): 
         $product = $order->get_product_from_item($item); 
         $variant_name = ($item['variation_id']) ? wc_get_product($item['variation_id']) : ''; 
        ?> 
         { 
         'name': '<?php echo $item['name']; ?>', 
         'id': '<?php echo $item['product_id']; ?>', 
         'price': '<?php echo number_format($order->get_line_subtotal($item), 2, ".", ""); ?>', 
         'brand': '', 
         'category': '<?php echo strip_tags($product->get_categories(', ', '', '')); ?>', 
         'variant': '<?php echo ($variant_name) ? implode("-", $variant_name->get_variation_attributes()) : ''; ?>', 
         'quantity': <?php echo $item['qty']; ?> 
         }, 
        <?php endforeach; ?> 
       ] 
      } 
      } 
     }); 
    </script> 

Für Marken Sie ein Produkt Attribut verwenden könnte, die Sie dann ausdrucken.

Viel Glück!

+0

Vielen Dank @webizon. Ich habe keine Antwort zu diesem Thema erwartet :) – Toni2708

+0

Gern geschehen! Dies ist eigentlich der erste Beitrag, den ich schreibe. Ich wäre sehr glücklich, wenn du die Antwort akzeptierst, wenn du damit zufrieden bist, mir ein paar Punkte zu geben. Prost! – webizon

Verwandte Themen