Sub topic 6
Custom Events: When Nothing Predefined Fits
Assume you know the GA4 event model, basic JavaScript, and how to use a GA4 property. This chapter focuses on deciding when to create a custom event and implementing it safely.
A custom event is an event whose name and parameters you define. Use one when GA4 has no automatically collected, enhanced measurement, or recommended event that describes the interaction you need to measure.
When to use a custom event
First compare the interaction with GA4's existing event names. For example, use sign_up for account creation and purchase for a completed transaction. Do not create new_account or order_finished for the same actions. Standard names make reports and integrations easier to understand.
Create a custom event when the action is specific to your product or does not match a recommended event.
| Measurement need | Suitable event |
|---|---|
| A visitor clicks a newsletter button | `newsletter_signup_click` |
| A learner completes a tutorial | `tutorial_complete` |
| A customer completes an order | `purchase` |
A custom event should describe what happened, not how your code handled it. newsletter_signup_click is clearer than footer_button_handler.
flowchart TD
A[Identify the interaction] --> B{Does an existing GA4 event fit?}
B -->|Yes| C[Use the existing or recommended event]
B -->|No| D[Define a custom event]
D --> E[Choose a clear name and parameters]
E --> F[Implement and validate]Design the event
Keep the event name in lowercase snake case. Include enough detail to explain the action, but avoid putting changing values into the name.
Good design:
- Event name:
newsletter_signup_click - Parameter:
placement: "footer" - Parameter:
plan_type: "free"
Avoid names such as newsletter_signup_click_footer, because every placement would require a separate event name. Parameters make the data easier to compare.
GA4 event names can contain letters, numbers, and underscores. They must not begin with a number. Avoid spaces, hyphens, and reserved names. Event names and parameters are case-sensitive, so Plan_Type and plan_type are different values.
Send a custom event with gtag.js
The following call sends an event when a visitor clicks a newsletter sign-up control:
gtag('event', 'newsletter_signup_click', {
placement: 'footer',
plan_type: 'free'
});The first argument identifies the command. The second is the event name. The object contains event parameters. Parameter values should describe the event at the time it occurs.
For a tutorial completion, you might send:
gtag('event', 'tutorial_complete', {
tutorial_id: 'ga4-events-101'
});Do not send personal information, such as email addresses or names, in event parameters. Use stable, non-personal identifiers instead.
Use a data layer with Google Tag Manager
With Google Tag Manager (GTM), your application can push an event into the data layer. A data layer is a JavaScript object that passes information from your site to GTM.
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'tutorial_complete',
tutorial_id: 'ga4-events-101'
});In GTM, create a Custom Event trigger whose event name is tutorial_complete. Then create a GA4 Event tag with the same event name and map the tutorial_id data-layer variable to an event parameter.
The application owns the data-layer push. GTM owns the tag configuration. Keeping those responsibilities separate makes tracking easier to maintain.
Exercise
A product team wants to measure clicks on pricing buttons in the header and footer. Should it create two event names?
Show answer
No. Use one event such as pricing_button_click and add a parameter such as placement with values such as header and footer.