Pendo with React SPA

Hi,

Looking at your SPA guide, we need to install the initialise after the user has logged in, but it needs to be run on every page. 

Does this mean that we can install this snippet to any common component once the user is logged in, such as the menu bar?

Secondly, if we put this into a react file, there is no reference to import. How do we reference Pendo from within a react file or should we be doing something differently?

0

Comments

4 comments
  • Did you every figure this out? In case some one else comes across this post. I had the same question and after hours of playing around I was able to get Pendo working with in a ReactJS SPA.

    you need to  include the common snippet in to your index.html, then add the `pendo.initialize` code in to a common component. (eg: component that loads after user logins)

    The caveat here is you need to use `window.pendo.initialize` or else it will say that pendo is undefined.

    9
  • I am having this same problem, but I can't seem to use window.pendo either, because Typescript doesn't understand that it exists. 

    0
  • For Typescript you need to create a definition file for pendo.
    The following is not a Pendo Official library but has worked for me.

    DefinitelyTyped/types/pendo-io-browser at master · DefinitelyTyped/DefinitelyTyped (github.com)

    0
  • load Pendo script dynamically from the CDN using a custom function, then initialize Pendo after the user logged in.
    This method works without using pendo-io-brower package.

    src/pendo.d.ts (create a custom typing file to let TypeScript know about the global pendo object;

    declare global{
    interface Window {
    pendo:any;
    }
    }
    export {};

    src/pendoUtils.ts (pendo utility functions)

    export const loadPendoScript = () => {
    const script = document.createElement('script');
    script.scr = 'https://cdn.pendo.io/agent/static/YOUR_PENDO_API_KEY/pendo.js';
    script.async = true;
    document.body.appendChild(script);
    };

    Or
    directly add the script in index.html file.
    <script type="text/javascript" src="https://cdn.pendo.io/agent/static/YOUR_PENDO_API_KEY/pendo.js"></script>
    export const initializePendo = (userData:any) => {
    if(window.pendo){
    window.pendo.initialize({
    apiKey: 'YOUR_PENDO_API_KEY',
    visitor: {id:userData.Id, },
    account: {id:userData.Id, }
    });
    }
    };

    In react component

    userEffect(() => {
    loadPendoScript();
    if(isLoggedIn) {
    getUserdata().then(data => {
    initializePendo(data.userinfo);
    });
    }
    },[isLoggedIn]);
    1

Please sign in to leave a comment.

Didn't find what you were looking for?

New post