I am looking to implement a Google Analytics Event for all audio tags on my website.
Currently, I have a script that targets audio tags with a specific class:
<script>
/* a test script */
$(function() {
// Execute function when any element with the audioClass class is clicked
$( "audioClass" ).on("click", (function() {
ga('send', 'event', 'audioclass', 'clicked', 'test');
});
});
</script>
However, I want to see if it's possible to achieve this without relying on a specific class for the audio tag.
<script>
/* 2nd test script */
$(function() {
// Hopefully, every audio tag can trigger the function.
$("audio").on("click", function() {
ga("send", "event", "audio", "clicked", "test");
});
});
</script>
I came across a solution mentioned here: Adding play buttons for all audio tags on a page , but I'm unsure how to incorporate the following code snippet into my existing code:
$( "audio" ).each(function( index ) {
If anyone has insights on how to integrate this into my code, I would greatly appreciate it.