RudderStack JavaScript SDK (Deprecated)
The `rudder-sdk-js` package is the original JavaScript SDK for RudderStack, a customer data platform. It enables tracking of customer event data from websites and sending it to various destinations via the RudderStack platform. The current stable version observed in the npm metadata is `2.52.11`. However, this specific package is explicitly marked as *deprecated* and is no longer actively maintained. Users are strongly recommended to migrate to the newer `@rudderstack/analytics-js` package for continued support, enhanced features, and security updates. This SDK allowed for identifying users, tracking actions, and managing event readiness through a global `rudderanalytics` object. Its primary differentiator was providing a unified API for sending data to multiple analytics and marketing tools, acting as a server-side customer data router.
Common errors
-
ReferenceError: rudderanalytics is not defined
cause The SDK script has not loaded or executed correctly, or `rudderanalytics` is being accessed before it is globally available or before the `rudderanalytics.ready()` callback fires.fixEnsure the SDK script tag is correctly placed in the `<head>` section of your HTML and that all RudderStack API calls are made within the `rudderanalytics.ready()` callback or explicitly after the script has fully loaded. -
This package is deprecated and no longer maintained.
cause This message (or similar warnings during installation/build) indicates that you are using the `rudder-sdk-js` package, which has been officially deprecated by RudderStack.fixMigrate your project to use the `@rudderstack/analytics-js` package. This new package offers enhanced features, ongoing maintenance, and critical security updates.
Warnings
- breaking This `rudder-sdk-js` package is officially deprecated and is no longer maintained. Continued use is not recommended due to lack of security updates, bug fixes, and new features.
- deprecated The service worker export has been moved from `rudder-sdk-js` to a separate, dedicated package, `@rudderstack/analytics-js-service-worker`. Attempting to use service worker features from `rudder-sdk-js` may result in errors or outdated behavior.
Install
-
npm install rudder-sdk-js -
yarn add rudder-sdk-js -
pnpm add rudder-sdk-js
Imports
- rudderanalytics
import { rudderanalytics } from 'rudder-sdk-js';window.rudderanalytics.load('YOUR_WRITE_KEY', 'YOUR_DATAPLANE_URL'); - track
import { track } from 'rudder-sdk-js';window.rudderanalytics.track('Event Name', { property: 'value' }); - Analytics (new package)
import { Analytics } from 'rudder-sdk-js';import { Analytics } from '@rudderstack/analytics-js';
Quickstart
<html>
<head>
<title>RudderStack Deprecated SDK Quickstart</title>
<script type="text/javascript">
!function(){var e=window.rudderanalytics=window.rudderanalytics||[];e.methods=["load","page","track","identify","alias","group","ready","reset","getAnonymousId","setAnonymousId"],e.factory=function(t){return function(){var n=Array.prototype.slice.call(arguments);n.unshift(t);e.push(n);return e}};for(var t=0;t<e.methods.length;t++){var n=e.methods[t];e[n]=e.factory(n)}e.load=function(t,n){var r=document.createElement("script");r.type="text/javascript",r.async=!0,r.src="https://cdn.rudderlabs.com/rudder-analytics.min.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(r,a);e._writeKey=t;e._options=n};}();
// Initialize RudderStack (replace with your actual keys)
rudderanalytics.load('YOUR_WRITE_KEY', 'YOUR_DATAPLANE_URL');
rudderanalytics.ready(function() {
console.log('RudderStack SDK is ready!');
rudderanalytics.track('Product Viewed', {
productId: 'RS-123',
productName: 'Example Product',
category: 'Electronics'
});
rudderanalytics.identify('user-123', {
email: 'test@example.com',
plan: 'premium'
});
});
</script>
</head>
<body>
<h1>Welcome to the website!</h1>
<button onclick="rudderanalytics.track('Button Clicked', { buttonName: 'Test' })">Click Me</button>
<p>Check your browser console and network requests for RudderStack events.</p>
</body>
</html>