Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics conversion code not working

I have enabled conversion tracking in Google Analytics. My analytics code is included on every page like this:

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-104000000-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];

  function gtag() {
    dataLayer.push(arguments);
  }
  gtag('js', new Date());
  gtag('config', 'UA-104000000-1');
</script>

Despite it showing Tag Manager code, I actually got it through Analytics, and intentionally DID NOT use Google Tag setup.

I also added JS code on a page that the user will end-up after completing transaction. My code is added at the bottom of the page and looks like this:

<script>
  ga('require', 'ecommerce');
  ga('ecommerce:addTransaction', {
    'id': '%invoice.public_id%',
    'revenue': ' %payment.amount%'
  });
  ga('ecommerce:addItem', {
    'id': '%invoice.public_id%',
    'name': '%item.item_title%',
    'price': '%item.first_total%',
    'quantity': '%item.qty%'
  });
  ga('ecommerce:send');
</script>

All the placeholder values populate properly, yet I don't get the conversion data trickle to the analytics. The rest of the analytics seem to be working just fine.

UPDATE: Also tried changing my code to the below one but still only visits an no e-commerce. I did check to make sure Enhanced e-commerce is enabled.

<script>
    gtag('event', 'purchase', {
        'transaction_id': '%invoice.public_id%',
        'value': '%payment.amount%',
        'items': [
            {
                'id': '%item.item_id%',
                'name': '%item.item_title%',
                'quantity': 1,
                'price': '%item.first_total%'
            }
        ]
    });
</script>

What am I missing?

like image 977
santa Avatar asked Jun 17 '26 01:06

santa


1 Answers

You are mixing the gtag code with the Universal Analytics (ga) code. Here you can find documentation for Enhanced Ecommerce with gtag:

https://developers.google.com/analytics/devguides/collection/gtagjs/enhanced-ecommerce

Another solution may be to use the Universal Analytics code instead gtag:

<!-- Google Analytics -->
<script>
(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','https://www.google- analytics.com/analytics.js','ga');

ga('create', 'UA-104000000-1', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->

So for ecommerce tracking you can use the snippet you are wrote in your post:

<script>
  ga('require', 'ecommerce');
  ga('ecommerce:addTransaction', {
    'id': '%invoice.public_id%',
    'revenue': ' %payment.amount%'
  });
  ga('ecommerce:addItem', {
    'id': '%invoice.public_id%',
    'name': '%item.item_title%',
    'price': '%item.first_total%',
    'quantity': '%item.qty%'
  });
  ga('ecommerce:send');
</script>
like image 73
Michele Pisani Avatar answered Jun 18 '26 14:06

Michele Pisani