我需要将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>
我在这里找到了类似的答案:Adding play buttons for all audio tags on a page
但是我看不到如何使用它:
$( "audio" ).each(function( index ) {
在我的代码中。
最佳答案
您可能正在寻找onplay事件:
$("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>
关于javascript - 如何使用Javascript向所有<audio>标签添加Google Analytics(分析)事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41496005/