Custom events
Record clicks, sign-ups, and other actions with analytics.track().
Pageviews are recorded by themselves. For other actions, such as a click on a button, a sign-up, or a download, send a custom event from your own code.
Send an event
Call analytics.track() with a name for the action:
analytics.track('signup');
You can add details as properties:
analytics.track('purchase', { plan: 'pro', value: 99 });
Example: count clicks on a button
<button id="download-button">Download</button>
<script>
document.getElementById('download-button').addEventListener('click', function () {
if (window.analytics) {
analytics.track('download');
}
});
</script>
analytics.track() exists only after the tracking
script has loaded. Call it from a click handler or after the page
has loaded, and check for window.analytics first, as
in the example. A call made before the script loads is lost.
Send an event with no code
Add a data-goal attribute to a button or a link. Each
click on it sends an event with that name. Each
data-goal- attribute adds a property:
<button data-goal="signup" data-goal-plan="pro">Start now</button>
This click sends the event signup with the property
plan set to pro. A dash in a property name
becomes an underscore: data-goal-billing-cycle becomes
billing_cycle. An element can have 10
data-goal- properties.
Events that the script sends by itself
| Event | When | Property |
|---|---|---|
outbound_click | A click on a link to another website | url |
file_download | A click on a link to a file, for example a PDF | url |
scroll_depth | The visitor scrolls to 25, 50, 75, or 100 percent of a page. Off until you turn it on. | depth |
You can make a goal for these events too. See Tracking script options to turn them on or off.
Rules for names and properties
- An event name can have up to 100 characters. Use short, clear names such as
signuporcontact_form. - Use the same name each time for the same action.
Signupandsignupare two different events. - Property values must be text, numbers, or true/false. Lists and nested objects are removed.
- Each event can have up to 20 properties. Names can have up to 50 characters, and text values up to 255 characters.
- A number in a property named
valuebecomes the value of a goal.
Do not put personal data, such as names, email addresses, or phone numbers, in event properties.
See your events
- Open Goals in the sidebar, then the Events tab.
- Each event name shows with its count and its unique visitors.
- Click an event to see it over time, the top pages where it happened, and the values of its properties.
To count an event as a conversion, make a goal for it.
Related articles
Still need help? Ask on our feedback board or see all topics.