Tracking Mouse Clicks

When using the IMA SDK, there are two separate video players, your content video player and the ad player. The SDK automatically manages overlaying the ad player over the content video player. As a result, adding event listeners to the video player directly will not work. One workaround is to show and hide the ad container, but this can interfere with SDK functionality. A better solution is to wrap both players in a parent div and attach event listeners to that. This guide shows you how.

Prerequisites

  • HTML5 video player with the IMA HTML5 SDK integrated. If you don't have one, check out our Get Started guide.

Modify your HTML

First you'll need to change your HTML to add a div wrapping both the ad player and content video player. In the Get Started guide, this is already done for you with mainContainer:

...
<div id="mainContainer">
  <div id="content">
    <video id="contentElement">
      <source src="http://rmcdn.2mdn.net/Demo/vast_inspector/android.webm"></source>
      <source src="http://rmcdn.2mdn.net/Demo/vast_inspector/android.mp4"></source>
    </video>
  </div>
  <div id="adContainer"></div>
</div>
...

Add event listeners to your JavaScript

Now you'll add event listeners to your JavaScript code, such as to detect a user clicking on the video. Here, we're using ads.js as an example:

...
function init() {
  videoContent = document.getElementById('contentElement');
  mainContainer = document.getElementById('mainContainer');
  mainContainer.addEventListener('click', function() { alert('I was clicked!');});
  ...
}
...

That's it! You can click on the video and see the pop-up appear regardless of whether an ad is playing and still have ad click-throughs work.

Try it out

You can see a working implementation below.

Send feedback about...

IMA SDK for HTML5
Need help? Visit our support page.