Amplitude: Event-Based Product Analytics with Behavioral | OpsBlu Docs

Amplitude: Event-Based Product Analytics with Behavioral

Implement Amplitude SDK initialization, logEvent tracking, user and event properties, Identify API, behavioral cohorts, HTTP API v2, schema...

How Amplitude Works

Amplitude is a product analytics platform built on an event-based data model. Every tracked interaction is an event: a button click, a page view, a feature activation, a purchase. Each event has a name and a set of event properties (key-value pairs describing the context of that specific interaction). Separately, each user has user properties that persist across events and describe the user's current state (plan type, signup date, company size, etc.).

The data model is schema-on-write. Events are validated against a taxonomy when ingested. Amplitude's Govern feature (part of Amplitude Data) lets you define expected event names, property names, and property types. Events that don't match the schema can be flagged, blocked, or allowed through. This enforcement happens at the project level and applies to all data sources (SDKs, HTTP API, integrations).

Amplitude stores events in a columnar format optimized for aggregation queries. When you build a chart in Amplitude (funnel, retention, event segmentation, etc.), the system runs a query across the event stream, filtering by event name, property values, user properties, and time range. There is no session concept in the core data model; sessions are computed at query time using a configurable inactivity window (default 30 minutes).

Identity resolution in Amplitude works through a device ID and an optional user ID. The SDK generates a random device ID on first load and stores it in a cookie or local storage. When a user authenticates, you set a user ID. Amplitude merges the anonymous device ID with the known user ID so pre-login and post-login events are attributed to the same user. This merge is permanent and one-directional.

Data is available in the Amplitude UI within seconds of ingestion. There is no batch processing delay for standard event queries.

Installing the Amplitude SDK

Browser SDK

Install via npm or script tag:

npm install @amplitude/analytics-browser
import * as amplitude from '@amplitude/analytics-browser';

amplitude.init('YOUR_API_KEY', {
  defaultTracking: {
    sessions: true,        // Track session_start and session_end
    pageViews: true,       // Track page views automatically
    formInteractions: true, // Track form start and form submit
    fileDownloads: true     // Track file download clicks
  }
});

Or load via script tag:

<script type="text/javascript">
  !function(){"use strict";!function(e,t){var r=e.amplitude||{_q:[],_iq:{}};if(r.invoked)e.console&&console.error&&console.error("Amplitude snippet has been loaded.");else{var n=function(e,t){e.prototype[t]=function(){return this._q.push({name:t,args:Array.prototype.slice.call(arguments,0)}),this}},s=["init","logEvent","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","enableTracking","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenue","regenerateDeviceId","groupIdentify","onInit","onNewSessionStart","setLibrary","setTransport","resetSessionId"];for(var o=0;o<s.length;o++)n(r,s[o]);e.amplitude=r}}(window,document);

  amplitude.init('YOUR_API_KEY');
</script>

The init call sets the API key, generates or retrieves the device ID, and starts the event queue. Events logged before init completes are queued and sent after initialization.

React Integration

import * as amplitude from '@amplitude/analytics-browser';
import { useEffect } from 'react';

// Initialize once at app root
amplitude.init('YOUR_API_KEY', {
  defaultTracking: { sessions: true, pageViews: true }
});

function FeatureButton({ featureName }) {
  const handleClick = () => {
    amplitude.track('Feature Clicked', {
      feature_name: featureName,
      page: window.location.pathname
    });
  };

  return <button {featureName}</button>;
}

Node.js (Server-Side)

import { init, track, identify, Identify } from '@amplitude/analytics-node';

init('YOUR_API_KEY');

// Track a server-side event
track('Subscription Renewed', {
  plan: 'annual',
  revenue: 599.00,
  currency: 'USD'
}, {
  user_id: 'USER_123'
});

Event Tracking and Data Collection

Tracking Events

Every event needs a name and optionally event properties:

// Basic event
amplitude.track('Button Clicked');

// Event with properties
amplitude.track('Item Purchased', {
  item_id: 'SKU_456',
  item_name: 'Pro Widget',
  price: 29.99,
  currency: 'USD',
  category: 'Widgets',
  is_first_purchase: true
});

// Track revenue specifically
const revenue = new amplitude.Revenue()
  .setProductId('SKU_456')
  .setPrice(29.99)
  .setQuantity(1)
  .setRevenueType('purchase');
amplitude.revenue(revenue);

Event Properties vs User Properties

Event properties describe the specific interaction (what was clicked, which page, what value). They are attached to a single event and do not persist.

User properties describe the user's state and persist across all future events until changed. Set them with the Identify API:

const identifyEvent = new amplitude.Identify();

// Set a user property (overwrites any existing value)
identifyEvent.set('account_type', 'premium');
identifyEvent.set('signup_date', '2026-01-15');

// Set only if not already set
identifyEvent.setOnce('first_referrer', document.referrer);

// Increment a numeric property
identifyEvent.add('login_count', 1);

// Append to an array property
identifyEvent.append('viewed_categories', 'Technology');

// Remove from an array property
identifyEvent.remove('viewed_categories', 'Outdated');

// Prepend to an array property
identifyEvent.prepend('recent_searches', 'amplitude docs');

amplitude.identify(identifyEvent);

The Identify API supports these operations: set, setOnce, add, append, prepend, remove, unset, and clearAll. These operations are atomic and resolve conflicts when multiple clients update the same user simultaneously.

Group Analytics

Amplitude supports group-level analytics for B2B products where you want to analyze behavior at the account or organization level:

// Associate the user with a group
amplitude.setGroup('company', 'Acme Corp');

// Set group properties
const groupIdentify = new amplitude.Identify();
groupIdentify.set('plan', 'enterprise');
groupIdentify.set('employee_count', 500);
amplitude.groupIdentify('company', 'Acme Corp', groupIdentify);

Group analytics lets you build charts that aggregate events across all users in a group, showing metrics like "events per company" or "feature adoption by account."

Identity and User Tracking

Amplitude uses a two-ID system: device ID and user ID.

Device ID: Automatically generated on first SDK initialization. Stored in a cookie (amp_APIKEY) or localStorage. Identifies a single browser or app instance.

User ID: Set by your code when the user authenticates:

amplitude.setUserId('USER_123');

When you set a user ID on a device that previously only had a device ID, Amplitude merges the two identities. All prior anonymous events on that device are retroactively attributed to the identified user. This merge happens once and is irreversible.

To handle logout:

// Reset user ID on logout
amplitude.reset();  // Clears user ID and generates a new device ID

Calling reset() prevents post-logout events from being attributed to the previous user. If you only call setUserId(null), the device ID remains the same, and events could still be correlated.

HTTP API v2

Send events server-side or from any system that can make HTTP requests:

# Send events via HTTP API v2
curl -X POST "https://api2.amplitude.com/2/httpapi" \
  -H "Content-Type: application/json" \
  -d '{
    "api_key": "YOUR_API_KEY",
    "events": [
      {
        "user_id": "USER_123",
        "event_type": "Backend Purchase",
        "event_properties": {
          "transaction_id": "T99887",
          "amount": 150.00,
          "currency": "USD"
        },
        "user_properties": {
          "$set": {
            "lifetime_value": 750.00
          }
        },
        "time": 1709568000000
      }
    ]
  }'

The HTTP API v2 endpoint accepts batches of up to 2000 events per request. The time field is a Unix timestamp in milliseconds. If omitted, Amplitude uses the server receive time.

Response codes:

  • 200: Events accepted
  • 400: Invalid request (malformed JSON, missing fields)
  • 413: Payload too large (> 20MB or > 2000 events)
  • 429: Rate limited (throttled)

Batch API

For high-volume server-side ingestion, use the Batch API which has higher throughput limits:

curl -X POST "https://api2.amplitude.com/batch" \
  -H "Content-Type: application/json" \
  -d '{
    "api_key": "YOUR_API_KEY",
    "events": [...]
  }'

The Batch API accepts up to 2000 events per request and processes them asynchronously. It returns 200 immediately upon receipt.

Schema Enforcement with Amplitude Data

Amplitude Data (formerly Govern/Taxonomy) lets you define and enforce your event schema:

  1. Event types: Define expected event names with descriptions
  2. Event properties: For each event, define expected property names, types (string, number, boolean, array), and whether they are required
  3. User properties: Define expected user property names and types
  4. Blocking: Mark unexpected events or properties as "blocked" to drop them at ingestion

The schema is managed through the Amplitude UI under Data > Tracking Plan, or via the Taxonomy API:

# Create an event type in the tracking plan
curl -X POST "https://amplitude.com/api/2/taxonomy/event" \
  -H "Authorization: Basic BASE64_API_KEY_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "event_type": "Item Purchased",
    "description": "User completes a purchase",
    "category": "Commerce"
  }'

Schema enforcement prevents data quality degradation as multiple teams instrument new events. Amplitude flags schema violations in the Data Quality dashboard.

Common Issues

Issue Cause Fix
Events not appearing in dashboard API key is incorrect, events are queued but not flushed, or ad blocker is blocking requests to api2.amplitude.com Verify the API key matches your project; call amplitude.flush() before page unload; proxy requests through your own domain to avoid ad blockers
User merge not working setUserId called before the SDK initializes, or called with an empty string Ensure init completes before setUserId; never pass empty string or undefined as user ID
Duplicate events SDK initialized multiple times (common in SPAs with hot module reload) Guard initialization with a flag; use amplitude.init only once at app root
Event properties showing as (none) Property name has a typo or case mismatch compared to the schema Check exact property name casing in the event stream; Amplitude is case-sensitive
Revenue not tracking Using track instead of the Revenue class, or revenue property name conflicts Use the amplitude.revenue() API with a Revenue object for accurate revenue tracking
High event volume on page load defaultTracking enabled for all categories, generating multiple automatic events per page Disable unnecessary defaultTracking categories; only enable what you need
Device ID changes on every visit Cookies blocked or cleared; SDK falling back to generating a new device ID each time Implement a custom device ID strategy using localStorage or pass a device ID from your backend
Group properties not updating groupIdentify called with wrong group type or group value Ensure the group type and value in groupIdentify match exactly what was set in setGroup

Data Export and Warehousing

Amplitude supports several data export methods:

Export API: Download raw event data as gzipped JSON for a specific date range:

curl -u API_KEY:SECRET_KEY \
  "https://amplitude.com/api/2/export?start=20260301T00&end=20260304T00" \
  -o events.json.gz

Cohort API: Export user lists from behavioral cohorts for use in external systems.

Snowflake/BigQuery/S3 integrations: Stream event data to your data warehouse in near-real-time via Amplitude's warehouse sync.

Each exported event includes the event name, all event properties, user properties at the time of the event, device metadata (OS, device type, carrier), location data (country, city, DMA), and Amplitude's computed fields (session ID, event ID, amplitude user ID).