Tracking video play only once time without Media Tracking Plugin

I don’t use the Matomo Media Tracking Plugin to track video play. Yet i have consider, that the JavaScript onplaying="" has the same effect as the onplay="". Both are similar to a simple onclick="", but only by play and not by pause.

The playing event is fired after playback is first started, and whenever it is restarted. For example it is fired when playback resumes after having been paused or delayed due to lack of data.

The play event is fired when the paused property is changed from true to false, as a result of the play method, or the autoplay attribute.

A search results in the information, that a onstart="" don’t exist. That’s a problem.

The onplaying="" was connected with a _paq.push(['trackEvent', category, action, name]);. So, by every play click a push was fired.

HTML

<video controls="" onplaying="matomovideostart(this,'video-name-001');" width="512">
  <source src="/video/video-name-001.mp4" type="video/mp4">
</video>

When there is only one video in a webpage:

var matomovideostartcount = 0;
function matomovideostart(event, video_filename_name) {
	if (matomovideostartcount == 0) {
		_paq.push(['trackEvent', 'video', 'start', video_filename_name]);
		matomovideostartcount++;
	}
}

When more videos are in a webpage:

var matomovideostartarray = [];
function matomobuttonvideostart(event, video_filename_name) {
	if (matomovideostartarray.includes(video_filename_name) === false) {
		_paq.push(['trackEvent', 'video', 'start', video_filename_name]);
		matomovideostartarray.push(video_filename_name);
	}
}

With this, by every video only one (the first) playingclick will be tracked.

For a more flexible function, you can use a ontimeupdate="" instead a onplaying="". That’s a little bit complicated, but is more functionable.
The ontimeupdate is an interval and get the current play time.
To expand the function, you can use the time value from the ontimeupdate event with event.currentTime, to calculate with it, if the Visitor set the play-cursor backward.

Or use the onended event to reset the counter or array.

1 Like