Google Analytics eCommerce Tracking

ga-ecommerce-tracking

Making the tracking code work with Interspire Shopping Cart 6

Chances are that you've landed here because you've got a store running Interspire Shopping Cart and you can't figure out why the ecommerce tracking in Google Analytics is not receiving any data. Before you implement the fix below, you'll need to ensure that you have:

  • Set up your Google Analytics tracking code in your Interspire Shopping Cart software via Settings > Analytics Settings
  • Enabled your Google Analytics profile for ecommerce - see below

What about the Ecommerce tracking code?

Yep, you've already done all that and the Ecommerce section of your GA account is blank. If you've looked into this further like I did, you will have discovered that there is another script required by Google Analytics to send the order data to your GA account. This is detailed on the Ecommerce Tracking page of Google Code.

Yeah, you get that, but how do you get that in your Interspire source code? Well, it's already there... but its wrong.

If you place an order through your store and land on the 'Thanks for your Order' page (which is finishorder.php) and you view the source code, you'll see right at the bottom, just inside the closing </div><!--#Container--> tag, the ecommerce tracking code complete with order data. The problem is that is the old traditional tracking code, instead of the new asynchronous code.

How to fix the tracking code

Open the file module.googleanalytics.php located in /modules/analytics/googleanalytics/ and make a backup copy before changing any code.

Note: This fix is for Interspire Shopping Cart version 6.1.1 and this fix will no doubt eventually become redundant.

Near the bottom of the file you'll see a comment // Using the new version of the tracking code. This marks the beginning of the relevant code that you need to modify.

1. Find this:

$(document).ready(function() {
	if(typeof(pageTracker) != 'undefined') {
		pageTracker._addTrans(  
  

Change to this:

_gaq.push(['_addTrans',

2. Find this:

pageTracker._addItem(

Change to this:

_gaq.push(['_addItem',

3. Find this (*make sure you select all three lines - thanks to Lance & Brian's comments):

        pageTracker._trackTrans();
    }
});

Change to this:

_gaq.push(['_trackTrans']);

4. Lastly, make sure you change the closing brackets for each of steps 1 and 2 above. Find the closing parenthesis:

);

And add a closing square bracket inside like this:

]);

5. Optionally, you can edit the comments after each order variable in the arrays for your own reference in the output source code. For example:

'".$order['orderid']."', // order ID

The complete corrected PHP code

$conversionCode = "
    <script type=\"text/javascript\">
    _gaq.push(['_addTrans',
                '".$order['orderid']."', // order ID
                '',
                '".number_format($this->GetTotalAmount(), 2, '.', '')."', // order total
                '".number_format($this->GetTaxCost(), 2, '.', '')."', // tax
                '".number_format($this->GetShippingCost(), 2, '.', '')."', // shipping
                '".$billingAddress['ordbillsuburb']."', // suburb
                '".$billingAddress['ordbillstate']."', // state
                '".$billingAddress['ordbillcountry']."' // country
            ]);
";
while($product = $GLOBALS['ISC_CLASS_DB']->Fetch($productResult)) {
    $productId = $product['ordprodid'];
    if($product['ordprodvariationid'] > 0) {
        $productId .= '-'.$product['ordprodvariationid'];
    }
    $prodCode = $product['ordprodsku'];
    if (empty($prodCode)) {
        $prodCode = $product['ordprodid'];
    }

    $conversionCode .= "
        _gaq.push(['_addItem',
            '".$order['orderid']."', // order ID
            '".addslashes($prodCode)."', // SKU
            '".addslashes($product['ordprodname'])."', // product name
            '',
            '".number_format($product['price_inc_tax'], 2, '.', '')."', // product price
            '".$product['ordprodqty']."' // order quantity
        ]);
    ";
}
$conversionCode .= "
        _gaq.push(['_trackTrans']);
</script>";

The final source code

Now the source code for your finishorder.php page should display the tracking code near the bottom of the page similar to this:

<!-- Start conversion code for analytics_googleanalytics -->   
<script type="text/javascript">
	_gaq.push(['_addTrans',
		'34', // order ID
		'',
		'19.99', // order total
		'1.81', // tax
		'3.00', // shipping
		'Sydney', // city or sububrb
		'New South Wales', // state or provence
		'Australia' // country
	]);

    _gaq.push(['_addItem',
        '34', // order ID
        '2731058', // SKU
        'Product Name', // product name
        '',
        '19.99', // product price
        '1' // order quantity
    ]);

    _gaq.push(['_trackTrans']);
</script>
<!-- End conversion code for analytics_googleanalytics -->  
  

Article by

21 Responses to “Google Analytics eCommerce Tracking”

  1. Lance says:

    Thanks for this post. It was exactly what I needed.

    To clarify a couple of things, let me add that this is necessary if your Google Analytics code (as defined under Settings / Analytics Settings) sets ‘ga.async = true’. That is why the ‘old’ code won’t work.

    Secondly, the step by step directions need one more item. You need to remove the closing curly braces and parenthesis after the last _trackTrans call. (It is shown correctly in the ‘Complete Corrected PHP Code’.)

  2. Imagixel says:

    Great post! thanks a lot!

    We were struggling with this for a while, never picked up on that bug (which by the way has been around since ISC 5.0.X) Lets hope they get it fixed for the upcoming releases.

    I have just changed the analytics module, haven’t seen if this works for me but seems very logical.

  3. Pete says:

    We just updated a clients website which runs Interspire 5.1.43 to async tracking. After setting up event tracking on some forms and links we noticed ecommerce had stopped gathering data. The code above was copied and pasted into the php module but no data has been gathered. I’m going to go through the check out process later to test that the source of finishorder.php has gathered all the variables properly. If you know of any other files that need to be edited for 5.1 please let me know

    thanks

  4. Brian says:

    Thanks a lot for doing this post. I’ve been trying to figure this out for a while now and I’ve finally got it working in Interspire 5.5.4. There was the one issue that Lance mentioned where, if you follow the step by step instructions it won’t work because the:
    }
    });
    needs to be removed before the final closing script tag on line 165. Like he said, it’s correct in the complete corrected PHP code.

  5. Tim says:

    Thanks Lance and Brian for the tip-off on the missing info in step 3. I have updated this now.

    Further to this post, I have since noticed that the ecommerce data that is tracked by Google Analytics differs from the data in the Interspire Shopping Cart control panel, sometimes with significant discrepancies.

    I have not thoroughly investigated this, but my best guess is that declined credit card transactions are to blame. In the case of my client’s store, the use a payment gateway (eWay) which will reject incorrect card details or cards (eg: Amex). The finishorder.php page is still loaded in this instance, thus the data is tracked by Google Analytics as if it were a successful transaction.

    I am hoping to find some way of conditionally loading the GA tracking code only if the transaction is successful. If and when I do, I will update this post.

  6. Brian says:

    It seems that around the time I made these changes we started having problems with the checkout process where it would loop back without storing changes. If you have more than one item in your cart and you choose “Ship items to different addresses” and you try and change one of the addresses in the drop-down menu and click “Ship to These Addresses” it will just return to the same page without saving the changes and revert to the first result in the drop-down menu. There’ve been other problems where it’s looping back around in other areas of the checkout process without saving the data also and I can’t figure out why so I was wondering if anyone else has run into this problem after making these changes. Sorry if this is off-topic but I’m not sure if it’s related and I’m getting desperate trying to figure this out.

    • Tim says:

      Hi Brian,

      No, I haven’t had any other reports of an issue like yours. I can’t see any reason why changes to the module.googleanalytics.php file would cause an issue with your checkout process, as the ecommerce tracking code is only loaded into finishorder.php, which happens after checkout.php

      My approach to weird issues like the one you are describing is always a process of elimination. You could start by reverting back to the unmodified module.googleanalytics.php file that shipped with your version of ISC and then try the checkout process again. Otherwise you could try removing the shortcode for analytics from your HTMLHead.html panel file (maybe substitute in your regular GA tracking code, just so you’re not messing with live stats).

      Honestly, it sounds like something is wrong with the checkout code. Maybe try the multi page checkout instead of single page express (or vice versa). Dare I ask if you’ve had any luck with Interspire support?

  7. Brian says:

    Thanks for your answer Tim. That was exactly what I was saying to the client – that I only modified the finishorder.php page and not the checkout page. When I did this change I also upgraded to the new asynchronous google analytics code but I already tried removing that and it didn’t help.

    We just ordered Interspire support for a large sum of money because we’re also having problems backing up the current database (gets stuck halfway) and I need to move Interspire to a new server.

  8. Dtrain says:

    AWESOME!!!!

  9. David says:

    The code example you provide – as far as I can tell has several errors.

    1. The line:

    ‘”.number_format($product['total_inc_tax'], 2, ‘.’, ”).”‘, // product price

    The result of this line iss $0.00 for each product.
    Your variable should be ‘price_inc_tax’ not ‘total_inc_tax’

    2. Your code is also missing the line responsible for inserting the category information –

    ‘Widgets’, // category

    People who have implemented this code will not get proper product tracking stats unless they make the change to price_inc_tax ?

    Also, whats with the blank line in the addtrans section just under order id and again in the add item section under product name (where the category code should be perhaps?

    • Tim says:

      Thanks for your feedback David.

      The instructions I have outlined in the ‘how to fix the tracking code’ simply cover modifying the original Interspire code to update it according to the newer version of GA. I am not offering any insight into the variables within the tracking code itself, other than step 5 where I note that comments can be added.

      I have updated my post with your fix for my error relating to price_inc_tax. This was apparently something I carried forward from a pre-6.1 version of the file. This error may in fact be a contributing factor to the discrepancies I mention in my comment on July 7. My apologies go out to all those who have made use of my fix.

      The blank line that you have pointed out is unchanged from the original Interspire code. I do not know what is the relevant variable code for category information but if you are able to figure this out I will be glad to update my post accordingly.

      Tim.

  10. David says:

    Hi

    Thanks for the quick reply.

    You must be refering to a dodgy version of the original code. 6.1.1 contains a line of code in addTrans:

    ‘”.addslashes(GetConfig(‘StoreName’)).”‘,

    Where as you have a blank line… Perhaps recheck your original zip file from Interspire?

    The category bit however is actually blank in the Original Interspire code – and I dont know how to make it work either :-(

  11. Tim says:

    A further note on this topic. After the correction that David made to the ‘product price’ value in the addItem part, I have reviewed the data that has been tracked on my site. I have discovered a bug where the total revenue is double the actual transactions revenue. See the following screenshots which show a single transaction logged in both the ‘Overview > Transactions’ page and then the detailed view page for the individual transaction:

    Total revenue (incorrect)

    Transaction revenue (correct)

    If anyone has an answer to this issue, I’d love to know what it is!

    Tim.

  12. louise says:

    Hi there

    I wonder can you help me. I run on 6.1.1 and the php you refer to does not show the same on my site. I cannot see this in my php $(document).ready(function() {
    if(typeof(pageTracker) != ‘undefined’) {
    pageTracker._addTrans(

    Could you possibly email me? I would really appreciate your help!

    many thanks

    Louise

    • Tim says:

      Hi Louise,

      I just had a look at the module.googleanalytics.php file from a 6.1.1 version and the code I refer to is on lines 135-137. The code you need to change in step 2 is on line 159 and the code in step 3 is on lines 170-172. Step 4 refers to the code on lines 146 & 166. Keep in mind that once you change the code in step 1, these line numbers will shift slightly.

      Cheers,

      Tim.

  13. jason says:

    hi, ive tried this on 5.5.4 and not getting any ecommerce details in my analytics account, changed all the code as per instructions but still got a big fat 0 on everything, any ideas?

  14. jonsam says:

    been stuck on this for a while thanks for the help please put your paypal email here or email me so i can buy you a beer :)

  15. Shuja says:

    Hi sir,

    Thanks for your this post, but sir i have implement the code in module.googleanalytics.php and follow the exit steps you have described but when i have checked the eCommerce tracking, there is no information about the ecommerce tracking while simple tracking comming on my GA dashboard, sir please help why the ecommerce tracking information are not shown on my dashboard.

    thanks,

Leave a Reply