Search code examples
ruby-on-railsvue.jswistia

How to bind Wistia video play/end event in vuejs


I am using Wistia script to embed a video into my vuejs page, the video is embeded successfull using their fallback script as below:

        <div class="wistia_responsive_padding" style="padding:55.94% 0 0 0;position:relative;">
          <div class="wistia_responsive_wrapper" style="height:100%;left:0;position:absolute;top:0;width:100%;">
            <iframe :src="defaultWistiaVideo" title="Welcome Video" allow="autoplay; fullscreen" allowtransparency="true" frameborder="0" scrolling="no" class="wistia_embed" name="wistia_embed" allowfullscreen msallowfullscreen width="100%" height="100%"></iframe>
          </div>
        </div>

Now I want to listen on play and end event to change user's watching status to watching/watched. I am adding both below scripts like their guide but it does not work.

Note that these script will work if I put them into a separated html file, not vuejs component

I am not sure how to set these events properly using vuejs.

<div id="wistia_abcde12345" class="wistia_embed" style="width:640px;height:360px;">&nbsp;</div>
<script src="//fast.wistia.com/assets/external/E-v1.js"></script>
<script>
wistiaEmbed = Wistia.embed("abcde12345");
wistiaEmbed.hasData(function() {
  wistiaEmbed.bind("play", function() {
    console.log("video played", wistiaEmbed.name());
  });
});
</script>
<script src="//fast.wistia.com/assets/external/E-v1.js" async></script>
<div class="wistia_embed wistia_async_abcde12345" style="width:640px;height:360px;"></div>
<script>
window._wq = window._wq || [];
_wq.push({ id: "abcde12345", onReady: function(video) {
  video.bind("play", function() {
    console.log("video played", video.name());
  });
}});
</script>

Solution

  • You should place  wistia event listening script on mounted function, like this:

      mounted() {
        window._wq = window._wq || [];
        let context = this;
        _wq.push({ id: '_all', onReady: function(video) {
          video.bind('play', function() {
            // event handler
          });
    
          video.bind('end', function() {
            // event handler
          });
        }});
      }