# JavaScript SDK

<p>RudderStack&rsquo;s <a href="https://github.com/rudderlabs/rudder-sdk-js" >JavaScript SDK</a> lets you track customer event data from your website and send it to your specified <a href="https://www.rudderstack.com/docs/destinations/overview/" >destinations</a>. It is written in TypeScript and offers significant improvements in performance, size, and features compared to its <a href="https://www.rudderstack.com/docs/archive/javascript-sdk/1.1/" >predecessor</a>.</p>
<p>This documentation covers the latest <strong>JavaScript SDK v3</strong> (<a href="https://www.npmjs.com/package/@rudderstack/analytics-js" ><code>@rudderstack/analytics-js</code></a> on npm).</p>
<div class="rs-version-row"><a class="rs-version" href="https://www.npmjs.com/package/@rudderstack/analytics-js" target="_blank" rel="noopener" data-version-url="https://registry.npmjs.org/@rudderstack/analytics-js" data-version-path="dist-tags.latest" data-version-prefix="">npm3.31.4</a></div>

<!-- end-chunk -->
<!-- begin-chunk -->
<h2 id="key-features">Key features</h2><ul>
<li>Fast, reliable, and less vulnerable to ad blockers.</li>
<li>Load specific SDK features on demand using <a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/load-js-sdk/#plugins" >plugins</a>.</li>
<li>Lightweight <a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/load-js-sdk/#storage" >storage data footprint</a> with improved encryption.</li>
<li>Complete control over the persistent data storage strategy.</li>
<li>ITP (Intelligent Tracking Prevention) compliant as it implements server-side cookies.</li>
<li>Smaller SDK of around 25KB (size reduction of approximately 30% from the previous version).</li>
</ul>
<!-- end-chunk -->
<!-- begin-chunk -->
<h2 id="get-started">Get started</h2>
<html lang="en">
<blockquote class="info">
  <div class="tip-quote">
    
    <div class="tip-text"><p><strong>Migrating from older versions?</strong></p>
<p>If you are upgrading from a version prior to v3, review these guides before getting started:</p>
<ul>
<li><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/migration-guide/" >Migration guide</a>: Step-by-step instructions for updating your SDK implementation.</li>
<li><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/breaking-changes/" >Breaking changes</a>: Understand the compatibility differences introduced in v3.</li>
</ul>
<p>Learn about the <a href="https://www.rudderstack.com/blog/feature-launch-javascript-sdk-v3-delivers-better-performance-and-more-control/" >benefits of upgrading to JavaScript SDK v3</a>.</p>
</div>
  </div>
</blockquote>

</html>
<p>If you&rsquo;re using <code>npm</code> module in your browser-side code, following code snippet will import the RudderStack JavaScript SDK and send its first <code>page</code> event.</p>
<div class="rs-code">
  <div class="rs-code__head">javascript<button class="rs-code__copy" type="button">
      
      Copy
    </button>
  </div>
  <div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript">// Step 1: Install the SDK - `npm i @rudderstack/analytics-js`
// Step 2: Initalize the SDK
import { RudderAnalytics } from &#39;@rudderstack/analytics-js&#39;;
const rudderAnalytics = new RudderAnalytics();
rudderAnalytics.load(process.env.WRITE_KEY, process.env.DATA_PLANE_URL, {});
// Q: How to generate your WRITE_KEY and DATA_PLANE_URL?
// A: Create a new JavaScript source at https://app.rudderstack.com
export { rudderAnalytics };
// Step 3: Call event tracking methods such as `page`, `track`, `identify`, etc. as needed
rudderAnalytics.page();
// NOTICE: This code works only if you have set up your browser-side code to use `npm` modules. Follow the quickstart guide otherwise.
// https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/quickstart/
</code></pre></div>
</div>
<p>Follow the <a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/quickstart/" >JavaScript SDK Quickstart</a> to quickly start using RudderStack JavaScript SDK with any of your preferred methods (Using CDN, npm (ESM or CJS), or for single-page applications).</p>
<!-- end-chunk -->
<!-- begin-chunk -->
<h2 id="core-tasks">Core tasks</h2><p>Find instructions for specific goals and common implementation tasks related to the JavaScript SDK:</p>
<table>
  <thead>
      <tr>
          <th>Guide</th>
          <th>Description</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/quickstart/" >Quickstart</a></td>
          <td>Set up and use the JavaScript SDK on your website in no time.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/installation/" >Install the JavaScript SDK</a></td>
          <td>Install the JavaScript SDK via CDN or NPM.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/load-js-sdk/" >Load the SDK</a></td>
          <td>Understand the different options for loading the SDK snippet.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/supported-api/#pre-load-api-buffering" >Pre-load API buffering</a></td>
          <td>Understand how calls made before a valid load are buffered and replayed in order after the SDK loads.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/supported-api/" >Track event data</a></td>
          <td>Use the SDK&rsquo;s API calls (<code>identify</code>, <code>track</code>, <code>page</code>, etc.) to send data.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/custom-context/" >Set custom context</a></td>
          <td>Attach enrichment data once and apply it to subsequent events during the current page load.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/data-storage-cookies/persistent-data-storage/" >Configure data persistence</a></td>
          <td>Customize how the SDK stores user information and preferences.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/server-side-cookies/" >Use server-side cookies</a></td>
          <td>Implement server-side cookies for improved tracking and ITP compliance.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/filtering/" >Filter events</a></td>
          <td>Control which events are sent to specific destinations.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/consent-management/" >Implement consent management</a></td>
          <td>Configure the various consent management options in JavaScript SDK.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/detecting-adblocked-pages/" >Detect Ad-blocked pages</a></td>
          <td>Identify when the SDK might be blocked by ad blockers.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/service-worker/" >Use the service worker SDK</a></td>
          <td>Implement tracking in service worker environments.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/service-worker/usage-in-chrome-extensions/" >Use JavaScript SDK in Chrome extensions</a></td>
          <td>Integrate the SDK within a Chrome browser extension.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/sdk-hardening/" >Harden JavaScript SDK</a></td>
          <td>Configure a hardened version of the JavaScript SDK for added security.</td>
      </tr>
  </tbody>
</table>
<!-- end-chunk -->
<!-- begin-chunk -->
<h2 id="understand-concepts">Understand concepts</h2><ul>
<li><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/new-features/" >JavaScript SDK key features</a>: Learn about the significant enhancements in SDK v3.</li>
<li><a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/data-storage-cookies/" >Data persistence in JavaScript SDK</a>: Understand how and what data the SDK stores locally.</li>
</ul>
<p>See <a href="https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/faq/" >FAQs</a> for answers to the commonly-asked questions about the JavaScript SDK.</p>
<br />

