How to automate unit testing of data layer triggered tags

There are a number of big advantages to implementing a data layer, including better data consistency, improved reliability, and increased flexibility when it comes to sending data to different analytics vendors. However, there is one downside — a data layer is yet another possible failure point in your analytics pipeline, which means that you need to test regularly to ensure that it’s collecting and sending complete, accurate data. 

In a previous blog post, we talked about how to validate the data that’s passed to your tag manager from your data layer. But what about verifying that the data layer is firing the correct tags in response to user actions? For example, if a customer adds an item to their cart, you want your data layer to record that event (along with relevant details such as the item name, price, quantity, etc.) and fire a tag to pass the new information to your analytics tools. If the tag doesn’t fire, or fires incorrectly, you could be missing critical data. 

You may be able to trigger certain data layer updates by visiting your site and manually stepping through the relevant actions, or (more efficiently) by running a Simulation Test, but there are some scenarios that are difficult or impossible to test directly. These include: 

  • Edge cases (e.g., out-of-stock products, missing or misformatted product data), 
  • Updates that occur in controlled areas or pages (e.g., order confirmation), or 
  • Updates resulting from workflows that aren’t yet built or stable in production. 

For these cases, you need a way to force the data layer to update so you can verify that the correct tags fire in response. The best way to do this is by including a “Run Script” step in your Simulation Test. In the “add to cart” scenario, for example, your script might look like this:

When the Simulation Test runs, this script will update the data layer with the “addToCart” event and item data — regardless of whether the event was actually completed on the page. 

From there, you can add a Tag Validation to the Run Script step to check that the expected tag fires when the data layer updates:

You can add as many Run Script steps as you need to test variations of the data layer event (e.g., adding multiple products to the cart) or to test related events (e.g., removing an item from the cart). And, of course, you can use similar test configurations to test edge cases and actions that can’t be easily completed on your live site. 

For more information about testing data layer triggered tags, including step-by-step instructions, check out this article in the DataTrue help center. 

Related Blogs

Analytics for PPC post image
5 Analytical Tricks For PPC
If you’re familiar with AdWords and you’re looking to learn some techniques that can help you to more fully analyze your PPC data, this is the guide for you! (more…)
Bad Data Mistakes post image
Ten Most Common Bad Data Mistakes
Hi, in this blog post we are going to talk about some of the most common mistakes that can lead to bad data or a poor analysis of that data. We...
Top 10 Google Analytics Custom Reports for SEO
In this post I’m going to share the top 10 Google Analytics Custom Reports that I use for analyzing SEO data. (more…)