skip to Main Content

How do I add an id tag, when a person clicks presses the Play Button on HTML video? We are trying to track usage.

<video width="728" height="410" controls>
   <source src={testURL} type="video/mp4" />
       Your browser does not support the video tag.
</video>

2

Answers


  1. You can add an event listener on your video tag and run your code whenever the video starts playing.

    var video = document.querySelector("video"); // select video element
    
    video.addEventListener("play", (event) => {
       // your tracking code goes here
    });
    
    Login or Signup to reply.
  2. const video = document.querySelector('video');
    
    video.addEventListener('play', () => {
      video.setAttribute('id', 'video-playing');
      // send tracking data here
    });
    

    You can add an event listener to the play button of the HTML video and then use the setAttribute method to add an id tag to the video element. If this help!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search