Tracking Custom Events
Beyond automatic page view tracking, Gooddata allows you to track custom interactions that are important to your business. This is essential for tracking conversions, button clicks, form submissions, and other critical user behaviors.
The track Method
To track a custom event, use the gooddata.track() method. You can call this anywhere in your frontend code when an interaction occurs.
// Track a simple event
if (window.gooddata) {
window.gooddata.track("signup_button_clicked");
}
Adding Metadata
You can attach optional metadata to your events to provide more context. This is useful for segmenting events later.
if (window.gooddata) {
window.gooddata.track("purchase_completed", {
revenue: 49.99,
currency: "USD",
planType: "Pro"
});
}
Common Use Cases
Tracking Conversions
If you want to track when a user successfully checks out, you might add a track call to your success page or within your checkout complete handler.
Tracking External Links
To track when users leave your site by clicking an outbound link:
<a href="https://twitter.com/yourbrand" onClick="window.gooddata?.track('outbound_twitter')">
Follow us on Twitter
</a>
Form Submissions
Track when a user successfully submits a contact or lead generation form:
async function handleSubmit(event) {
event.preventDefault();
// ... process form
window.gooddata?.track("lead_captured", { source: "contact_form" });
}