When to use a Nested Data Layer vs Flat Data Layer with GTM and GA4

As businesses collect more and more GA4 data, the structure in which that data is rendered becomes increasingly important.

When to use a Nested Data Layer vs Flat Data Layer with GTM and GA4
Written by
Matt Bentley
Published on
June 7, 2023
Category
Data

As businesses collect more and more Google Analytics 4 (GA4) data, the structure in which that data is rendered for collection from the user-facing front-end platform becomes increasingly important. Two common approaches to organising data on the front-end are a structured / nested data layer and a flat data layer. Each approach has its own advantages and disadvantages.

But first what is a Data Layer?

There are a few definitions depending on the context you're discussing the data layer in (e.g. when discussing it in conversations about team structure and resourcing, a data layer is a mechanism for dividing roles, responsibilities and resource), but the definition that applies to the context of this article is:

A data layer is a centralised repository of information that organises data in a structured format, most commonly a JavaScript object (though it can also apply to platforms that do not support JavaScript) with key value pairs, making it easy to access and manage the information on the user-facing front-end platform.  

The data layer is implemented by developers but designed and managed by data architects or data engineers, who ensure that the data is properly structured and formatted.

As implied, it acts as a 'layer' between data sources and applications such as websites or apps, allowing for efficient data collection and use.

Tag Managers such as Google Tag Manager (GTM) which is frequently used with GA4, will typically read from a data layer before enrichment and sending off to various 3rd party analytics and MarTech tags.  

Critical for large complex platform, a data layer enables scalability, efficient data collection, data standardisation, data security & compliance, and can help businesses make better decisions, improve efficiency (both development and data collection), and deliver better customer experiences.

Structured / Nested Data Layer

A structured nested data layer is where data is organised into a hierarchical structure with use of arrays. This structure can be thought of as a tree, with each node representing a level of the hierarchy. The nodes are connected to one another in a way that represents the relationships between the data they contain.

Note, for a Product Listing Page called 'Kitchen Products', the data layer has 2 sections, one for the page related data, and a second for the product items on display, in this case there are 2 products.

Pros:  

  1. Organisation: A structured / nested data layer is very organised, with data arranged in a logical hierarchy. This makes it easy to find and understand relationships between / context of different data points.
  2. Scalability: This type of data storage is scalable, which means it can handle large amounts of data without any significant slowdown in performance. This is particularly useful for businesses that need to store and manage large amounts of data. It can also be more easily tied into the content and structure of the platform, allowing for an easier and more scalable development process, leading to more consistent and accurate data.
  3. Flexibility: A structured / nested data layer can be adapted to meet the changing needs of a business. As new data is added, it can be easily integrated into the existing hierarchy.

Cons:  

  1. Complexity: Setting up a structured nested data layer can be complex, particularly for businesses that have not used this approach before. This can require significant investment in terms of time and resources.
  2. Maintenance: As the hierarchy grows and changes, updating the structure can become complex and challenging. This can require significant effort to keep the structure functional and adhere to the core underlying principles through regular iterative changes.
  3. End use: Querying a structured / nested data layer can be more complex than querying a flat data layer for the users of your tag manager. This requires careful planning for the set up of the tag manager, and if handled poorly, can result in slower query performance (particularly for complex queries), opening up the possibility of race conditions, lost data and wider impacts.

Flat Data Layer

A flat data layer is where the data is organised into a single level with each row representing a single field of data. The data is stored in a way that allows for easy retrieval of individual records.

For the same page as before, the keys are all at the same level, however, this time the multiple products are listed as arrays in the same key. So now you need to use an 'index' in GTM to read the position of a specific product in the product name and matching product id arrays, or just loop through to read all the products displayed.

Pros:  

  1. Simplicity: A flat data layer is very simple and straightforward, with all data stored in a simple hierarchy. This makes it easy to set up and use, particularly for businesses that are new to data storage.
  2. Query Performance: Querying a flat data layer is typically faster than querying a structured nested data layer, particularly for simple queries.
  3. Maintenance: Maintenance of a flat data layer is relatively simple, with little need for complex hierarchy maintenance.

 Cons:

  1. Limited Organisation: A flat data layer lacks the organisational structure of a structured nested data layer. This can make it difficult to understand relationships between different types of data.
  2. Limited Scalability: A flat data layer can become less efficient as the amount of data it contains grows. This can make it less effective for businesses that need to store and manage large amounts of data. Additionally, as data requirements increase, the flat data layer becomes more and more complicated and harder to maintain - individual line items can increase exponentially to mitigate for the lack of structure
  3. Limited Flexibility: A flat data layer can be less adaptable to changing business needs. As new types of data are added, it can become difficult to integrate them into the existing table structure.

Let's take a look back

Both a structured nested data layer and a flat data layer have their own unique strengths and weaknesses. A structured nested data layer is highly organised and scalable, but can be complex to set up and maintain. A flat data layer is simple and easy to use, but lacks the organisational structure and scalability of a structured nested data layer.  

Ultimately, the decision of which approach to use will depend on the specific needs of a business and the types of data it needs to store and manage.

If your business needs to transform your GTM data layer for GA4 as it has outgrown its purpose get in touch with us at Loop Horizon!

Looking for more articles like this? Follow us on LinkedIn so you never miss a post.