Skip to main content

User Identification

Identify users to track their activity across sessions.

Browser

// Identify with user ID only
analytics.identify('user@example.com')

// Identify with traits
analytics.identify('user@example.com', {
  first_name: 'John',
  last_name: 'Doe',
  email: 'user@example.com',
  plan: 'premium',
  created_at: new Date().toISOString()
})

// With callback
analytics.identify('user@example.com', {
  email: 'user@example.com'
}, () => {
  console.log('User identified successfully')
})

Node.js

// Identify with user ID and traits
analytics.identify({
  userId: 'user@example.com',
  traits: {
    first_name: 'John',
    last_name: 'Doe',
    email: 'user@example.com',
    plan: 'premium'
  }
})

// With anonymous ID
analytics.identify({
  userId: 'user@example.com',
  anonymousId: 'anon-123',
  traits: {
    email: 'user@example.com'
  }
})

// With callback
analytics.identify({
  userId: 'user@example.com',
  traits: { email: 'user@example.com' }
}, (err) => {
  if (err) {
    console.error('Identification failed:', err)
  } else {
    console.log('User identified successfully')
  }
})

Event Tracking

Track custom events to understand user behavior.

Browser

// Simple event
analytics.track('button_clicked')

// Event with properties
analytics.track('purchase_completed', {
  product_id: '123',
  product_name: 'Widget',
  price: 29.99,
  currency: 'USD',
  quantity: 1
})

// With callback
analytics.track('signup_completed', {
  method: 'email'
}, () => {
  console.log('Event tracked successfully')
})

Node.js

// Track event with user ID
analytics.track({
  userId: 'user@example.com',
  event: 'purchase_completed',
  properties: {
    product_id: '123',
    product_name: 'Widget',
    price: 29.99,
    currency: 'USD'
  }
})

// Track event with anonymous ID
analytics.track({
  anonymousId: 'anon-123',
  event: 'page_viewed',
  properties: {
    page: 'homepage'
  }
})

// With timestamp
analytics.track({
  userId: 'user@example.com',
  event: 'order_completed',
  properties: { order_id: 'ABC123' },
  timestamp: new Date()
})

// With callback
analytics.track({
  userId: 'user@example.com',
  event: 'signup_completed'
}, (err) => {
  if (err) {
    console.error('Tracking failed:', err)
  }
})

Page Tracking

Track page views in your web application.

Browser

// Track current page
analytics.page()

// Track with page name
analytics.page('Home')

// Track with category and name
analytics.page('Docs', 'Getting Started')

// Track with properties
analytics.page('Product Detail', {
  product_id: '123',
  category: 'Electronics',
  url: window.location.href,
  referrer: document.referrer
})

// Automatic page tracking on route changes
// For Single Page Applications (SPAs):
window.addEventListener('popstate', () => {
  analytics.page()
})

Node.js

// Track page view
analytics.page({
  userId: 'user@example.com',
  name: 'Home',
  properties: {
    title: 'Homepage',
    url: 'https://example.com'
  }
})

// With category
analytics.page({
  userId: 'user@example.com',
  category: 'Docs',
  name: 'Getting Started',
  properties: {
    url: 'https://example.com/docs/getting-started'
  }
})

Screen Tracking

Track screen views in your application.

Browser

// Track screen view
analytics.screen('Dashboard', {
  user_type: 'premium'
})

Node.js

// Track screen view
analytics.screen({
  userId: 'user@example.com',
  name: 'Dashboard',
  properties: {
    user_type: 'premium',
    version: '2.0'
  }
})

Group Tracking

Associate users with groups or organizations.

Browser

// Associate user with group
analytics.group('company-123', {
  name: 'Acme Inc',
  plan: 'enterprise',
  employees: 100
})

Node.js

// Associate user with group
analytics.group({
  userId: 'user@example.com',
  groupId: 'company-123',
  traits: {
    name: 'Acme Inc',
    plan: 'enterprise',
    employees: 100,
    industry: 'technology'
  }
})

Alias

Merge two user identities.

Browser

// Merge anonymous user with identified user
analytics.alias('user@example.com', 'anon-123')

Node.js

// Merge user identities
analytics.alias({
  userId: 'user@example.com',
  previousId: 'anon-123'
})

Reset/Logout

Clear user identification when they log out.

Browser

// Clear user identification
analytics.reset()

// Good practice: reset on logout
function handleLogout() {
  analytics.reset()
  // ... rest of logout logic
}