Google Analytics for eCommerce

From NewHaven Software Wiki

Revision as of 15:56, 8 April 2010 by Malcolm lee (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Below is the code you need to track visits and sales for Google Analytics.

Make sure you replace the tracker ID with the customer's ID (_gat._getTracker("UA-XXXXXXX-X")) and change the affiliate to the store name or leave it blank.

You will want to put this in the _bottom.tpl.

{**** Begin Google Analytics Code ****}

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {ldelim}
var pageTracker = _gat._getTracker("UA-1666866-3");
pageTracker._trackPageview();
{rdelim} catch(err) {ldelim}{rdelim}</script>

You will want to put this in the checkout_thanks.tpl.

{*** Sales Tracking Code ****}

{if $view == 'checkout_thanks'}
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
  var pageTracker = _gat._getTracker("UA-1666866-3");
  pageTracker._trackPageview();
  pageTracker._addTrans(

    "{$placed.info.order_id}",                                     // Order ID
    "W00t",                            // Affiliation
    "{$placed.info.totals.subtotal}",                                    // Total
    "{$placed.info.totals.taxtotal}",                                     // Tax
    "{$placed.info.totals.shiptotal}",                                        // Shipping
    "{$placed.info.billing.billing_city}",                                 // City
    "{$placed.info.billing.billing_state}",                               // State
    "{$placed.info.billing.billing_country}"                                       // Country

  );

 {foreach from=$placed.cart item=ditem key=dkey}
 {assign var="dcat" value=$ditem.cat}
  pageTracker._addItem(
    "{$placed.info.order_id}",                                     // Order ID
    "{$ditem.sku}",                                     // SKU
    "{$ditem.name}",                                  // Product Name 
    "{$categories.$dcat.name}",                             // Category
    "{$ditem.real_price}",                                    // Price
    "{$ditem.qty}"                                         // Quantity
  );
 {/foreach}

  pageTracker._trackTrans();

</script>
{/if}
{**** End Google Analytics Code ****}
Personal tools