Data Layer Forensics: Ecommerce Edition



Troubleshoot ecommerce analytics by taking a “data snapshot” the moment tracking occurs. Use our code and handy checklist to detect hard-to-find issues.

Something is wrong with your ecommerce analytics data, but you’re not sure exactly what. You’ve checked the tracking code and it looks fine. That means the problem is probably coming from the server side.

Server side code takes transaction details — like products, quantity, and price — and places them where your analytics tracking code can read them.

If the transaction details don’t make sense, you’ll have problems such as transaction revenue with no products, or products with no revenue. If some details are malformed or missing, the tracking code may fail entirely.

We frequently use Google Tag Manager (GTM) to send ecommerce data to Google Analytics, which means servers are putting transaction details in the data layer. Our GTM tags read what’s in the data layer and track accordingly.

Step 1: Take a Data Snapshot with Our Code

How can you know whether or not the data layer was correct? You can capture the contents of the data layer by firing an event and a transaction at the same time, using the same rule in GTM.

Here’s our code that captures the ecommerce portion of the data layer:


The first part of the code captures the details of the overall transaction, while the second part captures all the product details. Each macro (in curly brackets) refers to a data layer variable or array, which you need to define in GTM in order to use this code.

We put the code into a custom JavaScript macro in GTM, and then deliver it as the event label of our GTM tag that fires at the same time we track a transaction. Our event tag is set up as follows:


The event label contains a macro named “transaction datalayer” which is where our code delivers the details.

The macros named “pageSite” and “transaction type” that you see in our event action refer to other details we have in our data layer. You may have different details you prefer to track there and can fill the event action as you see fit.

Test first, then publish a new version of your GTM container. You want to make sure your transaction tracking is still working, in addition to your new event tracking. Then step back and wait a day or two for the new event data to accumulate.

Step 2: Examine the Data with Our Checklist

After you have a day or two of event data, it’s time to look for potential issues. You will probably need to load the data into Excel to examine it most efficiently.

Here’s our checklist to get you started:

1. Cross-check Transaction IDs – Are all the transaction IDs from your event data also in your ecommerce data? “Empty” transactions with no total, no tax, no shipping, and no products will not be added to your GA ecommerce data, but will appear in your events.

2. Check for Required Fields and Formats – For non-empty event transactions that do not show up as ecommerce transactions, is there evidence that points to why they failed? Required fields must not be missing, and numeric fields must not contain currency symbols or commas. It’s also easy to break the data layer syntax if your product names and other fields contain unescaped apostrophes.

3. Do All Values Make Sense? – For example, we discovered that one of our clients was passing the address as the shipping cost. Google tried to be helpful by converting “1234 Elm Street” to “$1,234.00” in shipping!

4. Check Transaction Totals in Event Data – You may find that some transaction totals do not equal the sum of product prices (plus tax and shipping if you include that in your totals). We’ve found cases where the total is zero even though prices are non-zero, and vice-versa. There may be an explanation, such as coupon codes or free offers. Ideally, though, what you pass into GA as product revenue should line up with your transaction revenue.

5. Compare Totals in Event vs. Ecommerce Data – Make sure the transaction totals from the data layer (passed to your event data) are the same as the transaction totals in your ecommerce data.

You may uncover other anomalies in your ecommerce data once you start investigating with this behind-the-scenes look at the data layer. Or you may find explanations for things that looked strange on the surface, but turn out to be normal (and simply not tracked, such as free items or promotional offers).

How confident are you in your ecommerce analytics data? What issues do you suspect and are itching to prove? Do you have other techniques for this type of ecommerce investigation? Please share in the comments.

Dorcas Alexander is a Manager for the Analytics & Insight department. Her path to LunaMetrics followed stints in ad agency creative, math, and computer science. Dorcas has a master's degree in language and information technologies from Carnegie Mellon University, where she helped build precursors to a Universal Translator. One of the top-rated tournament Scrabble players in Pennsylvania, Dorcas has an insatiable drive to compete and win.

  • Ash

    Thanks for the great post. Implemented with some adjustments for our specific site.

    Works a treat, can already see some potential issues in the Real-Time reports.

    Also great way to waste your afternoon watching transactions within the Real-Time reports….

  • I’m using the datalayer to track purchases as described in googles documentation. It seems redundant to have to pass it twice. Is there a way to access the variables passed through GTM already?

    • Dorcas Alexander

      You can still access information from your data layer, using dots to indicate nested values. Create a macro/variable with type = “data layer variable” and with data layer variable name = the desired path with dots to indicated nesting. For example, your transaction ID could be referenced with: as the data layer variable name.

  • Hello Dorcas,
    Thank you for clarification. I made some modifications and it now works perfectly!
    I set a datalayer variable:
    DL Purchase = ecommerce.purchase

    and then modified the script for the javascript macro:

    function() {
    var generalString = “General”
    + “–ID:” + {{DL Purchase}}
    + “,AFF:” + {{DL Purchase}}.actionField.affiliation
    + “,TOT:” + {{DL Purchase}}.actionField.revenue
    + “,TAX:” + {{DL Purchase}}
    + “,SHI:” + {{DL Purchase}}.actionField.shipping
    + “|”;
    var productString = “”;
    var numProducts = {{DL Purchase}}.products.length;
    for(var i=0; i 0) {productString = productString + “|”};
    productString =
    productString +
    “P” + (i+1) +
    “–SKU:” + {{DL Purchase}}.products[i].id +
    “,NAM:” + {{DL Purchase}}.products[i].name +
    “,PRI:” + {{DL Purchase}}.products[i].price +
    “,QUA:” + {{DL Purchase}}.products[i].quantity;
    return generalString + productString;

  • Nick

    Hello Dorcas/Victor

    I’ve tried both the code in the blog post and an adapted version of Victor’s script but whenever I use the Preview tool in GTM version 2 it says your code has invalid css or javascript, and Victor’s script has an error: Error at line 12, character 16: Parse error. ‘;’ expected

    The code I adapted is below:

    function() {
    var generalString = “General”
    + “–ID:” + {{transactionID}}
    + “,AFF:” + {{transactionAffiliation}}
    + “,TOT:” + {{transactionTotal}}
    + “,TAX:” + {{transactionTax}}
    + “,SHI:” + {{transactionShipping}}
    + “|”;
    var productString = “”;
    var numProducts = {{transactionProducts}}.length;
    for(var i=0; i 0) {productString = productString + “|”};
    productString = productString + “P” + (i+1) + “–SKU:” + {{transactionProducts}}[i].sku + “,NAM:” + {{transactionProducts}}[i].name + “,PRI:” + {{transactionProducts}}[i].price + “,QUA:” + {{transactionProducts}}[i].quantity;
    return generalString + productString;

    I can’t see an error but I’m not sure how I’m supposed to test it if I can’t use the preview and debug mode in GTM. Any help would be greatly appreciated. BTW, great approach to what seems to be quite a common problem.

    • Dorcas Alexander

      Hi Nick,

      When we launched our new blog last month, we didn’t catch the code on this page in our proofreading process. I’ve fixed it now, so you should not get an error.

Contact Us.

Follow Us



We'll get back to you
in ONE business day.
Our Locations
THE FOUNDRY [map] LunaMetrics

24 S. 18th Street
Suite 100

Pittsburgh, PA 15203


4115 N. Ravenswood
Suite 101
Chicago, IL 60613


2100 Manchester Rd.
Building C, Suite 1750
Wheaton, IL 60187