injectChatScriptByVersion()
This function should be called before any other API function. It will download the SDK from an S3 bucket, then inject the SDK into the webpage and attach it to the DOM. There is a callback that you can use to determine when the SDK has finished loading. There are several optional parameters that are described in detail below. Here is the function API and some example usage:
/**
* Injects the chat SDK into the document based on the version. Simplifies the injection process by
* removing the need to pass in the url. Needs to be called before calling initialize()
*
* @param id - a unique id that will be set as the script id
* @param version - the version of the SDK you wish to load (v1, v2, v3, etc)
* @param environment - the env to load for chat (development vs production)
* @param async(optional) - whether the script should be loaded asynchronously. default: false
* @param defer(optional) - whether the script should be loaded with defer. default: false
* @param nonce(optional) - optional custom nonce to use for the script
* @param callback - Any function that should be run after the script loads. initialize()
* is commonly passed here to init only after the script fully loads
*/
export const injectChatScriptByVersion = async (
{
id,
version = 'v1',
environment,
async = false,
defer = false,
nonce = undefined,
}: {
id: string;
version?: 'v1';
environment: 'development' | 'production';
async?: boolean;
defer?: boolean;
nonce?: string;
},
callback?: () => Promise<void>
);
// Example usage:
const someFunction = () => {
await injectChatScriptByVersion(
{
id: "remote-expert-sdk",
version: "v1",
environment: "development",
},
// This callback is called once the script has been injected
async () => {
console.log('Chat has been injected.');
}
);
}