Version:

How to Specify the Cookie Domain

Configure the JavaScript SDK to store cookies on a specific domain.

This guide shows you how to configure the RudderStack JavaScript SDK to store its cookies on a specific domain, rather than the default top-level domain. This can be useful in scenarios where you need finer control over cookie scope.

Default behavior

The SDK automatically sets cookies at the top-level domain (e.g. example.com). This enables consistent user identification across subdomains like admin.example.com and app.example.com, without requiring additional configuration.

This behavior can be customized by configuring SDK’s loadOptions.

Configuration steps

  1. Ensure you have initialized the JavaScript SDK using the rudderanalytics.load(WRITE_KEY, DATA_PLANE_URL, loadOptions) method.
  2. Modify the loadOptions.storage.cookie.domain property. Set its value to a string representing the domain where you want cookies to be stored.

Code example

// Replace with your actual Write Key and Data Plane URL
const WRITE_KEY = "YOUR_WRITE_KEY";
const DATA_PLANE_URL = "YOUR_DATA_PLANE_URL";

rudderanalytics.load(WRITE_KEY, DATA_PLANE_URL, {
  // Other load options can go here

  storage: {
    cookie: {
      // Specify the domain for cookies
      domain: "subdomain.yourwebsite.com" // Example: store only on subdomain.yourwebsite.com
      // domain: "yourwebsite.com" // Example: explicitly store on top-level domain enabling tracking across all subdomains, the default behavior
    }
  }
});

Questions? Contact us by email or on Slack