There is an explicit page call at the end in all above snippets. This is to ensure that a page call is sent whenever the SDK loads on a page. You can remove this call or modify it with any extra page properties. You can also add page calls in your application in places not tied directly to page load, for example, virtual page views and page renders on route change, such as in single-page applications.
Using NPM
While it is recommended to use the above method to integrate the JavaScript SDK with your website, you can alternatively use the NPM module for packaging RudderStack directly into your project.
To install the JavaScript SDK via NPM, run the following command:
npm install rudder-sdk-js --save
Use this NPM module only for browser installation. To integrate RudderStack with your Node.js apps, refer to the RudderStack Node.js SDK documentation.
Since the NPM module exports the related APIs on an already-defined object combined with the Node.js module caching, you should run the following code snippet once and use the exported object throughout your project:
import*asrudderanalyticsfrom"rudder-sdk-js";rudderanalytics.load(WRITE_KEY,DATA_PLANE_URL);rudderanalytics.ready(()=>{console.log("We are all set!!!");});export{rudderanalytics};
Alternatively, you can do this with ES5 using the require method:
varrudderanalytics=require("rudder-sdk-js");rudderanalytics.load(WRITE_KEY,DATA_PLANE_URL);rudderanalytics.ready(()=>{console.log("We are all set!!!");});exports.rudderanalytics=rudderanalytics;
The related APIs exported by the module are load, ready, identify, alias, page, track, group, reset, getAnonymousId, setAnonymousId, getUserId, getUserTraits, getGroupId, and getGroupTraits.
Refer to the sample Angular and React projects for a detailed walkthrough of the above steps.
The installation code snippets discussed above perform the following actions:
Creates an array to store the events until the analytics object is ready.
Stores the following methods to replay them when the analytics object is ready:
Resets the information related to the previously identified user.
getAnonymousId
Fetches the current anonymousId.
setAnonymousId
Sets the anonymousId.
getUserId
Fetches the current userId.
getUserTraits
Fetches the current user traits.
getGroupId
Fetches the current groupId.
getGroupTraits
Fetches the current group traits.
startSession
Starts a new session.
endSession
Clears the sessionId and ends the current session.
getSessionId
Fetches the session ID of the current session. Returns null if the session ID is not available.
Loads the analytics object with the specified write key.
Makes a page()call to track the page views. It captures the whole URL including the UTM parameters as part of the page call payload, such as path, referrer, search, title, and URL. Refer to the page method to override these properties.
Step 2: Check ready state
The JavaScript SDK provides the ready API with a callback parameter that triggers when the SDK is done initializing itself and the other third-party native SDK destinations.
The identify method lets you identify a user and associate them with their actions. It also enables you to record any traits about them like their name, email, etc.
A sample identify call is shown below:
rudderanalytics.identify("1hKOmRA4el9Zt1WSfVJIVo4GRlm",{firstName:"Alex",lastName:"Keener",email:"alex@example.com",phone:"+1-202-555-0146"},{page:{path:"/best-seller/1",referrer:"https://www.google.com/search?q=estore+bestseller",search:"estore bestseller",title:"The best sellers offered by EStore",url:"https://www.estore.com/best-seller/1"}},()=>{console.log("identify call");});
The JavaScript SDK captures the userId, email and the contextual information from the above snippet.
The anonymous visitors are automatically assigned an anonymousId. Refer to the Anonymous ID section for more information.
Step 4: Track user actions
The method lets you capture user events along with the associated properties.
The JavaScript SDK captures the Order Completed event along with revenue, currency, and anonymousId from the above snippet.
You can use the track method to track various success metrics for your website like user signups, item purchases, article bookmarks, and more.
Supported browsers
The JavaScript SDK supports the following browsers and their corresponding versions:
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
You can try adding the browser polyfills to your application if the SDK does not work on your browser.
Single-page application
The JavaScript SDK makes a page call after its initialization (explicitly called at the end of the installation script). However, in the case of a single-page application (SPA) where a route change does not reload the page, you need to make the page call explicitly after the route change on the frontend. For more information, refer to the RudderStack-Next.js Integration guide.
This site uses cookies to improve your experience while you navigate through the website. Out of
these
cookies, the cookies that are categorized as necessary are stored on your browser as they are as
essential
for the working of basic functionalities of the website. We also use third-party cookies that
help
us
analyze and understand how you use this website. These cookies will be stored in your browser
only
with
your
consent. You also have the option to opt-out of these cookies. But opting out of some of these
cookies
may
have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This
category only includes cookies that ensures basic functionalities and security
features of the website. These cookies do not store any personal information.
This site uses cookies to improve your experience. If you want to
learn more about cookies and why we use them, visit our cookie
policy. We'll assume you're ok with this, but you can opt-out if you wish Cookie Settings.