Version:

JavaScript SDK Quickstart

Install and use the RudderStack JavaScript SDK on your website.

This guide lists the steps to quickly install the JavaScript SDK on your website and start tracking user activity in no time.

Prerequisites

To set up and use the RudderStack JavaScript SDK, the following prerequisites must be met:

Step 1: Install JavaScript SDK

You can install the JavaScript SDK on your website via CDN or using NPM.

info

There is no page call embedded in the loading scripts listed below.

See Breaking Changes for more information.

Using CDN (asynchronous)

To integrate the SDK with your website and load it asynchronously:

  1. Go to the Setup tab of your JavaScript source in the RudderStack dashboard.
  2. Click the Minified or Unminified tab followed by Copy snippet and paste the script in your website’s <head> section.
JavaScript SDK installation snippet

Using CDN (synchronous)

To integrate the SDK with your website and load it synchronously:

  1. Go to the Setup tab of your JavaScript source in the RudderStack dashboard.
  2. Click the Minified or Unminified tab followed by Copy snippet.
JavaScript SDK installation snippet
  1. Set asyncScript (highlighted below) to false.
var asyncScript = false;
JavaScript SDK synchronous installation
  1. Paste the modified script in your website’s <head> section.
info

Note that:

  • Depending on the browser, the SDK loads either the legacy or modern JavaScript SDK bundle. The legacy bundle is built for ES5 while the modern bundle (with Module Federation) is built for ES6.
  • You can also pass your loadOptions as a third argument in the rudderanalytics.load method. See Load JavaScript SDK for more information.

Using NPM

While it is recommended to install the JavaScript SDK on your website via CDN, you can also use the NPM module for packaging RudderStack directly into your project.

warning
Use this NPM module only for browser applications. To integrate RudderStack with your Node.js apps, see the Node.js SDK documentation.

To install the JavaScript SDK via NPM, run the following command:

npm install @rudderstack/analytics-js --save

Run the following code snippet once and use the exported object throughout your project:

  • For ECMAScript modules (ESM):
import { RudderAnalytics } from '@rudderstack/analytics-js';

const rudderAnalytics = new RudderAnalytics();
rudderAnalytics.load(WRITE_KEY, DATA_PLANE_URL, {});
  
export { rudderAnalytics };
  • For CJS using the require method:
var RudderAnalytics = require("@rudderstack/analytics-js");

const rudderAnalytics = new RudderAnalytics();
rudderAnalytics.load(WRITE_KEY, DATA_PLANE_URL);

exports.rudderanalytics = rudderAnalytics;

Step 2: Check ready state

The JavaScript SDK provides the ready API with a callback that triggers when the SDK is done initializing itself and the other third-party destination SDKs.

rudderanalytics.ready(() => {
    console.log("All set!");
});

Step 3: Track current page

You can make a page call to track the current web page and pass any additional information:

rudderanalytics.page(
  "Cart",
  "Cart Viewed", {
    path: "/best-seller/1",
    referrer: "https://www.google.com/search?q=estore+bestseller",
    search: "estore bestseller",
    title: "The best sellers offered by EStore",
    url: "https://www.estore.com/best-seller/1"
  },
  () => {
    console.log("page call");
  }
);
info
Note that the above example highlights how to override and set custom values for path, referrer, search, title, and url. In reality, the SDK automatically captures these fields.

Single page applications

In the case of single-page applications (SPA) where a route change does not reload the page, you need to make the page call explicitly after the route change on the frontend.

See RudderStack-Next.js Integration guide for more information.

Step 4: Identify users

The identify method lets you identify a user and associate them with their actions. It also enables you to record any traits about them like their name, email, etc.

rudderanalytics.identify(
  "1hKOmRA4el9Zt1WSfVJIVo4GRlm", {
    firstName: "Alex",
    lastName: "Keener",
    email: "alex@example.com",
    phone: "+1-202-555-0146"
  },
  () => {
    console.log("identify call");
  }
);

The JavaScript SDK captures the user ID, first name, last name, email, and phone number from the above snippet.

info
The anonymous visitors are automatically assigned an anonymousId. See the Anonymous ID section for more information.

Step 5: Track user actions

The track method lets you capture user events along with the associated properties.

rudderanalytics.track(
  "Order Completed", {
    revenue: 77.6,
    currency: "USD",
  },
  () => {
    console.log("track call");
  }
);

The JavaScript SDK captures the Order Completed event along with revenueand currencyfrom the above snippet.

success
You can use the track method to track various success metrics for your website like user signups, item purchases, article bookmarks, and more.

Supported browsers

The JavaScript SDK supports the following browsers and their corresponding versions:

BrowserSupported Versions
Safariv7 and above
IEv11 and above
Edgev80 and above
Mozilla Firefoxv47 and above
Chromev54 and above
Operav43 and above
info
You can try adding the browser polyfills to your application if the SDK does not work on your browser.

Questions? Contact us by email or on Slack