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.