GTM

How to Sum Multiple GTM Data Layer Variables into One

Need a custom variable to help you manipulate your data layer for a marketing tag? Enter the custom javascript variable in GTM.

Have you ever come across a data layer that had just about every possible eCommerce order variable but it was missing the only one that you needed to implement for a marketing tag?

Let’s say this is an example of your data layer:

ecommerce data layer

But you need a variable that is the grand total: Revenue (87) + Tax  (7.73).

Enter the custom Javascript variable in Google Tag Manager.

If you aren’t able to add this grand total variable to the data layer yourself then you can use a custom Javascript variable to sum these for you.

Here’s what this looks like:

custom javascript variable sum

function() {
 return parseFloat({{Purchase - Revenue}}) + parseFloat({{Purchase - Tax}});
}

This is a simple function that converts the variables to a floating point number (parseFloat) and then allows you to sum (+) one or more variables together.

You can do the same for other math operations. Be sure to update with your own variables from GTM and test in preview mode first.


Brad Redding

Brad, co-founder of Elevar, has lived in eCommerce for over 12 years. He's helped design, build, and optimize over 100 websites in his career. From new retail startups to well-known brands like Le Creuset, Signature Hardware, Rebecca Minkoff, Char-Broil and more, he specializes in data analytics and conversion optimization to help achieve business goals.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Articles

Talk with Us

Please complete the form to request a demo, discuss our solutions, or to just say hi!



Some of our clients