Once your PostHog instance is up and running, the next step is to start sending events.
By default, PostHog automatically captures pageviews and pageleaves as well as clicks, change of inputs, and form submissions associated with a
, button
, form
, input
, select
, textarea
, and label
tags. See our autocapture docs for more details on this.
If you prefer to disable or filter these, set the appropriate values in your configuration options.
Custom event capture
You can send custom events using capture
:
posthog.capture('user_signed_up');
Tip: We recommend using a
[object] [verb]
format for your event names, where[object]
is the entity that the behavior relates to, and[verb]
is the behavior itself. For example,project created
,user signed up
, orinvite sent
.
Setting event properties
Optionally, you can include additional information with the event by including a properties object:
posthog.capture('user_signed_up', {login_type: "email",is_free_trial: true})
Manually capturing pageviews and pageleaves in single-page apps
PostHog automatically captures $pageview
events on page load and $pageleave
events on page unload. If you have a single-page app, that means it only sends pageview and pageleave once (when your app loads and when users leave).
To make sure any navigating a user does within your app gets captured, you can make pageview and pageleave calls manually like this:
// Capture pageviewposthog.capture('$pageview')// Capture pageleaveposthog.capture('$pageleave')
This automatically sends the current URL along with other autocaptured properties like the referrer, OS, scroll depth, and more. See our guide to tracking pageviews in single-page apps for more details.
Event ingestion
It's a priority for us that events are fully processed and saved as soon as possible. Typically, events will be usable in queries within a few minutes.
Advanced: Anonymous vs identified events
PostHog captures two types of events: anonymous and identified
Identified events enable you to attribute events to specific users, and attach person properties. They're best suited for logged-in users.
Scenarios where you want to capture identified events are:
- Tracking logged-in users in B2B and B2C SaaS apps
- Doing user segmented product analysis
- Growth and marketing teams wanting to analyze the complete conversion lifecycle
Anonymous events are events without individually identifiable data. They're best suited for web analytics or apps where users aren't logged in.
Scenarios where you want to capture anonymous events are:
- Tracking a marketing website
- Content-focused sites
- B2C apps where users don't sign up or log in
Under the hood, the key difference between identified and anonymous events is that for identified events we create a person profile for the user, whereas for anonymous events we do not.
💡 Tip: Under our current pricing, anonymous events can be up to 4x cheaper than identified ones (due to the cost of processing them), so it's recommended you only capture identified events when needed.
How to capture anonymous events
The JavaScript Web SDK captures anonymous events by default. However, this may change depending on your person_profiles
config when initializing PostHog:
person_profiles: 'identified_only'
(recommended) (default) - Anonymous events are captured by default. PostHog only captures identified events for users where person profiles have already been created.person_profiles: 'always'
- Capture identified events for all events.
For example:
posthog.init('<ph_project_api_key>',{api_host: 'https://us.i.posthog.com',person_profiles: 'always'})
How to capture identified events
If you've set the personProfiles
config to IDENTIFIED_ONLY
(the default option), anonymous events are captured by default. To capture identified events, call any of the following functions:
identify()
alias()
group()
setPersonProperties()
setPersonPropertiesForFlags()
setGroupPropertiesForFlags()
When you call any of these functions, it creates a person profile for the user. Once this profile is created, all subsequent events for this user will be captured as identified events.
Alternatively, you can set personProfiles
to ALWAYS
to capture identified events by default.