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.

Get Exclusive Elevar Insights directly to your inbox.

Get free analytics tips and resources delivered directly to your inbox.

Facebook Conversions API Setup

Join our server side tagging challenge to launch your FB conversion API integration.

Learn More

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.

  1. Hi, thanks for nice tutorial. But in cases when one variable is undefined, function show NaN. Any ideas how to fix it?

Leave a Reply

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

Related Articles