简体   繁体   中英

Google Analytics eCommerce Tracking snippet

I am trying to track eCommerce transactions using google analytics ecommerce tracking...

<?php
$tx_token = '234234234';
$amount = '19.99';
?>

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-12345678-1']);
  _gaq.push(['_trackPageview']);

  _gaq.push(['_addTrans',
    '<?php echo $tx_token; ?>',  // Transaction ID
    'My Store',             // Affiliation or store name
    '<?php echo $amount; ?>',    // Total
  ]);

  _gaq.push(['_addItem',
    'D1D44',
    'My Item',
    'Category / Variation',
    '<?php echo $amount; ?>',
    '1'
  ]);

  _gaq.push(['_trackTrans']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

Should this be enough? Do I need the $tx_token value in addItem as well as addTrans?

Yes, you need the transaction id for each item in the transaction (that's how Google Analytics can tell that the items belong to a transaction). So the transaction id is the required first parameter for each addItem call. Actually that is described quite thoroughly in the GA documentation ( Ecommerce Tracking with ga.js ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM