New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rudder-sdk-js

Package Overview
Dependencies
Maintainers
1
Versions
182
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rudder-sdk-js

RudderStack Javascript SDK

  • 1.0.20
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
104K
increased by11.94%
Maintainers
1
Weekly downloads
 
Created
Source

What is RudderStack?

RudderStack is a customer data pipeline tool for collecting, routing and processing data from your websites, apps, cloud tools, and data warehouse.

More information on RudderStack can be found here.

What Is the RudderStack JavaScript SDK?

The RudderStack JavaScript SDK (released under Apache License 2.0) allows you to utilize our npm module rudder-sdk-js or rudder-analytics.js library to start sending event data from your website to RudderStack.

After integrating this SDK, you will also be able to connect to multiple destinations such as Amplitude, Google Analytics, etc. to send your data.

How to Use the RudderStack JavaScript SDK?

This Quick Start Guide will help you get up and running with using the RudderStack JavaScript SDK in no time. You just need to follow the steps below:

Step 1: Install RudderStack Using the Code Snippet

To integrate the SDK: You can use this NPM module to package Rudder directly into your project.

To install, run:

npm install rudder-sdk-js --save

Since the module exports a bunch of APIs on an already-defined object combined with node module caching, you should run the below code only once and use the exported object throughout your project :

import * as rudderanalytics from "rudder-sdk-js"
rudderanalytics.load(<YOUR_WRITE_KEY>, <DATA_PLANE_URL>)
rudderanalytics.ready(() => {console.log("we are all set!!!")})
export  {  rudderanalytics  }

For ES5, with require:

var rudderanalytics = require("rudder-sdk-js")
rudderanalytics.load(<YOUR_WRITE_KEY>, <DATA_PLANE_URL>)
exports.rudderanalytics = rudderanalytics

For destinations where you don't want the SDK to load the third-party scripts separately, modify the load call:

rudderanalytics.load(<YOUR_WRITE_KEY>, <DATA_PLANE_URL>, {loadIntegration:  false})
// the SDK expects that the destination global queue or function for pushing events is
// already present for the destinations.
// Currently, the loadIntegration flag is supported for Amplitude and
// Google Analytics destinations. The SDK expects window.amplitude and window.ga to be already
// defined by the user separately for sending events to these destinations.

You can also refer to the sample projects for a walk through of the above: Sample Angular Project and Sample React Project

Step 2: Identify your users using the identify() method:

The identify() method allows you to link users and their actions to a specific userid.

A sample example of how the identify() method works is as shown:

rudderanalytics.identify(
  "12345",
  { email: "name@domain.com" },
  {
    page: {
      path: "",
      referrer: "",
      search: "",
      title: "",
      url: "",
    },
  },
  () => {
    console.log("in identify call");
  }
);

In the above example, information such as the user ID, email along with contextual information such as IP address, anonymousId, etc. will be captured.

There is no need to call identify() for anonymous visitors to your website. Such visitors are automatically assigned an anonymousId.

Step 3: Track Your Users’ Actions With the track() Method

The track() method allows you to track any actions that your users might perform.

A sample example of how the track() method works is as shown:

rudderanalytics.track(
  "test track event GA3",
  {
    revenue: 30,
    currency: "USD",
    user_actual_id: 12345,
  },
  () => {
    console.log("in track call");
  }
);

In the above example, the method tracks the event ‘test track event GA3’, and information such as the revenue, currency, anonymousId.

You can use this method to track various other success metrics for your website, such as user signups, item purchases, article bookmarks, and much more.

To override contextual information, for ex: anonymizing IP and other contextual fields like page properties, the following template can be used. Similarly one can override the auto-generated anonymousId with provided ID. For this:

rudderanalytics.track(
  "test track event GA3",
  {
    revenue:  30,
    currency:  'USD' ,
    user_actual_id:  12345
  },
  {
    page: {
          path: "",
          referrer: "",
          search: "",
          title: "",
          url: ""
    },
    context: {
      ip:  "0.0.0.0"
    },
    anonymousId:  "00000000000000000000000000"
  },
  () => {console.log("in track call");}
);

You’ve now successfully installed RudderStack's JavaScript SDK tracking. You can enable and use any event destination to send your event data via RudderStack, in no time at all.

Step 4: Check Ready State

There are cases when you may want to tap into the features provide by end destination SDKs to enhance tracking and other functionalities. RudderStack's JavaScript SDK exposes a ready API with a callback parameter, that fires when the SDK is done initializing itself and other third-party native SDK destinations.

For example:

rudderanalytics.ready(() => {
  console.log("we are all set!!!");
});

Adding Callbacks to Standard Methods

You can also define callbacks to the common methods of the rudderanalytics object.

For now, the functionality is supported for syncPixel method which is called in the SDK when making sync calls in integrations for relevant destinations.

For example:

rudderanalytics.syncPixelCallback = (obj) => {
  rudderanalytics.track(
    "sync lotame",
    { destination: obj.destination },
    { integrations: { All: false, S3: true } }
  );
};

In the above example, we are defining a syncPixelCallback on the analytics object before the call to load the SDK. This will lead to calling of this registered callback with the parameter {destination: <destination_name>} whenever a sync call is made from the SDK for relevant integrations like Lotame.

The callback can be supplied in options parameter like below as well:

// define the callbacks directly on the load method like:
rudderanalytics.load(<YOUR_WRITE_KEY>, <DATA_PLANE_URL>,
	                    { clientSuppliedCallbacks: {
						    "syncPixelCallback": () => {console.log('sync done!')}
						  }
						}
)

We will be adding similar callbacks for APIs such as track, page, identify, etc.

Autotrack

IMPORTANT: We have deprecated the Autotrack feature for the RudderStack JavaScript SDK. If you still wish to use it for your project, refer to this repository.

It may happen that the need arises to track most of user interactions with a web-app. It becomes hard for a developer to capture these DOM interactions and make track calls for all. The autotrack feature of Rudder SDK helps in tracking all user interactions like click | change | submit automatically. The data generated will be verbose and to make sense of the data, one can use user transformations from the config-plane to build use-cases like monitoring user journeys etc. For more information and payload structure, click here.

To enable autotracking, make the load call as:

rudderanalytics.load(<YOUR_WRITE_KEY>, <DATA_PLANE_URL>, {
  useAutoTracking: true,
});

By default, the SDK doesn't capture any DOM element values. To start capturing values, like form field values when submitting the form, other input element values etc, whitelist them using any attribute of the element for which you want to send values, For ex, tracking element values for all elements whose any one attribute is "CLASS_ATTR_NAME":

rudderanalytics.load(<YOUR_WRITE_KEY>, <DATA_PLANE_URL>, {
  useAutoTracking: true,
  valTrackingList: ["CLASS_ATTR_NAME"],
});

Self-hosted Config Plane

If you are using a device mode destination like Heap, FullStory, etc., the JavaScript SDK needs to fetch the required configuration from the Control Plane. If you are using the RudderStack Config Generator to host your own Control Plane, then follow this guide and make a load call as shown:

rudderanalytics.load(<YOUR_WRITE_KEY>, <DATA_PLANE_URL>, {configUrl: CONTROL_PLANE_URL});

Contribute

You can start adding integrations of your choice for sending data through their JavaScript SDKs.

How To Build the SDK

  • Look for run scripts in the package.json file for getting browser minified and non-minified builds. The builds are updated in the dist folder of the directory. Among the others, the important ones are:

    • npm run buildProdBrowser: This outputs rudder-analytics.min.js.
    • npm run buildProdBrowserBrotli: This outputs two files, rudder-analytics.min.br.js (the original minified file, same as above) and rudder-analytics.min.br.js.br (the brotli compressed file).
    • npm run buildProdBrowserGzip: This outputs two files, rudder-analytics.min.gzip.js (the original minified file, same as above) and rudder-analytics.min.gzip.js.gz (the gzipped compressed file).

    We are using rollup to build our SDKs, configuration for it is present in rollup.config.js in the repo directory.

  • For adding or removing integrations, modify the imports in index.js under integrations folder.

Contact Us

For more support on using the RudderStack JavaScript SDK, feel free to contact us or start a conversation on our Slack channel.

Keywords

FAQs

Package last updated on 05 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc