我需要为所有音频标签添加一个Google Analytics事件。
如果所有音频标签都属于特定类,我可以做到:
<script>
/* a test script */
$(function() {
// Every button with the audioClass class can execute the function.
$( "audioClass" ).on("click", (function() {
ga('send', 'event', 'audioclass', 'clicked', 'test');
});
});
</script>
但是可以只使用音频标签(即没有类名)来完成吗?
<script>
/* 2nd test script */
$(function() {
// Every audio tag will hopefully execute the function.
$("audio").on("click", function() {
ga("send", "event", "audio", "clicked", "test");
});
});
</script>
我在这里找到了类似的答案:为页面上的所有音频标签添加播放按钮但是我看不出如何使用它:
$( "audio" ).each(function( index ) {
在我的代码中。
您正在寻找播放事件:
$("audio").on("play", function() {
ga("send", "event", "audio", "clicked", "test");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<audio controls>
<source src="http://www.noiseaddicts.com/samples_1w72b820/280.mp3" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>