'Define Google Analytics 4 custom dimensions via API
Is there a way to programmatically define custom dimensions using the Google Analytics Admin API (that works with GA4)?
I'm talking about defining the dimensions ahead of time so I can send them along with events, which is what you'd manually do in the Google Analytics user interface (under All events > Manage Custom Definitions). This is what I'd like to do programmatically. Once they're defined, sending these custom dimensions along with tracking events is easy enough (using gtag).
Apparently, this was possible in the previous version of the API, but that API says that it "does not support GA4 properties".
Solution 1:[1]
You already can programmatically define/create custom dimensions and metrics with the Google Analytics Admin API for GA4 properties.
Custom dimensions
Can be created with this method providing:
- description
- displayName
- parameterName (it hasn't need to exist beforehand)
- scope (User or Event)
Custom metrics
Can be created with this method providing:
- description
- displayName
- measurementUnit
- restrictedMetricType
- scope (Metric_Scope_unspecified or Event)
Warning from Google: This is an early preview version of the API and is subject to change. While we will try to notify you of upcoming changes, you should expect to encounter breaking changes before the APIs are publicly released.
Solution 2:[2]
Jan,
You are correct that the Google Analytics Management API V3 cannot be used to configure GA4 properties. The Analytics Admin API V1, which supports GA4, does not currently have methods to create custom metrics/dimensions.
I will update this question once we have more information on when this feature can become available.
Thanks, Ilya
The Google Analytics Team
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ilya Kuleshov |
