I’m currently using WordPress 4.0 and all my audios on posts/pages are embedded using the default WordPress audio player – through a shortcode like:
Now, I want to track how many times the play button is pressed. So, my question is: how can I capture this player events?
PS: I tried the following (and it didn’t work):
$('.mejs-playpause-button button').click(function(){
console.log('TESTING');
});
It’s possible that the event propogation for that button is being prevented by the MediaElementJS library that manages the audio element.
As an alternative I would suggest listening for ‘play’ events on either the MediaElementJS element or directly selecting the native HTML audio element and listening for the ‘play’ event on this object.
I briefly attempted to get the MediaElementJS instance for a player but it is hard to get hold off because of the way that WordPress makes these instances.
However you can add an event listener to an audio element and record the amount of times this element is fired:
This might not be the best solution if you have multiple elements on a page though.