Real Life Example: Extract a Value from a String in GTM
This will come in handy when you need to extract a value from a string (like a product name from a title). I am sure that many people have covered this topic already, but let’s have a go on a specific example.
Situation
You have an e-shop selling motor oil. You would like to measure whether four litre bottles sell better than 208 litre barrels. Sadly, your measurement model isn’t set up for this, because you didn’t think of even wanting to measure it when you were building dataLayer.
Let’s say that developers charge you exorbitant amounts of money to modify the dataLayer, so you are on your own.
Fortunately, you notice that your products follow a precise naming convention.
- Total Hydroflo CT 20L TOTAL 20001218
- Castrol Magnatec A3/B4 10W40 4L CASTROL 11001209
- Mobil Nuto H 46 20L MOBIL 15001011
Another bit of good news is that product name gets pushed into the dataLayer as a variable called productName (yes, analysts are an imaginative lot).
Objectives
Your mission is simple. Find out how to extract volume of your products from their names and send the isolated variable to Google Analytics as a custom metric.
Action
Custom JavaScript variables to the rescue!
function() {
var pn = {{productName}};
return pn.split(" ").slice(-3).shift().split("L").shift()
}
Let’s see what this function does bit by bit.
The first method split();
separates the productName dataLayer variable into an array. Blank spaces act as separators. You have turned the product name into an array that looks like this: ["Total", "Hydroflo", "CT", "20L", "TOTAL", "20001218"]
The second method slice(-3);
selects the last three objects from the original array and discards the rest. Now you have this: ["20L", "TOTAL", "20001218"]
.
The third method shift();
selects the first object of the array and discards the rest. Voila, you are left with one string: "20L"
.
You are also left with one problem. Google Analytics won’t accept this string as a custom metric, because it’s neither an integer, nor a decimal, nor a time value. Let’s get rid of the “L” in the string. Using the split();
and shift();
methods again will clean up the value.
Almost there
Once you are done setting up the variable, send it with your Enhanced Ecommerce pageview or event hit as a Custom Metric.
Don’t forget to set up the custom metric in GA first. Set metric scope to “Product”, because you will be sending this variable only with product-related events or pageviews.
Do you want to know more about GTM magic? Our newsletter provides.
No spam, no annoying sales pitches.
Results
You have managed to enrich your GA reporting using common sense and a bit of simple JavaScript. Victory dance!
If you want to go down the rabbit hole of JavaScript in GTM, Simo has an overview of really nice tricks.
Managing Director
Honza Felt is a performance marketing specialist who turned into a marketing consultant after a mysterious accident. He spends his days leading a bunch of misfits at CF Agency. Drinks rum and knows things.