Sometimes you may want to store additional information in Google Analytics for E-commerce transactions. This post will detail one way of doing so for a ‘similar products’ feature, however, this solution can be adapted for other types of information.
To summarise the solution in a single line: store the information in a cookie and at check out submit the information in the items’ category parameter.
Let’s start with some simple HTML for the similar products feature.
Mozilla has a succinct cookie utility http://developer.mozilla.org/en-US/docs/Web/API/document.cookie but unfortunately, they added a GPL licence to it at some point making it unsuitable for some projects.
The code does the following:
- captures the click, (addEventListener polyfill for IE8)
- gets the product ID from the
data-skuattribute, if you do not have an attribute like this you will probably need an extra step to parse the id from the
- if a cookie called
similarProductsdoes not exist, creates one and adds the product ID to it.
- if a
similarProductscookie does exist, checks the product ID is not already stored and if not adds it to the cookie with a comma character as a separator.
The final step is to send the E-commerce data to Google Analytics after ordering.
(transaction data that is unimportant for this example such as price, revenue, tax… have been omitted to make the solution simpler).
First, we load in the e-commerce plugin and have the order data in a format we can use to send as a transaction to Google Analytics.
getSimilarProductIds() checks if a
similarProducts cookie exists and if so returns an array of the values.
updateItemIfSimilarProduct() loops through each product/item object, created from the actual customer order, and for each one then loops through the
similarProductIds array to see if the current product objects’ id can be found in the
similarProductIds array. If it does it amends the product objects’ category to add ‘Similar Product’ to the end.
(If not supporting Internet Explorer 8 or lower, or you have other JS libraries loaded on this page, you could re-write that function with some of the extra array methods available for a cleaner solution).
Finally, the code that uses those two methods along with setting up the E-commerce transaction, looping through the
products array to create E-commerce items, and then sending the transaction.
The downside to this solution, seen in the screenshot below, is if you wanted to see how many items from a category like Clothing were sold you would now need to do a search. The positive is you can combine this metadata with Segments, Custom Reports… to gain insight from Google Analytics data that is not available in database/server-side tools.
Another example of where this solution was useful was for a job website. Treating job applications as ‘transactions’, The Marketing team asked for metadata such as the client name, client type (e.g. agency or direct), promoted job… that they could then combine with other parts of Google Analytics to assist with the marketing strategy.