How to control HubSpot Video with a little JavaScript
Posted: Mon Feb 17, 2025 3:51 am
The UI and operation of HubSpot Video are similar to HTML video tags, but the big difference is that it allows you to analyze the number of views and video retention. For more information, please refer to the official website .
The communication process is handled by HubSpot's video delivery server, which is user-friendly because it only retrieves the data necessary for playback.
It would be great to take advantage of HubSpot Video's ease of use, but it becomes very difficult when trying to do something special.
For example, let's say you want to implement a process that "stops playback when the video area goes off screen."
This is relatively easy to implement with a normal video tag, but HubSpot Video is output with an iframe tag. If you're familiar with JavaScript, you might think at this point, "I might not be able to implement this..."
This is because, for security reasons, many conditions must be met in order to operate within an iframe using JavaScript.
However, HubSpot has provided an API that makes it easy to control the video player from JavaScript!
I thought that if there was an API, I could do anything! When I looked into it, I found that the only operations I could do were play and stop .
Please note that the method described here is not 100% secure as it is not documented, and also that HubSpot is updated daily so there is a possibility that it may become unusable at some point.
table of contents
1.About MessageEvent
2.Controlling HubSpot Video with JavaScript
3.Conclusion
About MessageEvent
Before we get to the main topic, we need to understand the MessageEvent that is notified within JavaScript.
MessageEvent is notified on pages that use videos, forms, etc.
You can get notifications by embedding the following code in your JavaScript.
window.addEventListener('message', function(e){
console.log(e)
});
JavaScript
The actual data looks like bahamas number data this:
fig03
The important data data:is the part in the red frame, which contains various data such as the video ID.
The data we use to determine data.type:"PLAYER_READY"this is a notification that the video is ready to play .
MessageEvent looks like this, data.typeand various values are entered into .
Therefore, once you understand the mechanism, you can create special processing since forms and other things have similar notifications.
Now let's move on to the main topic of this article: controlling the video using JavaScript.
The communication process is handled by HubSpot's video delivery server, which is user-friendly because it only retrieves the data necessary for playback.
It would be great to take advantage of HubSpot Video's ease of use, but it becomes very difficult when trying to do something special.
For example, let's say you want to implement a process that "stops playback when the video area goes off screen."
This is relatively easy to implement with a normal video tag, but HubSpot Video is output with an iframe tag. If you're familiar with JavaScript, you might think at this point, "I might not be able to implement this..."
This is because, for security reasons, many conditions must be met in order to operate within an iframe using JavaScript.
However, HubSpot has provided an API that makes it easy to control the video player from JavaScript!
I thought that if there was an API, I could do anything! When I looked into it, I found that the only operations I could do were play and stop .
Please note that the method described here is not 100% secure as it is not documented, and also that HubSpot is updated daily so there is a possibility that it may become unusable at some point.
table of contents
1.About MessageEvent
2.Controlling HubSpot Video with JavaScript
3.Conclusion
About MessageEvent
Before we get to the main topic, we need to understand the MessageEvent that is notified within JavaScript.
MessageEvent is notified on pages that use videos, forms, etc.
You can get notifications by embedding the following code in your JavaScript.
window.addEventListener('message', function(e){
console.log(e)
});
JavaScript
The actual data looks like bahamas number data this:
fig03
The important data data:is the part in the red frame, which contains various data such as the video ID.
The data we use to determine data.type:"PLAYER_READY"this is a notification that the video is ready to play .
MessageEvent looks like this, data.typeand various values are entered into .
Therefore, once you understand the mechanism, you can create special processing since forms and other things have similar notifications.
Now let's move on to the main topic of this article: controlling the video using JavaScript.