Google Tag Manager allows you to easily send all products to an external resource, by using the dataLayer, and a specific tag.
But what if you want to fire a tag only when a specific product, or set of products, have been purchased?
I worked out an easy way to get it done, using macro's and dataLayer.
The first step is to get your dataLayer working.
I use this magento extension https://github.com/CVM/Magento_GoogleTagManager for placing of the GTM script snippet, and dataLayer into my magento sites.
Once configured, and installed, you will get a datalayer as such on your checkout success page:
The next step is to create a macro, which will extract the 'transactionProducts' information from the dataLayer.
This is a simple macro, pulling the datalayer into a named macro. I simply used 'Order_Products'
Once you have done that, you will find you have the data in the macro, but it is not useable (as far as I could gather) for a rule.
Seems like the rules cannot actually use the array/object data.
(would be great to see a 'is one of' type of rules in GTM going forward, with the ability to iterate arrays in the rule conditions)
To make this usable in a rule, the trick is to use an additional macro, which uses the data from the first macro, and turns that back into a string.
You use a javascript macro for this
The js used is simply:
function() {
return JSON.stringify({{Order_Products}});
}
Once this is completed, you will have a string version of the dataLayer products:
Now you can target the products you want, via a rule, simply using 'contains' on a string match.
After some extensive testing, and a lot of checkouts!, I had not found any issues, and the tag fired faithfully when I had that specific product in my order.
I am not sure if there is a better way, as the above does seem a bit of a work-around.
Would appreciate some feedback from any one who knows of a smarter way.