'Duplicated conversion counting in GA when thankyou page is loaded again
I received 2 snippets from google support to connect my website with ads/analytics. It works fine (value, etc), except duplicates/multiplied values. My conversion (purchase) is duplicated every time when customer visit order confirmation link (like from email). 4 links clicked = transaction multipied by 4. How can I get rid off that?
Script 1:
function ads_purchase_tracking($order_id){
$order = new WC_Order($order_id);
$currency = $order->get_order_currency();
$total = $order->get_total();
$cart_value = number_format( (float) $order->get_total() - $order->get_total_tax() - $order->get_total_shipping(), wc_get_price_decimals(), '.', '' ); ?>
<script>
gtag('event', 'conversion', {
'send_to': 'AW-XXXXXXXX/XXXXXXXXXXXXXXXXXXXXXX',
'value': <?php echo $cart_value ?>,
'currency': '<?php echo $currency ?>',
'transaction_id': '<?php echo $order_id ?>'
});
</script>
Script 2:
<?php
$order_id = $order->get_order_number();
$total = $order->get_total();
$order_shipping_total = $order->get_total_shipping();
$order1 = new WC_Order( $order_id );
$items = $order1->get_items();
?>
<script type="text/javascript">
gtag('event', 'purchase', {
'transaction_id': '<?php echo $order_id ?>',
'affiliation': '<?php echo $order->get_payment_method() ?>',
'value': <?php echo $total ?>,
'shipping': <?php echo $order_shipping_total ?>,
'tax': <?php echo $order->get_total_tax() ?>,
'currency': '<?php echo $order->get_currency() ?>',
'items' : [
<?php if(count($items) > 0){ ?>
<?php foreach ( $items as $item ) { ?>
<?php $product = $order->get_product_from_item( $item ); ?>
{
'id': '<?php echo $product->get_id() ?>',
'name': '<?php echo $product->get_name()?>',
'category': '<?php echo $product->get_categories() ?>',
'price': <?php echo $order->get_item_total( $item ) ?>,
'quantity': <?php echo $item['qty'] ?>
},
<?php } ?>
<?php } ?>
]
});
</script>
<?php }
'''
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
