このページでは

For AI agents: a documentation index is available at /docs/llms.txt. Append .md to any page URL for markdown, or send Accept: text/markdown.

Get started with the Browser Unified SDK

npmv1.1.43117.5 kB gzip

このページはまだあなたの言語に翻訳されていません。 現在取り組んでいますので、後でもう一度確認してください。

For web developers10 minBeginner

This guide helps you install the Browser Unified SDK (@amplitude/unified), initialize every product it includes with one call, and send one custom event to Amplitude. For the full option list, go to UnifiedOptions. For changes between versions, go to the @amplitude/unified changelog.

The Unified SDK bundles Browser SDK 2 (@amplitude/analytics-browser) with Session Replay, Feature Experiment, and Guides and Surveys. Every analytics method and option works the same as in Browser SDK 2. If you need Analytics only, install Browser SDK 2 instead. Don't install both packages on the same page. To compare npm, the Unified Script, and Google Tag Manager, go to Choose a browser installation.

Prerequisites

  • An Amplitude project and its API key. To find the key, go to Settings > Projects, select your project, and open General.
  • The data center your organization uses: US (default) or EU.
  • A JavaScript bundler, such as webpack, Vite, or esbuild. The Unified SDK installs from npm only. For a script tag, use the Unified Script.
  • A page served over http:// or https://. Pages opened from file:// can't set cookies, so the SDK can't persist identity between loads.

Install the SDK

bash
npm install @amplitude/unified

Then import the package:

ts
import * as amplitude from "@amplitude/unified";

Fix installation problems

Initialize the SDK

Call initAll() once, as early as possible. It initializes Analytics first, then adds Session Replay, Feature Experiment, and Guides and Surveys as plugins. The SDK queues any calls you make before initAll() finishes and runs them afterward.

ts
amplitude.initAll("YOUR_API_KEY", {
  serverZone: "EU", // Omit for US projects.
  analytics: { autocapture: true },
  sessionReplay: { sampleRate: 1 }, // Records every session. Lower this before production.
});

Set serverZone at the top level, not inside analytics. initAll() passes the top-level value to every product and overwrites the per-product value, even when you leave the top-level value unset.

Session Replay records nothing until you set sessionReplay.sampleRate above 0.

initAll() doesn't accept a user ID argument. Call amplitude.setUserId() after it.

Fix initialization problems

For analytics initialization problems, such as cookies or autocapture warnings, go to Fix initialization problems in Browser SDK 2.

Send a first event

Track one custom event and wait for the upload result. track() returns an object whose promise resolves after the upload attempt.

ts
const result = await amplitude.track("Sign Up Clicked", {
  plan: "starter",
}).promise;

console.log(result.code, result.message);
// 200 "Event tracked successfully"

Fix upload problems

The Unified SDK uploads events through the bundled Browser SDK 2, so the result codes match Browser SDK 2. For every result.code and result.message, go to Fix upload problems in Browser SDK 2.

Confirm the event in Amplitude

  1. In Amplitude, open User Lookup and search for the device ID from amplitude.getDeviceId().
  2. Open the Activity tab and turn on Live event updates.
  3. Confirm that Sign Up Clicked appears with the plan property.

If the event doesn't appear within a minute, follow Verify events.

Next steps

これは役に立ちましたか?