Quick Start Guide
Step-by-step guide on installing and using the RudderStack JavaScript SDK on your website.
Last updated
Was this helpful?
Step-by-step guide on installing and using the RudderStack JavaScript SDK on your website.
Last updated
Was this helpful?
This guide is aimed at helping you quickly get up and running with the JavaScript SDK.
The following sections cover the SDK installation steps and details on using the SDK to identify your website users and track their actions.
To integrate the SDK with your website, place the following version of the code in the <head>
section of your website.
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, refer to the or versions of the snippet below.
The above code snippet does the following:
Creates an array to store the events until the analytics object is ready.
Stores a list of methods to replay them when the analytics object is ready. These methods are:
load()
Loads rudder-analytics.js
with your write key.
page()
Records page views whenever a user visits a page.
track()
Tracks user actions and important events.
identify()
Associates users and their traits or actions.
reset()
Resets the userid
and the associated traits.
Loads the analytics object with your write key.
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.
To integrate and initialize the JavaScript SDK, you need the source write key and the data plane URL.
To install the SDK via npm, run the following command:
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:
You can also do this with ES5 using the require
method, as shown in the following snippet:
The related APIs exported by the module are:
load
ready
identify
alias
page
track
group
reset
getAnonymousId
setAnonymousId
Refer to the following projects for a detailed walk-through of the above steps:
Browser
Supported Versions
Safari
v7 or later
IE
v10 or later
Edge
v80 or later
Mozilla Firefox
v47 or later
Chrome
v54 or later
Opera
v43 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.
identify()
methodThe 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:
There is no need to call identify()
for anonymous visitors to your website. Such visitors are automatically assigned an anonymousId
.
track()
The track
call lets you record the customer events, i.e. the actions that they perform, along with any properties associated with them.
A sample track
call is shown below:
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.
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 below:
Makes the page()
call to track the page view. It auto-captures properties such as path
, referrer
, search
, title
, and URL
. If you want to override them, refer to the API method.
To get the source write key, follow .
To get the data plane URL, follow .
While it is recommended to use the above-mentioned snippets to use the JavaScript SDK with your website, you can also use the to package RudderStack directly into your project.
This NPM module is only meant to be used for a browser installation. If you want to integrate RudderStack with a Node.js application, refer to the .
In the above example, the user-related information like the userId
and email
along with the is captured.
For more information, refer to the section of the SDK documentation.
For more information on the JavaScript SDK API methods, refer to the section of the SDK documentation.
For more information on any of the sections covered in this guide, you can or start a conversation in our community.
If you come across any issues while using this SDK, feel free to submit them on our .