Sub topic 12
Custom events and custom dimensions
This chapter covers sending your own events to GA4 and making their parameters reportable. It assumes you have a GA4 property with a web data stream, you can read the Events report, and you know the four event categories from the earlier chapter. Basic JavaScript helps for the code examples.
Why custom events exist
GA4 collects automatic events, enhanced measurement events, and recommended events. Sometimes none of them describes what you want to measure. A newsletter signup in a footer, a pricing calculator being used, or a video on your own player reaching 50% are examples. For these you define a custom event: an event with a name you choose, sent by your own code.
Before building one, check the other categories first. If a recommended event such as sign_up or generate_lead fits, use it. Recommended events have agreed names and parameters, which makes your data easier to compare and to use in features that expect those names.
Sending a custom event
If your pages already load gtag.js, one call is enough:
// Send a custom event with a parameter from a page that already loads gtag.js
gtag('event', 'newsletter_signup', {
placement: 'footer'
});The first argument, 'event', tells gtag.js you are sending an event. The second is the event name. The third is an object of event parameters, which are extra details attached to that one event.
After GA4 receives the event, newsletter_signup appears by name in the Events report. Standard reports can already show its event count and the users who triggered it. The placement parameter is collected but is not yet available as a dimension in reports. That requires registration, covered below.
Naming rules and limits
Poor names are hard to fix later, because you cannot rename an event that has already been collected. Follow these rules:
- Use letters, numbers, and underscores. Start with a letter.
- Event names are case-sensitive.
Newsletter_Signupandnewsletter_signupare two different events. - Use
snake_caseconsistently, as the automatic and recommended events do. - Do not use reserved prefixes such as
google_,ga_, andfirebase_. Do not reuse names of automatic events. - Keep names under 40 characters and parameter names under 40 characters. Parameter values can be up to 100 characters on standard properties.
A standard property accepts up to 500 distinct event names per app instance or web stream. A name you generate dynamically, such as one that includes a product ID, can use up that budget quickly. Put variable details in a parameter, not in the name.
Event or parameter?
A common design question is whether to create many events or one event with a parameter. Compare these two designs for tracking button clicks:
| Design | Example | Result |
|---|---|---|
| One event per button | `click_pricing_button`, `click_demo_button` | Many names, and a risk of reaching the event name limit |
| One event, a parameter for the button | `cta_click` with `button_name: 'pricing'` | One name, and the button is a reportable dimension |
The second design is usually better. Use separate event names when the actions mean different things, such as newsletter_signup and file_download. Use parameters when the action is the same and only its details differ.
From parameter to custom dimension
The Events report works on event names. To break a report down by a parameter, such as placement, you must register it. A custom dimension is a parameter you have told GA4 to turn into a dimension for reporting.
There are two scopes you will meet most often:
- Event scope: the value applies to the single event that carried it. Use it for parameters like
placementorbutton_name. - User scope: the value is tied to a user and persists across their events. It is set with user properties, not event parameters. Use it for something like
plan_type.
Item scope also exists for ecommerce item parameters, but it is outside this chapter.
Registering an event-scoped dimension
- In the Admin area, open the property and choose Custom definitions under Data display.
- Select Create custom dimension.
- Enter a Dimension name, which is the label shown in reports, such as
Placement. - Set Scope to Event.
- In Event parameter, choose or type the exact parameter name,
placement. - Save.
The parameter name must match exactly, including case. If the parameter has not been received yet, you may need to type it by hand because the dropdown only lists names GA4 has already seen.
After registration, it can take up to 24 hours for the dimension to appear in standard reports and Explorations, though it often takes less. Real-time reports can show it sooner in some cases.
Quotas
Standard properties allow a limited number of custom definitions. At the time of writing, this is 50 event-scoped custom dimensions, 25 user-scoped custom dimensions, and 50 custom metrics. GA4 360 properties have higher limits. Check Google's current documentation, because limits change.
Because the quota is small, register only parameters that you will actually report on. Registering one is not needed for the parameter to be collected, and unregistered parameters are still exported to BigQuery if you use that link.
High cardinality
Cardinality is the number of distinct values a dimension has. A parameter like page_title has moderate cardinality. A parameter holding a timestamp or user ID has very high cardinality. When a dimension has too many distinct values in a report, GA4 groups the overflow into an (other) row. Avoid registering parameters with unbounded values. They also tend to fill reports with rows that tell you nothing.
Using the dimension in reports
Once registered and populated, the dimension is available in several places:
- In Explorations, add it as a dimension next to
Event nameandEvent count. - In the standard Events report, you can add it as a secondary dimension in some views, or build a custom report from it.
- In the Data API, it is addressed as
customEvent:placement.
The API name always has the form customEvent: followed by the parameter name. A user-scoped dimension uses customUser: followed by the property name.
The next example requests the signup count by placement:
{
"dateRanges": [{ "startDate": "28daysAgo", "endDate": "yesterday" }],
"dimensions": [
{ "name": "eventName" },
{ "name": "customEvent:placement" }
],
"metrics": [{ "name": "eventCount" }],
"dimensionFilter": {
"filter": {
"fieldName": "eventName",
"stringFilter": { "matchType": "EXACT", "value": "newsletter_signup" }
}
}
}Send this body to the properties/PROPERTY_ID:runReport method, replacing PROPERTY_ID with your numeric property ID. If the request returns an error that the dimension is not valid, the usual causes are a misspelled parameter, a dimension not yet registered, or one registered under a different scope.
The overall flow
Combining custom events with key events
A custom event is often the thing you want to count as a goal. In the Events report, each event row has a Mark as key event toggle. Turning it on for newsletter_signup means GA4 counts it as a key event from then on. Key events then appear in acquisition and other reports as a metric. Like registration, this does not rewrite earlier data.
Common mistakes
- Typos and case differences.
newsletterSignupandnewsletter_signupbecome separate events. Agree on names before release and document them. - Assuming parameters are automatically reportable. A parameter that is not registered will not appear as a dimension. This is the most common reason a custom dimension looks empty.
- Registering with the wrong scope. A user-scoped dimension will not read an event parameter, and the reverse is also true.
- Putting personal data in parameters. Google's policies prohibit sending personally identifiable information, such as email addresses, to GA4. Use a non-identifying value like
placement: 'footer'. - Creating too many names. Hitting the 500-name limit stops new names being recorded. Use parameters for variation.
- Testing in production only. Use DebugView, enabled with debug mode, to confirm that the event name and parameters arrive correctly before you rely on them.
Exercises
Exercise
You added gtag('event', 'file_download_click', { file_type: 'pdf' }) three weeks ago. Today you register file_type as an event-scoped custom dimension. Will the report show pdf for the clicks from the past three weeks?
Show answer
No. Custom dimensions are not retroactive. The dimension fills only with events collected after registration, so the earlier clicks will not carry a file_type value in reports, even though the parameter was sent.
Exercise
You need to track clicks on seven different buttons. Should you create seven events or one event, and how would you report on each button?
Show answer
Prefer one event, such as cta_click, with a button_name parameter. Register button_name as an event-scoped custom dimension, then break eventCount down by it. This saves event-name budget and keeps the data structured. In the Data API, the dimension is customEvent:button_name.