Sunday 21 June 2015

Product-scoped custom dimensions with Google Tag Manager

Product-scoped custom dimensions (and metrics) are one of my favorite features of Universal Analytics. They allow you to categorize your products in a very flexible way, capturing the complexities of real businesses.

I've often been asked to see purchasing behavior for products like 'the red ones' or 'cars with more than 150k on the odometer'. Try doing that with the default eCommerce implementation!

So when I was recently moving an iOS app over to GTM, I wanted to throw on a bunch of custom dimensions and metrics on our products. I found that not only is there no documentation on how to do so, there wasn't even any forum chatter on this subject. Oh Noes!

Eventually the Google Analytics Premium team helped clear things up, so for you benefit here's the trick:

You can't set up your own GTM variables in the product object - you have to declare the dimension directly.

Ergh. So GTM is supposed to remove the need to push static data fields directly into Google Analytics, but this is an exception. So, onto the actual code. Here's what I hoped would work:

[dataLayer push:@{@"ecommerce": @{
                    @"purchase": @{
                      @"actionField": @{
                        @"id": @"T12345",  
                        @"affiliation": @"Online Store",
                        @"revenue": @"35.43",  
                        @"tax":"4.90",
                        @"shipping": @"5.99",
                        @"coupon": @"SUMMER_SALE"},
                      @"products": @[ // List of productFieldObjects.
                        @{@"name": @"Triblend Android T-Shirt",
                          @"id": @"12345",
                          @"price": @"15.25",
                          @"brand": @"Google",
                          @"category": @"Apparel",
                          @"variant": @"Gray",
                          @"quantity": @1,
                          @"coupon": @"",
                          @"howManyArms": @"A thousand arms"}, // My custom dimension

THIS DOESN'T WORK even when you setup the GTM variable on howManyArms and set it as a custom dimension in the eCommerce tag. Basically I think GA won't accept fields that don't have special GA names in the eCommerce objects. The solution is to do this instead:

[dataLayer push:@{@"ecommerce": @{
                    @"purchase": @{
                      @"actionField": @{
                        @"id": @"T12345",
                        @"affiliation": @"Online Store",
                        @"revenue": @"35.43",
                        @"tax":"4.90",
                        @"shipping": @"5.99",
                        @"coupon": @"SUMMER_SALE"},
                      @"products": @[ // List of productFieldObjects.
                        @{@"name": @"Triblend Android T-Shirt",
                          @"id": @"12345",
                          @"price": @"15.25",
                          @"brand": @"Google",
                          @"category": @"Apparel",
                          @"variant": @"Gray",
                          @"quantity": @1,
                          @"coupon": @"",
                          @"dimension6": @"A thousand arms"}, // My custom dimension works!


I hope that helps you as much as it did me!