# Prevent bot from loading immediately

**URL:** <https://community.hubspot.com/t/prevent-bot-from-loading-immediately/16819>\
**Category:** APIs & Integrations\
**Tags:** timeline-and-extensions-apis\
**Created:** [October 18, 2019, 3:04pm UTC](https://community.hubspot.com/t/prevent-bot-from-loading-immediately/16819 "2019-10-18T15:04:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siyabonga](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/siyabonga/32/15622_2.png) [@Siyabonga](https://community.hubspot.com/u/Siyabonga)\
**Post date:** [October 18, 2019, 3:04pm UTC](https://community.hubspot.com/t/prevent-bot-from-loading-immediately/16819/1 "2019-10-18T15:04:25Z")

</div>

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](https://d2qpr2jzcazej7.cloudfront.net/original/3X/4/b/4b3cdbb3970cdedb6c66a00679c56166645e9233.png)

---

<div class="post-metadata">

**Author:** ![WendyGoh](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/wendygoh/32/42320_2.png) [@WendyGoh](https://community.hubspot.com/u/WendyGoh)\
**Post date:** [October 21, 2019, 4:32am UTC](https://community.hubspot.com/t/prevent-bot-from-loading-immediately/16819/2 "2019-10-21T04:32:38Z")

</div>

Hi @Siyabonga,

I hope all is well with you 😄

As mentioned on this documentation - [HubSpot Conversations JavaScript API:](https://developers.hubspot.com/docs/methods/conversations_api/hubspot-conversations-javascript-api#loadImmediately)

"_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`](https://developers.hubspot.com/docs/methods/conversations_api/hubspot-conversations-javascript-api#hsConversationsOnReady) helper._ "

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

```auto
<script >
 if (window.HubSpotConversations) {
 window.hsConversationsSettings = {
 loadImmediately: false,
 };
 }else {
 window.hsConversationsOnReady = [
 () => {
 window.hsConversationsSettings = {
 loadImmediately: false,
 };
 },
 ];
 }
</script>

```

---

<div class="post-metadata">

**Author:** ![Siyabonga](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/siyabonga/32/15622_2.png) [@Siyabonga](https://community.hubspot.com/u/Siyabonga)\
**Post date:** [October 21, 2019, 7:02am UTC](https://community.hubspot.com/t/prevent-bot-from-loading-immediately/16819/3 "2019-10-21T07:02:47Z")

</div>

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.

```auto
$(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](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)

---

<div class="post-metadata">

**Author:** ![WendyGoh](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/wendygoh/32/42320_2.png) [@WendyGoh](https://community.hubspot.com/u/WendyGoh)\
**Post date:** [October 21, 2019, 8:11am UTC](https://community.hubspot.com/t/prevent-bot-from-loading-immediately/16819/4 "2019-10-21T08:11:05Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![Siyabonga](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/siyabonga/32/15622_2.png) [@Siyabonga](https://community.hubspot.com/u/Siyabonga)\
**Post date:** [October 21, 2019, 5:01pm UTC](https://community.hubspot.com/t/prevent-bot-from-loading-immediately/16819/5 "2019-10-21T17:01:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![WendyGoh](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/wendygoh/32/42320_2.png) [@WendyGoh](https://community.hubspot.com/u/WendyGoh)\
**Post date:** [October 22, 2019, 1:33am UTC](https://community.hubspot.com/t/prevent-bot-from-loading-immediately/16819/6 "2019-10-22T01:33:39Z")

</div>

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.
