Initializing the PageSense Node.js SDK

Initializing the PageSense Node.js SDK

Before you start  

PageSenseClient is the core interface for running experiments. Everything — activating experiments, tracking goals, evaluating variations — runs through this client. You need to initialise it once before any of that works.
Initialization is async and happens in a single step. The SDK calls the PageSense server, fetches your project settings, and sets up the client. Project settings include:
  • All FullStack experiments configured in your project.
  • The current status of each experiment.
  • Traffic allocation and audience targeting rules.

Method  

  1. const pageSenseClient = await PageSenseClientBuilder.createNewPageSenseClient(
  2.   accountId,
  3.   sdkKey,
  4.   projectName,
  5.   sdkOptions
  6. );

Parameters  

Parameter
Type
Description
accountId
String
Your PageSense account identifier.
sdkKey
String
Your environment's SDK key. Find it in SDK Settings on the FullStack project configuration page.
projectName
String
The PageSense project that holds your experiments.
sdkOptions
Object
Optional. Configure logging, polling, and request behaviour here.

What happens under the hood  

When createNewPageSenseClient() runs:
  1. The SDK requests project settings from the PageSense server.
  2. It parses the experiment configurations, targeting rules, and traffic allocation.
  3. It initialises and returns the PageSenseClient instance.
From that point, the client is ready to evaluate experiments and track events.
If initialization fails — wrong credentials, network issues, missing config — the SDK returns null or throws an error.

Usage Example  

  1. const { PageSenseClientBuilder } = require('@zoho/pagesense-node-sdk');
  2. async function initializeSDK() {
  3.   const accountId   = "YOUR_ACCOUNT_ID";
  4.   const sdkKey      = "YOUR_SDK_KEY";
  5.   const projectName = "YOUR_PROJECT_NAME";
  6.   const sdkOptions  = {};
  7.   const pageSenseClient = await PageSenseClientBuilder.createNewPageSenseClient(
  8.     accountId,
  9.     sdkKey,
  10.     projectName,
  11.     sdkOptions
  12.   );
  13.   if (!pageSenseClient) {
  14.     console.error("PageSense SDK initialization failed.");
  15.     return;
  16.   }
  17.   console.log("PageSense SDK initialized successfully.");
  18. }
  19. initializeSDK();

A few things to keep in mind  

  • createNewPageSenseClient() is async — always await it or use a Promise chain.
  • Don't call experiment or tracking APIs before the client is ready.
  • Handle the failure case — a null return means something went wrong during in it.





We’ve designed this documentation to guide you every step of the way. If you need further assistance or have any questions, don’t hesitate to contact us at support@zohopagesense.com - we’re always here to help!