マウスクリックをトラッキングする
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
IMA SDK を使用する場合、コンテンツ動画プレーヤーと広告プレーヤーの 2 つの動画プレーヤーが存在します。SDK は、コンテンツ動画プレーヤーの上に広告プレーヤーをオーバーレイすることを自動的に管理します。そのため、動画プレーヤーにイベント リスナーを直接追加することはできません。回避策として、広告コンテナを表示したり非表示にしたりすることもできますが、この方法は SDK の機能に干渉する可能性があります。より良い解決策は、両方のプレーヤーを親 div
にラップし、それにイベント リスナーをアタッチすることです。このガイドでは、その方法について説明します。
前提条件
- IMA HTML5 SDK が統合された HTML5 動画プレーヤー。まだ設定していない場合は、IMA SDK を設定するをご覧ください。
HTML を変更する
まず、HTML を変更して、広告プレーヤーとコンテンツ動画プレーヤーの両方をラップする div
を追加する必要があります。[IMA SDK を設定する] では、mainContainer
によってすでに設定されています。
...
<div id="mainContainer">
<div id="content">
<video id="contentElement">
<source src="https://storage.googleapis.com/gvabox/media/samples/stock.mp4">
</video>
</div>
<div id="adContainer"></div>
</div>
...
JavaScript にイベント リスナーを追加する
次に、ユーザーが動画をクリックしたことを検出するなどの、イベント リスナーを JavaScript コードに追加します。たとえば、ads.js
では次のようになります。
...
function init() {
videoContent = document.getElementById('contentElement');
mainContainer = document.getElementById('mainContainer');
mainContainer.addEventListener('click', function() { alert('I was clicked!');});
...
}
...
これで、動画をクリックすると、広告の再生中かどうかにかかわらずポップアップが表示され、広告のクリックスルーが機能します。
試してみる
動作する実装は次のとおりです。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-31 UTC。
[null,null,["最終更新日 2025-08-31 UTC。"],[[["\u003cp\u003eThe IMA SDK uses two separate video players: one for content and one for ads, which it automatically overlays.\u003c/p\u003e\n"],["\u003cp\u003eDirectly adding event listeners to the content video player is ineffective due to the ad overlay.\u003c/p\u003e\n"],["\u003cp\u003eWrap both video players in a parent \u003ccode\u003ediv\u003c/code\u003e and attach event listeners to that parent element for proper functionality.\u003c/p\u003e\n"],["\u003cp\u003eThis method enables event handling (like click detection) on the video, irrespective of ad playback, while preserving ad click-throughs.\u003c/p\u003e\n"]]],[],null,["# Track mouse clicks\n\nWhen using the IMA SDK, there are two separate video players, your content\nvideo player and the ad player. The SDK automatically manages overlaying the ad\nplayer over the content video player. As a result, adding event listeners to the\nvideo player directly does not work. One workaround is to show and hide the ad\ncontainer, but this can interfere with SDK functionality. A better solution is to\nwrap both players in a parent `div` and attach event listeners to that.\nThis guide shows you how.\n\nPrerequisites\n-------------\n\n- HTML5 video player with the IMA HTML5 SDK integrated. If you don't have one, check out [Set up the IMA SDK](/interactive-media-ads/docs/sdks/html5/client-side).\n\nModify your HTML\n----------------\n\nFirst you need to change your HTML to add a `div` wrapping both\nthe ad player and content video player. In \\*Set up the IMA SDK\\*, this is already\ndone for you with `mainContainer`: \n\n```text\n...\n\u003cdiv id=\"mainContainer\"\u003e\n \u003cdiv id=\"content\"\u003e\n \u003cvideo id=\"contentElement\"\u003e\n \u003csource src=\"https://storage.googleapis.com/gvabox/media/samples/stock.mp4\"\u003e\n \u003c/video\u003e\n \u003c/div\u003e\n\n \u003cdiv id=\"adContainer\"\u003e\u003c/div\u003e\n\u003c/div\u003e\n...\n```\n\nAdd event listeners to your JavaScript\n--------------------------------------\n\nNext, add event listeners to your JavaScript code, such as to detect a\nuser clicking on the video. For example, in `ads.js`: \n\n```text\n...\nfunction init() {\n videoContent = document.getElementById('contentElement');\n mainContainer = document.getElementById('mainContainer');\n mainContainer.addEventListener('click', function() { alert('I was clicked!');});\n ...\n}\n...\n```\n\n\u003cbr /\u003e\n\nThat's it! You can click on the video and see the pop-up appear regardless of\nwhether an ad is playing and still have ad click-throughs work.\n\nTry it out\n----------\n\nYou can see a working implementation below.\nSee the Pen \\\u003ca href='http://codepen.io/imasdk/pen/gPYzYO/'\\\u003egPYzYO\\\u003c/a\\\u003e by IMA SDK (\\\u003ca href='http://codepen.io/imasdk'\\\u003e@imasdk\\\u003c/a\\\u003e) on \\\u003ca href='http://codepen.io'\\\u003eCodePen\\\u003c/a\\\u003e."]]