.agents/skills/analytics/references/tracking-patterns.md
Use route analytics hooks for tracking when a user visits a page. These fire automatically on route navigation.
Where: Inside the top-level component for a route (the component rendered by React Router).
import {useRouteAnalyticsEventNames} from 'sentry/utils/routeAnalytics/useRouteAnalyticsEventNames';
import {useRouteAnalyticsParams} from 'sentry/utils/routeAnalytics/useRouteAnalyticsParams';
function MyFeaturePage() {
const organization = useOrganization();
// Register the page view event
useRouteAnalyticsEventNames('my_feature.viewed', 'My Feature: Viewed');
// Attach contextual parameters
useRouteAnalyticsParams({
has_data: true,
tab: 'overview',
});
return <div>...</div>;
}
Rules:
useRouteAnalyticsEventNames exactly once per route component.useRouteAnalyticsParams to add context. It can be called multiple times — params merge.useRouteAnalyticsParams must be called within 2 seconds of organization context loading.useRouteAnalyticsParams to add their own params (e.g., trace status, replay availability).trackAnalytics for the same page view.Real-world example (from static/app/views/issueDetails/groupDetails.tsx):
useRouteAnalyticsEventNames('issue_details.viewed', 'Issue Details: Viewed');
useRouteAnalyticsParams({
...getAnalyticsDataForGroup(group),
...getAnalyticsDataForEvent(event),
...getAnalyicsDataForProject(project),
tab,
group_event_type: groupEventType,
});
Use built-in button analytics props for simple click tracking. No event definition required for Reload-only tracking.
<Button
analyticsEventKey="feedback.filter-applied"
analyticsEventName="Feedback: Filter Applied"
analyticsParams={{filter_type: 'status', source: 'sidebar'}}
>
Apply Filter
</Button>
Props:
| Prop | Required | Purpose |
|---|---|---|
analyticsEventKey | Yes | Reload event key (snake_case with dots) |
analyticsEventName | No | Amplitude display name. Omit to skip Amplitude. |
analyticsParams | No | Additional key-value pairs sent with the event |
Rules:
trackAnalytics call in an onClick handler.TrackingContext — the GetSentry override wires it to Reload/Amplitude.trackAnalytics() CallsUse for interactions that aren't button clicks or page views: toggles, drag actions, form submissions, modal opens, etc.
import {trackAnalytics} from 'sentry/utils/analytics';
function handleFilterChange(filterType: string) {
trackAnalytics('feedback.filter-applied', {
organization,
filter_type: filterType,
source: 'list',
});
// ... actual handler logic
}
Rules:
organization (string slug or Organization object).*EventParameters type and registered in the domain event map.trackAnalytics at the point of user action, not in render or effects (unless tracking a "viewed" event).useEffect:useEffect(() => {
trackAnalytics('feedback.banner-viewed', {
organization,
});
}, [organization]);
Use AnalyticsArea to tag events with their UI location. This is useful when the same component appears in multiple contexts.
import {AnalyticsArea, useAnalyticsArea} from 'sentry/components/analyticsArea';
// Wrap a section of UI
<AnalyticsArea name="feedback">
<AnalyticsArea name="details">
<MyComponent />
</AnalyticsArea>
</AnalyticsArea>;
// Use in a component
function MyComponent() {
const area = useAnalyticsArea();
function handleClick() {
trackAnalytics('feedback.action-clicked', {
organization,
area, // "feedback.details"
});
}
}
Rules:
outer.inner.overrideParent to strip the outer area (e.g., for modals that should have their own top-level area).| Situation | Use |
|---|---|
| User navigates to a page | Route analytics hooks |
User clicks a <Button> | Button analytics props |
| User clicks a non-Button element | trackAnalytics() in click handler |
| User submits a form | trackAnalytics() in submit handler |
| Component renders / becomes visible | trackAnalytics() in useEffect |
| Same component used in multiple places | AnalyticsArea + useAnalyticsArea() |
| Feature flag should be tracked | Add flag value as a param to the existing event |