1

I'm using Google Tag Manager (GTM) to integrate Google Analytics 4 (GA4) into my web project. My setup involves dynamic event tracking using custom variables like {{event}}, {{custom_trigger}}, and {{event_data}} to automatically capture a wide range of events and associated data.

One of the variables I'm pushing to the dataLayer is event_data, which can sometimes be a nested object. For example:

dataLayer.push({
  event: "custom_event",
  event_data: {
    user: {
      id: "123",
      type: "premium"
    },
    action: "clicked"
  }
});

In GTM, I'm sending this event_data to GA4 via a custom event tag. However, when it reaches GA4, any nested properties (like user) show up as [object Object] instead of retaining their actual structure.

To work around this, I tried using JSON.stringify(event_data) in GTM before sending the data. This works in the sense that it preserves the full structure, but GA4 then stores it as a string, not as a structured object — making it difficult to query individual nested fields in GA4 reports or BigQuery.

What I'm looking for: A way to send a nested object from GTM to GA4 such that:

It retains its nested structure.

It's not flattened, and also not stored as a single string.

The nested properties are individually accessible in GA4 or BigQuery.

Additional Info: I understand GA4's schema prefers flat parameters, but in some cases (like ecommerce or user traits), nested objects could be very helpful.

I’m using standard GA4 event tags in GTM with dynamic fields mapped using variables.

Is there a recommended pattern for flattening objects automatically before sending to GA4? Or an approach that GA4 supports for handling nested data structures?

Any suggestions or best practices would be appreciated.

1 Answer 1

0

Quick answer is you don't do it. GA doesn't allow objects. And that's very expected given how they store the data, so no surprise there at all.

No best practice to auto-flattening objects. Your view is skewed, it's not a good idea for an analytics system to just consume randomly structured objects. GA allows objects when it's strictly structured like in ecommerce tracking. Because vast majority of ecommerce is similar enough to be fit in static syntax.

Now regarding best practices:

  1. You carefully plan the custom dimensions in your GA, paying close attention to your limits

  2. You go to GTM, and write logic there to parse the data you send via FE into the dimensions you've planned out in GA.

  3. You go to your front-end and supply GTM with the objects that you structure appropriately so GTM can parse them into dimensions. Usually you use dataLayer for sending the data to GTM.

Stringifying objects and shoving them into one dimension still can be valuable if you watch its length and then export the GA data to BQ, and then in BQ you parse them out into separate dimensions or export further and query json.

Given how GA is strictly limited, we often store multiple dimensions in some custom dimensions. We wouldn't typically stringify an object since that would take too much of precious space. We would instead go with bitmaps or similar solutions, encoding values to ensure the length. It wouldn't be easy to use in GA4 (though still possible by using filter regexes in explorations), but most advanced users of GA never use GA's interface for analysis. It's pretty weak. They would typically ETL the data elsewhere and then use a proper BI tool.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.