skip to Main Content

I have added the following script in my Magento website’s order confirmation page, but still, I am unable to track e-commerce data. Please help me to track e-commerce data in Google Analytics.

    <script async src="https://www.googletagmanager.com/gtag/js?id=UA 000000000-1"></script>

    <script>
    window.dataLayer = window.dataLayer || [];
    dataLayer.push({
   'thansactionId' : '<?php echo $orderId; ?>',
    'currency': 'GBP',
    'event' : 'conversion',
    'Grandtotal': '<?php echo $grandtotal; ?>',
    'status': '<?php echo "$status";?>',
    'storename': '<?php echo "$storename";?>',
    'shippingmethod': '<?php echo "$shipping";?>',
    'shippinamount': '<?php echo "$shippingamount";?>'
    });
    </script>

I am not using the Google Tag Manager, So I want the solution for Google Analytics only.

2

Answers


  1. Chosen as BEST ANSWER

    After hours of research I have found following script for my case as I am using analytics.js for my website. Let me know is it correct or not?

    <script type="text/javascript">
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    
    ga('create', 'UA-35506203-1', 'auto');
    ga('require', 'displayfeatures');
    ga('send', 'pageview');
    ga('require', 'ecommerce');
    
    ga('ecommerce:addTransaction', {
      'id': '1234',                     // Transaction ID. Required.
      'affiliation': 'Acme Clothing',   // Affiliation or store name.
      'revenue': '11.99',               // Grand Total.
      'shipping': '5',                  // Shipping.
      'tax': '1.29'                     // Tax.
    'currency': 'GBP'  // local currency code.
    });
    
    ga('ecommerce:addItem', {
      'id': '1234',                     // Transaction ID. Required.
      'name': 'Fluffy Pink Bunnies',    // Product name. Required.
      'sku': 'DD23444',                 // SKU/code.
      'price': '11.99',                 // Unit price.
      'quantity': '1'                   // Quantity.
    });
    ga('ecommerce:send'); 
    </script> 
    

  2. You are not using Google Tag Manager (GTM), but you are using data format of GTM. As per GTAG documentation, the (standard ecommerce) transaction tracking code is the following for gtag():

    gtag('event', 'purchase', {
      "transaction_id": "24.031608523954162",
      "affiliation": "Google online store",
      "value": 23.07,
      "currency": "USD",
      "tax": 1.24,
      "shipping": 0,
      "items": [
        {
          "id": "P12345",
          "name": "Android Warhol T-Shirt",
          "list_name": "Search Results",
          "brand": "Google",
          "category": "Apparel/T-Shirts",
          "variant": "Black",
          "list_position": 1,
          "quantity": 2,
          "price": '2.0'
        },
        {
          "id": "P67890",
          "name": "Flame challenge TShirt",
          "list_name": "Search Results",
          "brand": "MyBrand",
          "category": "Apparel/T-Shirts",
          "variant": "Red",
          "list_position": 2,
          "quantity": 1,
          "price": '3.0'
        }
      ]
    });
    

    You can fill in the variables from your PHP code, and optionally include product data as well.

    Enhanced ecommerce format is available here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search