Sub topic 5
Recording Actions as Events
Assume you know basic JavaScript, how a web page loads scripts, and how URLs and social posts work. This chapter uses Google Analytics 4 (GA4), where an event is a recorded user action. A page view, sign-up, purchase, or file download can all be events.
Model actions as events
An event should answer two questions:
- What happened?
- What details make the action useful?
The event name answers the first question. Event parameters answer the second. Parameters are key-value pairs attached to an event, such as method: "email" or value: 25.
Choose names that describe the action, not the page element. For example, use generate_lead instead of blue_button_click. The first name remains useful if the button design changes.
| Action | Event name | Useful parameters |
|---|---|---|
| Newsletter registration | `sign_up` | `method` |
| Contact form submission | `generate_lead` | `form_id`, `lead_type` |
| Completed order | `purchase` | `transaction_id`, `value`, `currency` |
Use GA4 recommended event names when one fits. Recommended names make reports and integrations easier to understand. Keep custom parameters small, stable, and meaningful. Do not send email addresses, phone numbers, or other personally identifying information in event parameters.
Send an event from JavaScript
The GA4 Google tag provides the gtag function. After GA4 has loaded, call it when the action succeeds. For a form, send the event after validation and submission logic confirms success.
document.querySelector('#signup-form').addEventListener('submit', function (event) {
event.preventDefault();
// Your validation and request would run here.
const submissionSucceeded = true;
if (submissionSucceeded) {
gtag('event', 'sign_up', {
method: 'email'
});
}
});This example reports a sign_up event with one parameter. If the form can fail, do not send the success event before you know the submission worked. Otherwise, GA4 will count attempts as completed registrations.
For a purchase, send a stable transaction identifier and the order value. The identifier helps prevent duplicate purchase reporting.
gtag('event', 'purchase', {
transaction_id: 'ORDER-1042',
value: 49.99,
currency: 'USD',
items: [
{
item_id: 'course-101',
item_name: 'Analytics Fundamentals',
price: 49.99,
quantity: 1
}
]
});Use a real order identifier in production. Do not generate a new identifier each time a confirmation page reloads.
Understand the event path
An event moves from the browser to GA4, where its name and parameters become available for analysis.
Common errors
- Sending
sign_upwhen the server rejected the form. - Using different names such as
signup,signUp, anduser_signupfor the same action. - Sending sensitive personal information.
- Sending a purchase more than once after page reloads or retries.
- Using text values where a number is expected, such as
"49.99"instead of49.99. - Assuming an event appears in every report immediately. Processing can take time, while DebugView is intended for near-real-time checks.
Exercise
A lead form displays a success message only after the server returns HTTP 200. When should generate_lead be sent, and which parameter could identify the form?
Show answer
Send it after the successful response, not on the initial click. Include a parameter such as form_id: "demo_request".