SensorsData JavaScript SDK

1.27.11 · active · verified Sun Apr 19

The `sa-sdk-javascript` is the official client-side SDK for SensorsData, a leading user behavior analytics platform. It facilitates comprehensive data collection from web applications, supporting various tracking methods including code-based, full-stack, and visual full-stack event tracking. The current stable version is 1.27.11, with frequent patch releases addressing bug fixes, performance improvements, and feature additions, as evidenced by its rapid iteration cadence. Key differentiators include its status as an open-source commercial SDK, robust support for diverse data collection strategies, and proven stability across a large customer base. The SDK is designed to help businesses implement data-driven strategies by providing reliable user behavior insights.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the SensorsData SDK, track a custom event, set user profile properties, and log in a user, showcasing fundamental data collection patterns.

import sensorsdata from 'sa-sdk-javascript';

// Initialize the SDK with your SensorsData project configuration
sensorsdata.init({
  // Replace with your SensorsData data collection server URL
  serverUrl: process.env.SENSORS_DATA_SERVER_URL ?? 'https://your-sensorsdata-server.com/sa?project=your_project',
  // Enable debug mode to see logs in the console (useful during development)
  show_log: true,
  // Automatically track page views when the URL changes
  autoTrack: {
    pageview: true
  },
  // Enable heatmap functionality (requires server-side configuration)
  heatmap: {
    clickmap: 'default',
    scroll_nickname: 'default'
  },
  // Use IndexedDB for local data persistence before sending
  use_indexeddb: true,
  // Configure specific plugin options, e.g., for user attributes
  preset_properties: {
    // Example: Capture the user's browser language on first visit
    '$browser_language': navigator.language
  }
});

// Track a custom event, e.g., 'UserSignup'
sensorsdata.track('UserSignup', {
  signup_method: 'Email',
  signup_time: new Date().toISOString()
});

// Set or update user profile properties
sensorsdata.setProfile({
  email: 'user@example.com',
  account_type: 'Premium'
});

// Log in a user (associates anonymous data with a known user ID)
sensorsdata.login('unique_user_id_123');

// Once logged in, track another event for the identified user
sensorsdata.track('ProductPurchased', {
  product_id: 'SKU789',
  price: 99.99,
  currency: 'USD'
});

console.log('SensorsData SDK initialized and events tracked.');

view raw JSON →