LogRocket JavaScript SDK

12.1.0 · active · verified Sun Apr 19

LogRocket's JavaScript SDK, currently at version 12.1.0, provides a comprehensive solution for front-end monitoring, session replay, and product analytics. It allows development and product teams to reproduce user issues by replaying sessions pixel-perfectly, alongside integrated technical context such as console logs, network requests, and Redux state changes. This SDK differentiates itself by its strong focus on engineering-grade debugging, offering detailed diagnostic insights into client-side errors and performance issues. While a fixed release cadence isn't explicitly published, the package undergoes regular updates with new features and improvements. A key feature is its ability to automatically detect user frustration signals like rage clicks and dead clicks, and its robust data privacy features include default obfuscation of sensitive information, with configurable options for granular control over what data is collected and sent to the servers. It integrates with various tools like Sentry, Jira, and Google Analytics to streamline developer workflows and provide a holistic view of user experience.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize LogRocket, identify a user with custom traits, integrate the Redux middleware, and manually log errors and track custom events. It highlights privacy configuration for network requests.

import LogRocket from 'logrocket';
import { reduxMiddleware } from '@logrocket/redux';
import { createStore, applyMiddleware, combineReducers } from 'redux';

interface AppState {
  counter: number;
  message: string;
}

const counterReducer = (state = 0, action: { type: string }) => {
  switch (action.type) {
    case 'INCREMENT': return state + 1;
    case 'DECREMENT': return state - 1;
    default: return state;
  }
};

const messageReducer = (state = '', action: { type: string, payload?: string }) => {
  switch (action.type) {
    case 'SET_MESSAGE': return action.payload || '';
    default: return state;
  }
};

const rootReducer = combineReducers<AppState>({
  counter: counterReducer,
  message: messageReducer
});

// Replace with your actual LogRocket App ID
const LOGROCKET_APP_ID = process.env.LOGROCKET_APP_ID || 'your/app-id';

// Initialize LogRocket
LogRocket.init(LOGROCKET_APP_ID, {
  // Optional: Configure specific options
  console: {
    isEnabled: true,
    shouldAggregateConsoleErrors: true,
  },
  network: {
    isEnabled: true,
    requestSanitizer: (request) => {
      // Redact sensitive headers, e.g., Authorization
      if (request.headers?.Authorization) {
        request.headers.Authorization = 'Bearer [Redacted]';
      }
      return request;
    },
  },
  dom: {
    // Optional: Redact elements containing sensitive data
    // For example, an element with data-lr-hide will not be recorded
    baseHref: '/', // Specify if your app uses a base href
  }
});

// Identify the user after initialization
LogRocket.identify('unique-user-id-123', {
  name: 'Jane Doe',
  email: 'jane.doe@example.com',
  subscriptionType: 'premium',
  // Custom user traits for filtering sessions
  'user-type': 'admin'
});

// Integrate Redux middleware (must be after LogRocket.init)
const store = createStore(rootReducer, applyMiddleware(reduxMiddleware));

// Simulate some actions
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'SET_MESSAGE', payload: 'User clicked increment twice' });

try {
  throw new Error('This is a test error to see in LogRocket!');
} catch (error) {
  LogRocket.error(error);
}

console.log('LogRocket initialized and user identified. Check your dashboard for the session.');

// Example of sending a custom event
LogRocket.track('Product View', { productId: 'ABC-123', category: 'Electronics' });

// This is client-side code, typically run in a browser environment.
// Ensure to include your actual LogRocket App ID from your project settings.

view raw JSON →