function push_product_view_event() {
global $product;
$product_data = array(
‘event’ => ‘product_view’,
‘ecommerce’ => array(
‘detail’ => array(
‘products’ => array(
array(
‘id’ => $product->get_id(),
‘name’ => $product->get_name(),
‘price’ => $product->get_price(),
)
)
)
)
);
dataLayer.push($product_data);
}
add_action(‘woocommerce_before_single_product’, ‘push_product_view_event’);
//direct to fb
function push_product_view_event() {
global $product;
$product_id = $product->get_id();
$product_name = $product->get_name();
$product_price = $product->get_price();
$product_category = wp_get_post_terms( $product_id, ‘product_cat’ );
?>
<script>
fbq(‘track’, ‘ViewContent’, {
content_name: ‘<?php echo $product_name; ?>’,
content_ids: [‘<?php echo $product_id; ?>’],
content_type: ‘product’,
value: <?php echo $product_price; ?>,
currency: ‘USD’,
category: ‘<?php echo $product_category[0]->name; ?>’
});
</script>
<?php
}
add_action(‘woocommerce_before_single_product’, ‘push_product_view_event’);