Skip to content

Latest commit

 

History

History
96 lines (64 loc) · 3.7 KB

File metadata and controls

96 lines (64 loc) · 3.7 KB
title JavaScript web
sidebarTitle JavaScript web
sidebar Docs
showTitle true
github https://github.com/PostHog/posthog-js
platformLogo javascript
features
eventCapture userIdentification autoCapture sessionRecording featureFlags groupAnalytics surveys llmAnalytics errorTracking
true
true
true
true
true
true
true
false
true

Note: This doc refers to our posthog-js library for use on the browser. For server-side JavaScript, see our Node SDK.

Installation

import JSInstall from '../../integrate/_snippets/install-web.mdx'

Identifying users

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.

Track across marketing website & app

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.com and your app is on app.yourapp.com users 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.

Replay triggers

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.

Opt out of data capture

You can completely opt-out users from data capture. To do this, there are two options:

  1. Opt users out by default by setting opt_out_capturing_by_default to true in your PostHog config.
posthog.init('<ph_project_token>', {
    opt_out_capturing_by_default: true,
});
  1. 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()

Running more than one instance of PostHog at the same time

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.

Development

For instructions on how to run posthog-js locally and setup your development environment, please checkout the README on the posthog-js repository.