John Davidson

php - javascript array displays the name of the array not the contents (gtag, purchase event)

0 comments
Message:


I am trying to create a gtag purchase event code in a Woocommerce Eshop in Thank you page.


But when I see the code in source in the browser it is displaying the name of the array and not the contents.


I can see the purchases in google dashboard but there is no track of the items.


I can only see the total price of the purchases.


Here is my code:


if( is_wc_endpoint_url( 'order-received' ) ){
$order_id = absint( get_query_var('order-received') );
$order = wc_get_order( $order_id );
$order_items = $order->get_items();
$items_data = []; // Initializing
foreach ( $order_items as $item_id => $item ) {
$variation_id = $item->get_variation_id();
$product_id = $variation_id > 0 ? $variation_id : $item->get_product_id();

//get the categories tree
foreach( wp_get_post_terms( $product_id, 'product_cat' ) as $term ){
if( $term ){
$categories .= $term->name;
$categories .= '/';
}
}
$categories = trim($categories, '/');
$product = wc_get_product( $product_id );
// Set specific data for each item in the array
array_push($items_data, array(
item_id => $product->get_sku(),
item_name => $product->get_name(),
item_category => $categories,
quantity => $item->get_quantity(),
price => $item->get_subtotal(),
));
$categories = '';
}
?>
<script>
var items_data = <?=json_encode($items_data)?>;
var my_items = [];

for(var i=0; i<items_data.length; i++){
var item = items_data[i]; console.log(item);
my_items.push({
item_id:item.item_id,
item_name:item.item_name,
item_category:item.item_category,
quantity:item.quantity,
price:item.price
});
}
</script>

<!-- Event snippet for Website sale conversion page -->
<script> gtag('event', 'conversion', { 'send_to': 'AC-898s89ds/PNdsds78dsy_ds82B', 'value': '<?php echo $order->get_total(); ?>', 'currency': 'EUR', 'transaction_id': '<?php echo $order_id; ?>' }); </script>
<script>
console.log({items:my_items}); //this displays correctly the content of the array

gtag('event', 'purchase', { 'transaction_id': '<?php echo $order_id; ?>', 'affiliation': 'eartshop.gr', 'value': '<?php echo $order->get_total(); ?>', 'currency': 'EUR', 'shipping': '<?php echo $order->get_shipping_total(); ?>', 'tax' : '<?php echo $order->get_total_tax(); ?>', 'items': my_items });
</script>

And this is what I see in the source code of Thank you page :


gtag('event', 'purchase', { 'transaction_id': '10707', 'affiliation': 'eshopdomain.com', 'value': '152.00', 'currency': 'EUR', 'shipping': '0.00', 'tax' : '29.03', 'items': my_items });


My Answer:
>received' ) ){
$order_id = absint( get_query_var('order-received') );
$order = wc_get_order( $order_id );
$order_items = $order->get_items();

$products = array();

foreach( $order_items as $item ){
$product_id = $item->get_product_id();
$product_name = $item->get_name();
$product_price = $item->get_total();

$products[] = array(
'id' => $product_id,
'name' => $product_name,
'price' => $product_price
);
}

?>

}
?>

In this code, I have added a loop to get each item in the order and create an array of products with their id, name, and price. Then I encode this array as JSON and pass it to the gtag purchase event as the "items" parameter. This should display the contents of the array in the Google dashboard instead of just the total price of the purchases.

Rate this post

3 of 5 based on 1326 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us