Actions

Last updated:

Often a single event is not specific enough to represent a behavior. For example, to track signups from Google Ads, a pageview or button click isn't enough. You need a valid signup submission where the user has properties showing they came from a Google Ad.

Also, multiple events might represent the same behavior. For example, "trying a feature" could include visiting its page, enabling it, or generating a related backend event.

Actions are a collection of events fitting event types and filters that represent a single behavior. They are created using a combination of autocapture events, custom events, or pageviews, along with filters. They can then be analyzed in insights, dashboards, and more.

You can create and view actions by going to "Data Management" in the left-hand navigation sidebar, and then click on the "Actions" tab.

How to create an action

Actions can be created by either:

  1. Clicking the "New action" button in the Actions tab, or
  2. Using the PostHog Toolbar to tag the elements that are relevant. We have a step-by-step tutorial on creating actions using the toolbar.

Note: Actions are retroactive, meaning they also apply to past events, not only events that happen after the action was created.

You can create actions using events from three sources:

1. Autocapture

Autocapture actions are based on frontend elements from your website, such as a button or an input. The easiest way to select them is using the toolbar. However, you can also set them manually if you wish by providing something to identify the element, like a selector.

Our autocapture functionality captures a lot of frontend elements by default, but you'll need to manually set anything else you want to be captured.

Currently, autocapture captures any click or change of input or submission of a, button, form, input, select, textarea, and label tags. All other elements need to be set manually. This important to note if you deviate from established HTML practices, such as using a div as a button.

Autocapture is also conservative regarding input tags to prevent grabbing sensitive data. See Autocapture for more details.

There are three ways to match your elements:

  1. Text: The text on the element, if applicable. If you have buttons across your site, all saying ‘Sign Up’, you can track ALL of them, site-wide, as the same action, by choosing Text: "Sign Up" and not matching the URL.

  2. Only match if URL contains: The URL where this action needs to take place. If you choose to match the URL as well, it will track any button on that page if it contains the ‘Sign Up’ text (which could be more than one button).

  3. Matching selectors: details below.

You can have more than one match type selected. Having multiple fields selected is an AND statement, so all of them will need to match an event for it to count as the action.

Matching selectors

You can use standard basic CSS selectors to uniquely identify the elements you care about.

The following types of selectors are supported:

  • Recommended: Attribute selectors. Example:[data-attr="value"] will match elements that have the given attribute and value.
  • ID selectors. Example: #speciallink will match elements with ID "speciallink".
  • Class selectors. Example: .importantlink will match elements with CSS class "importantlink".
  • Type selectors. Example: input will match any<input> elements.
  • Combinators are also supported (e.g. descendant or child combinators). Learn more about combinators. Examples: form > [data-attr="submit"] or button span.

The following patterns are not currently supported:

  • Wildcard operators (e.g. *, ~, |)
  • Combining multiple attribute selectors
  • Non-alphanumeric classes (like clases containing -, [], .)

2. Page views

Actions can be creating using page view events that match urls containing a string, based on a regex or match exactly.

Regex matching is commonly used for URLs containing IDs like /some/path/123/view, which could be matched by a regex like /some/path/\d+/view into a single action.

3. Custom events

Custom events can be sent to your PostHog instance via our API, or one of our SDKs.

It is possible for an action to match multiple events. To do this, click Add another match group when you are creating your action. Actions with multiple events work based on OR operations. That means that an action like "Clicked Read More Button" OR "Clicked More Information Button" will trigger as soon as the user clicks either of the buttons. Both clicks are not required for the action.

Further reading

Want to know more about what's possible with Actions in PostHog? Try these tutorials:

Questions?

Was this page useful?

Next article

Annotations

Annotations enable you to add written notes to a particular day or time on all your insights and dashboards so you can see how changes or incidents impact your metrics. You can use annotations to mark things like feature updates, version releases, and marketing campaigns, which adds helpful context when you're looking into what might have caused a change in your metrics. Why Annotations are useful The chart below shows a large spike in website traffic, which has been annotated to explain why…

Read next article