All Projects → rudderlabs → rudder-sdk-js

rudderlabs / rudder-sdk-js

Licence: MIT license
JavaScript SDK for RudderStack - the Customer Data Platform for Developers.

Programming Languages

HTML
75241 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to rudder-sdk-js

Rudder Server
Privacy and Security focused Segment-alternative, in Golang and React
Stars: ✭ 2,874 (+3023.91%)
Mutual labels:  data-synchronization, customer-data, customer-data-platform, segment-alternative, rudderstack, customer-data-pipeline
Snowplow
The enterprise-grade behavioral data engine (web, mobile, server-side, webhooks), running cloud-natively on AWS and GCP
Stars: ✭ 5,935 (+6351.09%)
Mutual labels:  product-analytics, marketing-analytics
Pimcore
Open Source Data & Experience Management Platform (PIM, MDM, CDP, DAM, DXP/CMS & Digital Commerce)
Stars: ✭ 2,352 (+2456.52%)
Mutual labels:  cdp, customer-data-platform
MauticFBAdsLeadAdsBundle
Enables synchnronization of Facebook Lead Ads as leads into Mautic.
Stars: ✭ 45 (-51.09%)
Mutual labels:  marketing-automation
mautic-contact-ledger
Historical accounting for contacts in Mautic.
Stars: ✭ 17 (-81.52%)
Mutual labels:  marketing-automation
Flowy
The minimal javascript library to create flowcharts ✨
Stars: ✭ 8,636 (+9286.96%)
Mutual labels:  marketing-automation
saisoku
Saisoku is a Python module that helps you build complex pipelines of batch file/directory transfer/sync jobs.
Stars: ✭ 40 (-56.52%)
Mutual labels:  data-synchronization
drip-nodejs
The complete Nodejs wrapper for the Drip REST API
Stars: ✭ 18 (-80.43%)
Mutual labels:  marketing-automation
rust-cdp
🔨 Chrome DevTools Protocol toolkit for Rust
Stars: ✭ 25 (-72.83%)
Mutual labels:  cdp
Mautic
Mautic: Open Source Marketing Automation Software.
Stars: ✭ 4,887 (+5211.96%)
Mutual labels:  marketing-automation
python-automated-bulk-whatsapp-messages
It is a python script to send automated bulk WhatsApp messages to multiple recipients from an excel sheet at once.
Stars: ✭ 56 (-39.13%)
Mutual labels:  marketing-automation
mautic-api-services
Add an external API as a service in Mautic
Stars: ✭ 13 (-85.87%)
Mutual labels:  marketing-automation
Erxes
Free and open fair-code licensed all-in-one growth marketing & management software
Stars: ✭ 1,988 (+2060.87%)
Mutual labels:  marketing-automation
mautic-advanced-templates-bundle
Plugin extends default email template capabilities with TWIG block so you can use advanced scripting techniques like conditions, loops etc
Stars: ✭ 63 (-31.52%)
Mutual labels:  marketing-automation
automato
🎉🎉 ( v2 ) Web Application to automate sending Whatsapp, SMS & Email* campaigns
Stars: ✭ 92 (+0%)
Mutual labels:  marketing-automation
vtiger
🐯 Vtiger is the #1 business automation software. Try it with Docker!
Stars: ✭ 59 (-35.87%)
Mutual labels:  marketing-automation
mmetrics
Easy computation of Marketing Metrics in R
Stars: ✭ 26 (-71.74%)
Mutual labels:  marketing-analytics
easy-adwords
EasyAdWords is an easy-to-use wrapper library for simple reporting and entity operations for Google AdWords.
Stars: ✭ 16 (-82.61%)
Mutual labels:  marketing-automation
Facebook-Auto-Pilot
Automate common Facebook activities such as posting to groups and pages walls. Effortlessly post to multiple groups or pages.
Stars: ✭ 126 (+36.96%)
Mutual labels:  marketing-automation
Mailtrain
Self hosted newsletter app
Stars: ✭ 4,931 (+5259.78%)
Mutual labels:  marketing-automation

The Customer Data Platform for Developers

Website · Documentation · Community Slack

Releases Quality Gate Status Maintainability Rating Reliability Rating Security Rating Coverage


RudderStack JavaScript SDK

The JavaScript SDK lets you track customer event data from your website and send it to your specified destinations via RudderStack.

For detailed documentation on the RudderStack JavaScript SDK, click here.

Table of Contents

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

Installing the JavaScript SDK

To integrate the JavaScript SDK with your website, place the following code snippet in the <head> section of your website.

<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 r=Array.prototype.slice.call(arguments);return r.unshift(t),e.push(r),e}};for(var t=0;t<e.methods.length;t++){var r=e.methods[t];e[r]=e.factory(r)}e.loadJS=function(e,t){var r=document.createElement("script");r.type="text/javascript",r.async=!0,r.src="https://cdn.rudderlabs.com/v1.1/rudder-analytics.min.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(r,a)},e.loadJS(),
e.load(<WRITE_KEY>,<DATA_PLANE_URL>),
e.page()}();
</script>

The above snippet lets you integrate the SDK with your website and load it asynchronously to keep your page load time unaffected.

To load rudder-analytics.js on to your page synchronously, you can refer to the minified or non-minified versions of the code in the following sections:

Minified code

<script>
  rudderanalytics=window.rudderanalytics=[];for(var methods=["load","page","track","identify","alias","group","ready","reset","getAnonymousId","setAnonymousId"],i=0;i<methods.length;i++){var method=methods[i];rudderanalytics[method]=function(a){return function(){rudderanalytics.push([a].concat(Array.prototype.slice.call(arguments)))}}(method)}rudderanalytics.load(<WRITE_KEY>,<DATA_PLANE_URL>),rudderanalytics.page();
</script>

<script src="https://cdn.rudderlabs.com/v1.1/rudder-analytics.min.js"></script>

Non-minified code

<script>
  rudderanalytics = window.rudderanalytics = [];

  var methods = [
    'load',
    'page',
    'track',
    'identify',
    'alias',
    'group',
    'ready',
    'reset',
    'getAnonymousId',
    'setAnonymousId',
  ];

  for (var i = 0; i < methods.length; i++) {
    var method = methods[i];
    rudderanalytics[method] = (function (methodName) {
      return function () {
        rudderanalytics.push([methodName].concat(Array.prototype.slice.call(arguments)));
      };
    })(method);
  }
  rudderanalytics.load(YOUR_WRITE_KEY, DATA_PLANE_URL);
  //For example,
  //rudderanalytics.load("1Qb1F3jSWv0eKFBPZcrM7ypgjVo", "http://localhost:8080");
  rudderanalytics.page();
</script>

<script src="https://cdn.rudderlabs.com/v1.1/rudder-analytics.min.js"></script>

In all the above versions, there is an explicit page call at the end. This is added to ensure that whenever the SDK loads in a page, a page call is sent. You can remove this call completely or modify it with the extra page properties to suit your requirement. You can also add page calls in your application in places not tied directly to page load, e.g., virtual page views, page renders on route change such as in SPAs, etc.

Write key and data plane URL

To integrate and initialize the JavaScript SDK, you will need the source write key and the data plane URL.

Alternative installation using NPM

Although we recommend using the snippets mentioned above to use the JavaScript SDK with your website, you can also use this NPM module to package RudderStack directly into your project.

To install the SDK via npm, run the following command:

npm install rudder-sdk-js --save

Note that this NPM module is only meant to be used for a browser installation. If you want to integrate RudderStack with your Node.js application, refer to the RudderStack Node.js repository.

IMPORTANT: Since the module exports the related APIs on an already-defined object combined with the Node.js module caching, you should run the following code snippet only once and use the exported object throughout your project:

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

You can also do this with ES5 using the require method, as shown:

var rudderanalytics = require("rudder-sdk-js");
rudderanalytics.load(<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 as shown:

rudderanalytics.load(<YOUR_WRITE_KEY>, <DATA_PLANE_URL>, {loadIntegration:  false})

For more information on the load() method, refer to the detailed JavaScript SDK documentation.

A few important things to note:

  • The SDK expects the destination global queue or function for pushing the events is already present for the particular destination/s.
  • Currently, loadIntegration is supported only for Amplitude and Google Analytics.
  • The JavaScript SDK expects window.amplitude and window.ga to be already defined by the user separately for the sending the events to these destinations.

Exported APIs

The APIs exported by the module are:

  • load
  • ready
  • identify
  • alias
  • page
  • track
  • group
  • reset
  • getAnonymousId
  • setAnonymousId

Sample implementations

Refer to the following projects for a detailed walk-through of the above steps:

Supported browser versions

Browser Supported Versions
Safari v7 or later
IE v10 or later
Edge v15 or later
Mozilla Firefox v40 or later
Chrome v37 or later
Opera v23 or later
Yandex v14.12 or later

If the SDK does not work on the browser versions that you are targeting, verify if adding the browser polyfills to your application solves the issue.

Identifying users

The identify call lets you identify a visiting user and associate them to their actions. It also lets you record the traits about them like their name, email address, etc.

A sample identify() call is shown below:

rudderanalytics.identify(
  '12345',
  {
    email: '[email protected]',
  },
  {
    page: {
      path: '',
      referrer: '',
      search: '',
      title: '',
      url: '',
    },
  },
  () => {
    console.log('in identify call');
  },
);

In the above example, the user-related information like the userId and email along with the contextual information is captured.

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

For more information on how to use the identify call, refer to the JavaScript SDK documentation.

Tracking user actions

The track call lets you record the customer events, i.e. the actions that they perform, along with any associated properties.

A sample track call is shown below:

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

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

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

The ready API

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

An example is shown in the following snippet:

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

For more information on the other supported methods, refer to the JavaScript SDK APIs.

Self-Hosted control 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 self-hosting the control plane using the RudderStack Control Plane Lite utility, your load call will look like the following:

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

More information on how to get the CONTROL_PLANE_URL can be found here.

For detailed technical documentation and troubleshooting guide on the RudderStack’s JavaScript SDK, check out our docs.

Adding your own integrations

You can start adding integrations of your choice for sending the data through their respective web (JavaScript) SDKs.

How to build the SDK

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

    • npm run build:browser: This outputs rudder-analytics.min.js.
    • npm run build:npm: This outputs rudder-sdk-js folder that contains the npm package contents.
    • npm run build:integration:all: This outputs integrations folder that contains the integrations.

We use rollup to build our SDKs. The configuration for it is present in rollup-configs folder.

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

Usage in Chrome Extensions

RudderStack JS SDK can be used in Chrome Extensions with manifest v3, both as a content script or as a background script service worker.

For examples and specific details look into Chrome Extensions Usage

Contribute

We would love to see you contribute to this project. Get more information on how to contribute here.

Contact us

For more information on any of the sections covered in this readme, you can contact us or start a conversation on our Slack channel.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].