APIs & Integrations

Siyabonga
Membre

Prevent bot from loading immediately

Résolue

if (window.HubSpotConversations) {
console.log('The api is ready already');

window.hsConversationsSettings = {
loadImmediately: false,
};
}

 

Hi everyone,

I am trying to prevent my chatbot from loading immediately on my page using the above code, but for some reason the code doesn't work, the bot is loading on the page.

 

bot.PNG

0 Votes
1 Solution acceptée
WendyGoh
Solution
HubSpot Employee
HubSpot Employee

Prevent bot from loading immediately

Résolue

Hi @Siyabonga,

 

Thank you for sharing the page.

 

On my end, I have clone out the page and name it as hstest that is using a clone template and a clone javascript file. I made two changes here:

 

#1 When looking at the placement of your javascript file, I noticed you added both the tracking code and chatbot-script javascript in the 'Additional markup before </body>'. I shifted it to the 'Additional <head> markup' so that the script is loaded before anything else. 

 

#2 In the javascript file, I removed $(document).ready(function() { 

 

Now, if you look at this page - https://campaign.bupaglobal.com/test-page-0-0-0?hs_preview=vJdsHqyx-18747364193?adsfd > It is working! > I tried refresh multiple times and the chat bot didn't appear.

 

Do get it a try and see if it's working well for you.

Voir la solution dans l'envoi d'origine

0 Votes
5 Réponses
WendyGoh
HubSpot Employee
HubSpot Employee

Prevent bot from loading immediately

Résolue

Hi @Siyabonga,

 

I hope all is well with you 😄

 

As mentioned on this documentation - HubSpot Conversations JavaScript API:

"The API is housed in the window.HubSpotConversations object. All available methods can be accessed via this object. The HubSpot script loader on your page will create this object for you, but it may not be available immediately. To defer accessing the API until it has been initialized, you may use the window.hsConversationsOnReady helper. "

 

And so, can you try the following code and see if this works for you:

<script >
        if (window.HubSpotConversations) {
            window.hsConversationsSettings = {
                loadImmediately: false,
            };
        }else {
            window.hsConversationsOnReady = [
                () => {
                    window.hsConversationsSettings = {
                        loadImmediately: false,
                    };
                },
            ];
        }
</script>
0 Votes
Siyabonga
Membre

Prevent bot from loading immediately

Résolue

Hi @WendyGoh

 

Thank you for the reply.

 

I have tried the code you provided and I am still having a problem.

 

This is the code in my script file.

$(document).ready(function() {
  console.log('bot test 1');

  if (window.HubSpotConversations) {
    console.log('The api is ready already');
    window.hsConversationsSettings = {
      loadImmediately: false,
    };
  }else {
    window.hsConversationsOnReady = [
      () => {
        console.log('The api is now ready');
        window.hsConversationsSettings = {
          loadImmediately: false,
        };
      },
    ];
  }
});

I have the example running here: https://campaign.bupaglobal.com/test-page 

 

The issues I am seeing are that:

 

1. The code to prevent the bot from loading inside the "if (window.HubSpotConversations) {" statement, does not work.  The bot still shows when we go into that IF statement.

2. The "window.hsConversationsOnReady" deferrer is not always called. (try refreshing a few times and you'll see that the message I am logging does not always come through)

 

 

 

 

0 Votes
WendyGoh
Solution
HubSpot Employee
HubSpot Employee

Prevent bot from loading immediately

Résolue

Hi @Siyabonga,

 

Thank you for sharing the page.

 

On my end, I have clone out the page and name it as hstest that is using a clone template and a clone javascript file. I made two changes here:

 

#1 When looking at the placement of your javascript file, I noticed you added both the tracking code and chatbot-script javascript in the 'Additional markup before </body>'. I shifted it to the 'Additional <head> markup' so that the script is loaded before anything else. 

 

#2 In the javascript file, I removed $(document).ready(function() { 

 

Now, if you look at this page - https://campaign.bupaglobal.com/test-page-0-0-0?hs_preview=vJdsHqyx-18747364193?adsfd > It is working! > I tried refresh multiple times and the chat bot didn't appear.

 

Do get it a try and see if it's working well for you.

0 Votes
Siyabonga
Membre

Prevent bot from loading immediately

Résolue

Hi @WendyGoh 

 

The code works well for me, however I am still having a problem with my first point .

 

1. The code to prevent the bot from loading inside the "if (window.HubSpotConversations) {" statement, does not work.  The bot still shows when we go into that IF statement. 

 

 

0 Votes
WendyGoh
HubSpot Employee
HubSpot Employee

Prevent bot from loading immediately

Résolue

Hi @Siyabonga,

 

Glad to know that it's working well for you.

 

That's because the HubSpot script loader on your page will create the window.HubSpotConversations for you, but it may not be available immediately and hence we need another else statement to defer accessing the API until it has been initialized by using window.hsConversationsOnReady.

0 Votes