cancel
Showing results for 
Search instead for 
Did you mean: 

How to add ecommerce tracking using google analytics in magento 2.2.2

How to add ecommerce tracking using google analytics in magento 2.2.2

Hi,

I need to send ecommerce data from success page to the google analytics, Where I have to place the analytics code, my reference url is : https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce

4 REPLIES 4

Re: How to add ecommerce tracking using google analytics in magento 2.2.2

You can add Orders data from success page by event observer.

You need to create an event 

checkout_onepage_controller_success_action

You need to create observer and set your data to send to google analytics.

 

Check List of all events in Magento 2, list of events in Magento 2

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: How to add ecommerce tracking using google analytics in magento 2.2.2

you can also directly add the script in success.phtml by getting the order details and pass it through, below is the code to get details

 

<?php
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$orderDatamodel = $objectManager->get('Magento\Sales\Model\Order')->getCollection()->getLastItem();
$orderId   =   $orderDatamodel->getId();
$order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
$orderItems = $order->getAllItems();

foreach ($orderItems as $item) {
    $productArray[] = array(
                "product_sku" => $item->getSku(),
                "product_magento_id" => $item->getProductId(),
                "product_name" => $item->getName(),
                "product_qty" => $item->getQtyOrdered(),
                "product_price" => $item->getPrice(),
                "product_discount_amount" => $item->getDiscountAmount(),
                "product_row_price" => $item->getPrice() - $item->getDiscountAmount(),
                );
};
?>   

At last add the script as the format required,  

 

 <script>
ga('require', 'ecommerce');
<?php
$items=[];
foreach ($orderItems as $item) {?>
$items[]=['sku'='<?php echo $item->getSku() ?>', 'price'= '<?php echo round($item->getPrice(),2); ?>', 'quntity'='<?php echo round($item->getQtyOrdered()); ?>'];
<?php } ?>
//similarly for order transaction details an then send

ga('ecommerce:send'); 
</script>

 

Re: How to add ecommerce tracking using google analytics in magento 2.2.2

I have added this code in success.html page, but we did't get any data in analytics. Actually we need the data in shopping behavior that is under the conversions in analytics, Could you please explain how to get the shopping behavior. Any help will be appreciated 

 

Thanks and Regards

 

Megha P . D 

Re: How to add ecommerce tracking using google analytics in magento 2.2.2

Hi @Kavitha Mano ,

I have the same issue, my customer is asking to place the Google ads conversion event tag to success.phtml,

but its in this format: (google tag manager code for conversion)

<!-- Event snippet for Thankyou Page conversion page -->
<script>
  gtag('event', 'conversion', {
      'send_to': 'AW-XXXXXXXXXX/qwz_XXXXXXXXX',
      'value': 1.0,
      'currency': 'EUR',
      'transaction_id': ''
  });
</script>

what code I have tı place inside the success.phtml?

 

This is little bit different then your code I think?

Looking forward to hearing from you soon.

regards