Conditionally initialize Pendo

Last updated:

This article provides examples for conditionally initializing Pendo as part of the installation process. For more information about installing Pendo, see the Developer's guide to implementing Pendo using the install script.

Warning: The examples in this article use custom code snippets to help you extend the capabilities of Pendo products. However, there is no warranty, SLA, or support available for their use. All snippets are free to use, but custom code is outside the remit of our Technical Support team's responsibilities. If you have a request for creating new snippets, modifying existing snippets, or customizing functionality, contact Pendo Professional Services (services@pendo.io) instead. These requests require a custom services contract with Pendo.

Use cases

In a standard Pendo installation, you initialize Pendo for all visitors to your application and then use include and exclude lists to filter out the analytics from specific Visitor IDs, Account IDs, or domains. This works well for excluding visitors from test or staging environments, and to exclude internal users. If you want to exclude a group of visitors and can't use Visitor ID, Account ID, or domain, you can conditionally initialize Pendo.

Additionally, in a standard installation, Pendo loads asynchronously with the rest of your application, which means, when a page loads, initialization can occur before all your variables are created and defined. If you want Pendo to wait until all of the variables you pass are defined before initializing, you can conditionally initialize Pendo. Otherwise, in a standard Pendo installation, Pendo assumes all of the variables that are passed to Pendo by your application are available and defined before Pendo begins its initialization.

Example 1. Conditionally initializing for paying visitors

This example covers how to conditionally initialize Pendo for specific visitors. In this example, these visitors are those on a paid plan with your application, but you can target any visitor trait in your install script for conditional intialization.

Situation

In the AcmeCorp CRM app, visitors can either be part of a free plan or a paid plan. You only want to initalize Pendo for visitors on the paid plan. Because free visitors belong to multiple accounts and all are active on you primary (production) domain, we use an exclude list to exclude their activity. 

Task

In the above-described situation, you want to conditionally initialize Pendo so that it only runs when paid users sign in.

Action

To do this, you must update your Pendo install script so that it includes some conditional logic to decide when Pendo should and shouldn't initialize. The pseudocode below illustrates how you might do this: 

if (user.type=='paid') {
 pendo.initialize()...
else {
}

In this example, you have a visitor with a trait of type that can be either free or paid. You're wrapping the Pendo initialization code in an if/else statement to check the type of visitor trait and then only initializing Pendo if this is equal to paid.

Outcome

The above code excludes all free users from Pendo while continuing to use Pendo for all paid users. 

Example 2. Conditionally initializing after a variable is populated

This example covers how to conditionally initialize Pendo so that, when a page loads, Pendo waits until all variables have finished populating before initializing. In this example, you only want Pendo to initialize after a particular variable is created and defined, but you can do this for multiple variables.

Situation

In the AcmeCorp CRM app, visitor data is passed through a variable that's created when the page loads, and this variable can take time to load. You want to ensure that no important visitor or account information is missed as a result of Pendo initializing before the variable's values are ready to be passed to Pendo.

Task

In the above-described situation, you want to conditionally initialize Pendo so that it only runs when the variable has been defined. 

Action

To do this, you must update your Pendo install script so that it includes some conditional logic to decide when Pendo should and shouldn't initialize. The pseudocode below illustrates how you might do this: 

if (user.visitorId && user.accountId && user.otherVariable) {
 pendo.initialize()...
else {
}

In this example, you have a visitor with a trait of vistorId that you want to define before passing it to Pendo, as well as accountId and an otherVariable. You're wrapping the Pendo initialization code in an if/else statement to check that these traits exist and are neither null nor undefined, and then only initializing Pendo if those variables are populated, thus returning true.

Outcome

The above code ensures that all variables are present before initializing Pendo so that all of the vital visitor and account information passed from your application to Pendo is included.

Was this article helpful?
1 out of 1 found this helpful