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

EventWhenProperty
outbound_clickA click on a link to another websiteurl
file_downloadA click on a link to a file, for example a PDFurl
scroll_depthThe 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 signup or contact_form.
  • Use the same name each time for the same action. Signup and signup are 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 value becomes the value of a goal.

Do not put personal data, such as names, email addresses, or phone numbers, in event properties.

See your events

  1. Open Goals in the sidebar, then the Events tab.
  2. Each event name shows with its count and its unique visitors.
  3. 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.