ติดตามการคลิกเมาส์
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เมื่อใช้ IMA SDK จะมีวิดีโอเพลเยอร์ 2 รายการแยกกัน ได้แก่ วิดีโอเพลเยอร์สำหรับเนื้อหาและวิดีโอเพลเยอร์สำหรับโฆษณา SDK จะจัดการการวางซ้อนโปรแกรมเล่นโฆษณาบนโปรแกรมเล่นวิดีโอเนื้อหาโดยอัตโนมัติ ด้วยเหตุนี้ การเพิ่มโปรแกรมรับฟังเหตุการณ์ในวิดีโอเพลเยอร์โดยตรงจึงใช้ไม่ได้ วิธีแก้ปัญหาอย่างหนึ่งคือการแสดงและซ่อนคอนเทนเนอร์โฆษณา แต่วิธีนี้อาจรบกวนฟังก์ชันการทำงานของ SDK วิธีแก้ปัญหาที่ดีกว่าคือการรวมทั้ง 2 โปรแกรมเล่นไว้ใน div
หลักและแนบ Listener เหตุการณ์ไว้กับ div
นั้น
คู่มือนี้จะแสดงวิธี
ข้อกำหนดเบื้องต้น
- โปรแกรมเล่นวิดีโอ HTML5 ที่ผสานรวม IMA HTML5 SDK หากยังไม่มี ให้ดูตั้งค่า 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>
...
เพิ่ม Listener เหตุการณ์ลงใน JavaScript
ถัดไป ให้เพิ่ม Listener เหตุการณ์ลงในโค้ด 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."]]