Quantcast
Viewing all articles
Browse latest Browse all 31

Tracking Removed Products from the Shopping Cart in Google Analytics with jQuery

Part of the buying process on an Ecommerce site are visitors removing products from the shopping cart.

Image may be NSFW.
Clik here to view.
Removed Products from Cart - Goal Conversion Rate

Tracking this can be done by programming code into the Ecommerce solution (that may take some time/cost extra money since you will probably have to involve other people to do the programming/implementation, and then you have to wait for the release). To get around this problem I track this with some help from jQuery.

Scripts in this blog post are not "out of the box", so you will have to rewrite them to suit your needs. It's your HTML structure and elements that defines what your script will look like.

In this blog post you will find 2 different examples of shopping carts that hopefully will make it easier for you to do the rewriting.

Tracking Removed Products from Cart in Google Analytics

I track products removed from the shopping cart using Event Tracking. Although Event Tracking is great for this task, Events have their limitations. These are the questions I will have answered with my Removed Products from Cart tracking:

  • Which product where removed from cart?
  • How many items where removed from cart?
  • What was the product value?

If you are familiar with the Event Tracking syntax, you know that an Event is built up by Category, Action, Label and Value.

If you take a look at the list above, you will see that all the things I want to know can't be fitted into 1 Event tracker. With other words, we will have to run 2 Event trackers when a product is Removed from Cart.

This is how my Events are organized. Red text means that this information is collected from the page using jQuery.

CategoryActionLabelValue
Removed from CartItemsProduct NameNumber of Items
Removed from CartSumProduct NameTotal Product Price (No decimals)

As you can see from the report below, sometimes Event 2 isn't tracked (can happen if the page does a refresh. We don't live in a perfect data world unfortunately). It's therefor important to put the most "valuable" tracker first in your script. For me that is the "Sum tracker".

Image may be NSFW.
Clik here to view.
Removed Products from Cart - Events

Even if this data tell you a story inside the Google Analytics interface, this data begs for Excel. Here I combine Sum & Items data, graph the data, and I can also "score" Added to Cart products against Removed from Cart products.

Image may be NSFW.
Clik here to view.
Removed Products from Cart - Excel Dashboard

To extract the data from Google Analytics to Excel I use NextAnalytics, an Excel plugin that I highly recommend. It gives you a ton of possibilites for slicing and dicing data from Google Analytics, Facebook, Twitter and more, and the support is awesome.

Removing products using checkboxes

To remove products from some shopping carts, checkboxes are used. This means that several products can be removed in one operation. On purpose I'm mixing different currencies just to demonstrate that the script can handle that.

ProductItemsItem PriceTotal Price
My Awesome Product1 139.40 NOK1 139.40 NOK
Another Awesome Product$ 23.95$ 47.90
Total Price inc. VATNOK 334.00
VATNOK 66.80


Product HTML

Below is how the HTML for the example above looks like.

Remove formatting from Code

  1. <table class="basket">
  2. <tr>
  3. <th>Product</th><th align="right">Items</th><th align="right">Item Price</th><th align="right">Total Price</th>
  4. </tr>
  5. <tr class="data">
  6. <td><input id="remove-123" type="checkbox" name="remove-123" />My Awesome Product</td><td align="right"><input name="quantity-123" type="text" value="1" size="2" id="quantity-123" /></td><td align="right">1 139.40 NOK</td><td align="right">1 139.40 NOK</td>
  7. </tr>
  8. <tr class="data">
  9. <td><input id="remove-456" type="checkbox" name="remove-456" />Another Awesome Product</td><td align="right"><input name="quantity-456" type="text" value="2" size="2" id="quantity-456" /></td><td align="right">$ 23.95</td><td align="right">$ 47.90</td>
  10. </tr>
  11. <tr>
  12. <td colspan="3"><strong>Total Price inc. VAT</strong></td><td align="right"><strong>NOK 334.00</strong></td>
  13. </tr>
  14. <tr>
  15. <td colspan="3">VAT</td><td align="right">NOK 66.80</td>
  16. </tr>
  17. </table>
  18. <input type="button" name="btnRemove" value="Remove" id="btnRemove" />

jQuery for tracking Products Removed using Checkboxes

Since several products can be removed in one operation, the jQuery script will have to loop through the shopping cart  based on which checkboxes that are checked. You will at least have to rewrite the code in red.

Remove formatting from Code

  1. <script type="text/javascript">
  2. $(document).ready(function() {
  3. if ($(".basket .data").length > 0){ // Only run the script if there are rows that contains products
  4. function removeItems() {
  5. $('.basket .data:has(input:checked)').each(function() { //Check if checkbox is checked, then loop through those that are checked.
  6. var productName = $.trim($(this).find("td").filter(':first').text()); // Product name is in first column. You may have to change this part
  7. var productSum = $.trim($(this).find("td").filter(':last').text().replace(/\,/g,".")); // Product sum is in last column. If you are using , (comma) as a 1000 separator, change this RegEx to (/\,/g,"")
  8. productSum = Math.round(productSum.replace(/(?!\.)[\D\s]/g,"")); // Decimals are not allowed in Events. We strips away everything except numbers and . (dot), and then we round the number.
  9. var productItems = ($.trim($(this).find("td input").filter(':last').val())); // Find last input field that contain Items and get the value.
  10. // Send data to Google Analytics
  11. _gaq.push(['_trackEvent','Removed from Cart','Sum',productName,Number(productSum)]);
  12. _gaq.push(['_trackEvent','Removed from Cart','Items',productName,Number(productItems)]);
  13. });
  14. }
  15. }
  16. $("#btnRemove").on("mousedown tap", (removeItems)); // When a visitor clicks on the "Remove Button", run the tracking function. Using "tap" to collect iPad/iPhone data better.
  17. });
  18. </script>

Removing Products One by One

In the shopping cart demo below, products are removed one by one. On purpose I'm mixing different currencies just to demonstrate that the script can handle that.

ProductItemsItem PriceTotal PriceRemove
My Awesome Product1 139,40 NOK1 139.40 NOKRemove
Another Awesome Product$ 23.95$ 47.90Remove
Total Price inc. VATNOK 334.00 
VATNOK 66.80 

Product HTML

Below is how the HTML for the example above looks like.

Remove formatting from Code

  1. <table class="basket">
  2. <tr>
  3. <th>Product</th><th align="right">Items</th><th align="right">Item Price</th><th align="right">Total Price</th><th>Remove</th>
  4. </tr>
  5. <tr class="data">
  6. <td>My Awesome Product</td><td align="right"><input name="quantity-123" type="text" value="1" size="2" /></td><td align="right">1 139,40 NOK</td><td align="right">1 139.40 NOK</td><td><a href="#" class="btn-remove">Remove</a></td>
  7. </tr>
  8. <tr class="data">
  9. <td>Another Awesome Product</td><td align="right"><input name="quantity-456" type="text" value="2" size="2" id="quantity-456" /></td><td align="right">$ 23.95</td><td align="right">$ 47.90</td>
        <td><a href="#" class="btn-remove">Remove</a></td>
  10. </tr>
  11. <tr>
  12. <td colspan="3"><strong>Total Price inc. VAT</strong></td><td align="right"><strong><u>NOK 334.00</u></strong></td><td align="right">&nbsp;</td>
  13. </tr>
  14. <tr>
  15. <td colspan="3">VAT</td><td align="right">NOK 66.80</td><td align="right">&nbsp;</td>
  16. </tr>
  17. </table>

jQuery for tracking Products Removed One by One

You will at least have to replace the code in red. What to replace with depends on your HTML.

Remove formatting from Code

  1. <script type="text/javascript">
  2. $(document).ready(function() {
  3. $('a.btn-remove').on("mousedown tap", function() { // Using "tap" to collect iPad/iPhone data better.
  4. var prodElem = $(this).parent().parent();
  5. var productName = $.trim($(prodElem).find('td:first').text()); // Product name is in first column. You may have to change this part
  6. var productSum = $.trim($(prodElem).find('td:first').next().next().next().text().replace(/\,/g,".")) // If you are using , (comma) as a 1000 separator, change this RegEx to (/\,/g,"")
  7. productSum = Math.round(productSum.replace(/(?!\.)[\D\s]/g,"")); // Decimals are not allowed in Events. We strips away everything except numbers and . (dot), and then we round the number.
  8. var productItems = $.trim($(prodElem).find("td input").filter(':last').val()); // Find input field for Items and get the value.
  9. // Send data to Google Analytics
  10. _gaq.push(['_trackEvent','Removed from Cart','Sum',productName,Number(productSum)]);
  11. _gaq.push(['_trackEvent','Removed from Cart','Items',productName,Number(productItems)]);
  12. });
  13. });
  14. </script>
So how can this be easier than just get somebody to implement the necessary scripts into the HTML of the page? The answer to that is Tag Management Systems, which makes you less dependent of the IT-department.

The demo script on this page is served through Google Tag Manager.

Google Universal Analytics

With the next version of Google Analytics, called Google Universal Analytics, only 1 Event is needed since additional data can be tracked as a Custom Metric. This will improve the data quality, and make the data more accessible in Google Analytics.

Below is an example how this could be tracked with Universal Analytics.

Remove formatting from Code

  1. <script type="text/javascript">
  2. $(document).ready(function() {
  3. ...original script here
  4. // Send data to Google Analytics
  5. ga("send", "event", {eventCategory: "Removed from Cart", eventAction: "Items", eventLabel: productName, eventValue: Number(productItems), metric2: Number(productSum)});
  6. ...original script here
  7. </script>

Some final words

If you think the code could be improved, feel free to comment (I'm not a programmer).

I'm also curious about how you are tracking products Removed from Cart in Google Analytics, and how you are using this data for optimization?

Related Articles

Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 31

Trending Articles