tessl install tessl/npm-posthog-js@1.335.0PostHog Browser JS Library is a comprehensive browser analytics and feature management SDK that enables developers to capture user events, track product analytics, manage feature flags, record session replays, and implement feedback mechanisms like surveys and conversations in web applications.
The PostHog Sentry integration links error tracking from Sentry with session replays and user context from PostHog, providing a unified view of errors and user behavior.
Creates a Sentry integration instance that connects PostHog with Sentry error tracking.
/**
* Creates a Sentry integration
* Available as both a class and a function
*/
const integration = posthog.sentryIntegration(options?: SentryIntegrationOptions);
// Or as a class
const integration = new posthog.SentryIntegration(options?: SentryIntegrationOptions);Usage Examples:
import * as Sentry from '@sentry/browser';
import posthog from 'posthog-js';
// Initialize PostHog
posthog.init('<posthog-api-key>', {
api_host: 'https://us.i.posthog.com'
});
// Initialize Sentry with PostHog integration
Sentry.init({
dsn: '<sentry-dsn>',
integrations: [
posthog.sentryIntegration({
organization: 'my-org',
projectId: 12345,
severityAllowList: ['error', 'fatal']
})
]
});/**
* Configuration options for Sentry integration
*/
interface SentryIntegrationOptions {
/**
* Sentry organization slug
* Used to generate links to Sentry issues
*/
organization?: string;
/**
* Sentry project ID or slug
* Used to generate links to Sentry issues
*/
projectId?: number | string;
/**
* Custom prefix for self-hosted Sentry
* @default 'https://sentry.io'
* @example 'https://sentry.mycompany.com'
*/
prefix?: string;
/**
* Error severity levels to send to PostHog
* @default ['error']
* @example ['error', 'fatal', 'warning']
*/
severityAllowList?: Array<'fatal' | 'error' | 'warning' | 'info' | 'debug'>;
/**
* Whether to send exceptions to PostHog as well
* @default false
*/
sendExceptionsToPostHog?: boolean;
}When an error occurs in Sentry, the integration:
When viewing session replays in PostHog:
import * as Sentry from '@sentry/react';
import posthog from 'posthog-js';
// Initialize PostHog first
posthog.init('<posthog-key>');
// Then initialize Sentry with the integration
Sentry.init({
dsn: '<sentry-dsn>',
integrations: [
posthog.sentryIntegration()
]
});Sentry.init({
dsn: '<sentry-dsn>',
integrations: [
posthog.sentryIntegration({
organization: 'my-company',
projectId: 12345
})
]
});Sentry.init({
dsn: '<sentry-dsn>',
integrations: [
posthog.sentryIntegration({
organization: 'my-company',
projectId: 'my-project',
prefix: 'https://sentry.mycompany.com'
})
]
});Sentry.init({
dsn: '<sentry-dsn>',
integrations: [
posthog.sentryIntegration({
// Only send critical errors to PostHog
severityAllowList: ['error', 'fatal'],
sendExceptionsToPostHog: true
})
]
});Always initialize PostHog before Sentry:
// ✅ Correct order
posthog.init('<posthog-key>');
Sentry.init({
dsn: '<sentry-dsn>',
integrations: [posthog.sentryIntegration()]
});
// ❌ Wrong order - integration won't work properly
Sentry.init({
dsn: '<sentry-dsn>',
integrations: [posthog.sentryIntegration()]
});
posthog.init('<posthog-key>');Identify users in PostHog to see user context in Sentry:
// Identify user in PostHog
posthog.identify('user-123', {
email: 'user@example.com',
name: 'John Doe'
});
// Sentry will automatically receive this contextFilter errors sent to PostHog based on severity:
posthog.sentryIntegration({
// Only critical errors
severityAllowList: ['error', 'fatal']
})
// Or include warnings
posthog.sentryIntegration({
severityAllowList: ['warning', 'error', 'fatal']
})// Only integrate in production
const sentryIntegrations = [
// ... other integrations
];
if (process.env.NODE_ENV === 'production') {
sentryIntegrations.push(posthog.sentryIntegration({
organization: 'my-org',
projectId: 12345
}));
}
Sentry.init({
dsn: '<sentry-dsn>',
integrations: sentryIntegrations
});// Use PostHog properties for better error grouping
Sentry.init({
dsn: '<sentry-dsn>',
integrations: [posthog.sentryIntegration()],
beforeSend(event) {
// Add PostHog context to Sentry events
event.tags = {
...event.tags,
posthog_distinct_id: posthog.get_distinct_id(),
posthog_session_id: posthog.get_session_id()
};
return event;
}
});// Track error recovery in PostHog
Sentry.init({
dsn: '<sentry-dsn>',
integrations: [posthog.sentryIntegration()],
beforeSend(event, hint) {
// Capture error recovery attempts
posthog.capture('error_occurred', {
error_type: event.exception?.values?.[0]?.type,
error_message: event.exception?.values?.[0]?.value,
sentry_event_id: event.event_id
});
return event;
}
});sendExceptionsToPostHog: true