Skip to content

JavaScript API ​

Overview ​

In some advanced embedding cases, such as single-page apps (SPAs), there is a need to control the Videobot dynamically after the page has loaded. For instance, the Videobot should only be shown in certain pages or situations, but the page does not refresh between user actions.

One option for solving these cases is using Videobot's JavaScript-based API.

Methods ​

The main way to interact with a Videobot on the page is through the global object window.Videobot, which is created after the initial Videobot script has loaded.

Currently the following methods are considered publicly available:

MethodDescription
window.Videobot.refresh()Reload Videobot and its configuration
window.Videobot.remove()Remove Videobot from the page
window.Videobot.open()Open Videobot iframe
window.Videobot.close()Close Videobot iframe
window.Videobot.show()Show Videobot widget & iframe (if previously hidden)
window.Videobot.hide()Hide Videobot widget & iframe (and pause video)
window.Videobot.enableAnalytics()Enable analytics events
window.Videobot.disableAnalytics()Disable analytics events
window.Videobot.setGoogleConsent(value)Define user consent for Google Analytics

WARNING

These methods will not be available before the Videobot script has loaded, see the Examples section for a possible solution.

Messages ​

In addition to the callable methods, the Videobot itself communicates with the widget and the parent page with message events. To learn more about how they work, see this guide on MDN.

Messages from the Videobot are objects with an action and optional per-action value fields:

js
{
	action: 'VIDEO_BOT_LOADED',
	...
}

The actions that can be used are:

MessageDescription
VIDEO_BOT_LOADEDThe Videobot has initialized and will be shown
VIDEO_BOT_OPENThe Videobot has been opened
VIDEO_BOT_CLOSEThe Videobot has been closed
VIDEO_BOT_ANALYTICS_EVENTAn analytics event from within the Videobot. Requires the Iframe forwarding feature to be enabled

INFO

Always check the origin of the incoming event to ensure it comes from Videobot

Examples ​

If we wanted to only show the Videobot on the /campaign page of our site, we could do the following:

js
if (window.location.pathname === '/campaign') {
	window.Videobot.show()
} else {
	window.Videobot.hide()
}

As another example, we could dynamically allow enabling analytics based on a custom consent button:

html
<button onclick="window.Videobot.enableAnalytics()">Allow analytics</button>

If we wanted to use the above examples immediately after the page has loaded, we should wait for the DOMContentLoaded event to fire:

js
document.addEventListener('DOMContentLoaded', (event) => {
	window.Videobot.hide()
})

Alternatively the Videobot script can be imported as a module:

html
<script type="module">
  import { Videobot } from 'https://videobot.com/embed/videobot.mjs'
  Videobot.hide()
</script>
```