I’m trying to translate GTM/UA enhanced ecommerce dataLayer values to dataLayer variables to build a Piwik-compatible ecommerce transaction tag. I’ve added the dataLayer variables to the tag manager, but the values are still ‘undefined’.
This is the content of my GTM/UA dataLayer after a successful donation:
Hi, thanks for reaching out. Unfortunately, it’s not possible to refer to nested attributes of a datalayer object directly. You could use a PPTM datalayer variable, but you would need to fetch the whole ecommerce object and then iterate through on the custom HTML tag level. It would look roughly like this:
var orderDetails = {{ ecommerce object }};
var order_id = orderDetails.purchase.actionField.id
var grand_total = orderDetails.purchase.actionField.revenue
{{ ecommerce object }} is a PPTM datalayer variable ecommerce.
Please see this thread for complete code snippet examples for similar data layer objects.
The GA enhanced ecommerce dataLayer contains more details about the transaction. On a product level there’s brand and variant, on purchase level there’s list and option. We currently use these variables in our reporting. Is there any way I could capture these and use them as custom dimensions?
Hi @michiel , most definitely!
On the product level, you could utilize Category placeholder (please find more details here). I know that it’s not exactly the same as brand or variant, but it is an additional product-level attribute.
As for purchase level - custom dimensions in the scope of an event are the way to go. Please declare them first in Piwik PRO following this article. Once that’s done - please use the following method to set custom dimension value:
Hey @pslonina, I just ran into another challenge with ecommerce. We’re running a Woocommerce based webshop with the WooCommerce Google Analytics extension to track cart actions and purchases. I’m trying to translate this plugin’s dataLayer purchase event to Piwik, but I can’t get this to work. The dataLayer looks a bit different:
Hi @michiel ,
My apologies for the late reply. You are right - the issue is the formatting and there is no simple way of addressing it from Piwik PRO level.
Is it possible to configure the plugin so it sends a key-value pair "event": "purchase"? If not - maybe there are alternative plugins that would build dataLayer objects for WooCommerce?
Hey @pslonina,
I found another plugin (GTM4WP) that constructs dataLayer events in a correct way. I’m working on getting the add_to_cart event working, but I think I’m getting it wrong.
This is an example dataLayer for the add_to_cart event:
<script>
var _paq = _paq || [];
var orderDetails = {{ DLV | Ecommerce }}
var products = orderDetails.items;
products.forEach(function(product){
_paq.push(['addEcommerceItem', product.id.toString(), product.name, product.category, product.price, product.quantity])
});
</script>
Unfortunately, this results in a JavaScript error (Uncaught ReferenceError: ecommerce is not defined). I tried several other options, but none of them seems to work.
EDIT
I changed the tag a bit and resolved the JS error, but the add_to_cart event still isn’t pushed to Piwik correctly.
Thanks, works like a charm! The only thing that doesn’t seem to work is updating the total cart value: adding a new item to the cart changes the value of the cart to the last added item:
I guess this has more to do with the dataLayer? The value in the dataLayer doesn’t calculate the sum of the price of the products added to the cart. Is there any way to make that calculation?
EDIT
The view_cart event does update the total cart value:
It’s strange, but I get mixed results with the add_to_cart event.
Product overview page
On the product overview page I can add a single product if the product doesn’t have any options to choose from. If I add such a product to the cart, I get a Cart update event in the tracker debugger with the correct product, quantity and price. From the overview page, I can only add 1 item to the cart.
Product detail page
If I add a product to the cart from a product detail page, I can see the add_to_cart tag fires, but it doesn’t register an event in the tracker debugger. I tested this with ánd without the option to prevent page reload in the tag manager debug mode.
In the tag manager debugger I still see the add_to_cart event, including the right details for the cart update. In the example below I added 4 chocolate bars to the cart with a total value of 26 EUR, which add the following to the dataLayer:
I was able to pinpoint the problem. So the thing is that for Product overview page, product quantities are numbers while for Product detail page product quantities are string. Therefore you need to use parseFloat() in order to convert them to numbers. Please see the code below:
<script>
var _paq = _paq || [];
var orderDetails = {{ DLV | Ecommerce }};
var products = orderDetails.items;
var grand_total = parseFloat(orderDetails.value);
products.forEach(function(product){
_paq.push(["addEcommerceItem", product.item_id, product.item_name, product.item_category, product.price, parseFloat(product.quantity)])
});
_paq.push(["trackEcommerceCartUpdate", grand_total]);
</script>