| title | JavaScript web | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| sidebarTitle | JavaScript web | ||||||||||||||||||
| sidebar | Docs | ||||||||||||||||||
| showTitle | true | ||||||||||||||||||
| github | https://github.com/PostHog/posthog-js | ||||||||||||||||||
| platformLogo | javascript | ||||||||||||||||||
| features |
|
Note: This doc refers to our posthog-js library for use on the browser. For server-side JavaScript, see our Node SDK.
import JSInstall from '../../integrate/_snippets/install-web.mdx'
import IdentifyFrontendCallout from '../../_snippets/identify-frontend-callout.mdx'
Once you've installed PostHog, see our features doc for more information about what you can do with it. You can also install the PostHog VS Code extension to see live analytics, flag status, and session replay links inline in your code.
We recommend putting PostHog both on your homepage and your application if applicable. That means you'll be able to follow a user from the moment they come onto your website, all the way through signup and actually using your product.
PostHog automatically sets a cross-domain cookie, so if your website is
yourapp.comand your app is onapp.yourapp.comusers will be followed when they go from one to the other. See our tutorial on cross-website tracking if you need to track users across different domains.
You can configure "replay triggers" in your project settings. You can configure triggers to enable or pause session recording when the user visit a page that matches the URL(s) you configure.
You are also able to setup "event triggers". Session recording will be started immediately before PostHog queues any of these events to be sent to the backend.
You can completely opt-out users from data capture. To do this, there are two options:
- Opt users out by default by setting
opt_out_capturing_by_defaulttotruein your PostHog config.
posthog.init('<ph_project_token>', {
opt_out_capturing_by_default: true,
});
- Opt users out on a per-person basis by calling
posthog.opt_out_capturing().
Similarly, you can opt users in:
posthog.opt_in_capturing()
To check if a user is opted out:
posthog.has_opted_out_capturing()
While not a first-class citizen, PostHog allows you to run more than one instance of PostHog at the same time if you, for example, want to track different events in different posthog instances/projects.
posthog.init accepts a third parameter that can be used to create named instances.
posthog.init('<ph_project_token>', {}, 'project1')
posthog.init('<ph_project_token>', {}, 'project2')You can then call these different instances by accessing it on the global posthog object
posthog.project1.capture('some_event')
posthog.project2.capture('other_event')Note: You'll probably want to disable autocapture (and some other events) to avoid them from being sent to both instances. Check all of our config options to better understand that.
For instructions on how to run posthog-js locally and setup your development environment, please checkout the README on the posthog-js repository.