GoDAM

Run code when the GoDAM player is ready

GoDAM for WordPress v1.4.9 Updated: 2026-11-19

GoDAM builds each video player in the browser after the page loads. If your script reaches for a player before GoDAM has finished, the player isn’t there yet and your code fails. This page shows how to wait for the right moment with the godamPlayerReady event, and how to control the player with GoDAMAPI.getPlayer() once it’s ready.

Use GoDAM 1.4.9 or later. The examples on this page were tested on GoDAM 2.10.0. For the full list of player methods, see the GoDAM JavaScript API.

Run code when a player is ready

GoDAM fires godamPlayerReady on document once for each player on the page, as soon as that player has started. By then GoDAM has set up the player’s controls, chapters, captions and layers, so you can read it and control it straight away.

document.addEventListener( 'godamPlayerReady', ( event ) => {
	const { attachmentId, videoElement } = event.detail;

	// Get the GoDAM player for this exact video.
	const player = window.GoDAMAPI.getPlayer( attachmentId, videoElement );

	console.log( 'Player ' + attachmentId + ' is ready. Paused: ' + player.isPaused() );
} );

The event carries three values in event.detail:

ValueWhat it is
attachmentIdThe video’s attachment ID as a string, for example '1641'. It comes from the player’s data-id attribute.
videoElementThe <video> element GoDAM built this player from. Pass it to getPlayer() to get this exact player when the same video is on the page more than once.
playerThe underlying Video.js player. Use getPlayer() instead if you want the GoDAM methods listed below.

Three things to know:

  • Listen on document. The event is not sent to window and does not bubble from the video.
  • The event fires once per player and is not repeated. If the same video appears twice on a page, you get two events, one for each copy.
  • Add your listener before the players start, for example in a script that loads in the page head. If your script may load later, use the pattern in the next section.

If your script might load late

A listener added after a player has started never hears its godamPlayerReady event. To cover both cases, ask for the player first and only wait if it isn’t ready:

function whenGodamPlayerReady( attachmentId, callback ) {
	let player = null;

	try {
		// Throws if GoDAM hasn't loaded yet or the player is still starting.
		player = window.GoDAMAPI.getPlayer( attachmentId );
	} catch ( error ) {
		player = null;
	}

	if ( player ) {
		callback( player );
		return;
	}

	document.addEventListener( 'godamPlayerReady', function onReady( event ) {
		if ( event.detail.attachmentId !== String( attachmentId ) ) {
			return;
		}
		document.removeEventListener( 'godamPlayerReady', onReady );
		callback( window.GoDAMAPI.getPlayer( attachmentId, event.detail.videoElement ) );
	} );
}

whenGodamPlayerReady( 1641, ( player ) => {
	player.play();
} );

Don’t call getPlayer() at the top of a script and assume the player is there. GoDAM builds its players after the page’s HTML has loaded, so a call made before that throws an error.

What getPlayer() gives you

window.GoDAMAPI.getPlayer( attachmentId, videoElement ) returns a GoDAM Player object. It wraps the Video.js player and adds GoDAM’s own methods.

ArgumentWhat to pass
attachmentIdThe video’s attachment ID, as a string or a number.
videoElement (optional)The <video> element, or the player’s outer element. Pass it when the same video is on the page more than once, to get that copy. Without it you get the first copy on the page.

Methods you’ll use most on the returned player:

MethodWhat it does
play()Starts the video.
pause()Pauses the video.
seek( seconds )Jumps to a time in seconds, kept between 0 and the video’s length.
currentTime()The current time in seconds.
duration()The video’s length in seconds. It is 0 until the browser has loaded the video’s details, which may be after the player is ready.
isPaused()true when the video is paused.
on( eventName, callback )Listens for play, pause, ended, timeupdate, seeking, seeked, volumechange and fullscreenchange.
videoJsThe underlying Video.js player, if you need something GoDAM doesn’t wrap.

Each call to getPlayer() makes a new Player object that adds its own listeners to the video. Call it once per player and keep the result, rather than calling it every time you need the player.

When getPlayer() fails

getPlayer() never returns null or undefined. If it can’t give you a player, it throws an error, so a try...catch around it is enough to check. Which error you get depends on what you pass and how far GoDAM has got:

Error messageWhen you see it
No video found for attachment ID 1641There is no GoDAM player with that ID on the page. You also get this when you pass only the ID and GoDAM hasn’t built that player yet.
Player for attachment ID 1641 is currently initializingYou passed the <video> element while GoDAM was building that player.
Player requires both videoJs and video elementsYou passed the <video> element before GoDAM started building that player.
Cannot read properties of undefined (reading ‘getPlayer’)Your code ran before GoDAM’s script loaded, so window.GoDAMAPI doesn’t exist yet.

In every case the fix is the same: wait for godamPlayerReady, or use the pattern above.

Examples

Track how far a visitor watched

document.addEventListener( 'godamPlayerReady', ( event ) => {
	const { attachmentId, videoElement } = event.detail;
	if ( attachmentId !== '1641' ) {
		return;
	}

	const player = window.GoDAMAPI.getPlayer( attachmentId, videoElement );
	let furthest = 0;

	player.on( 'timeupdate', ( data ) => {
		furthest = Math.max( furthest, data.currentTime );
	} );

	player.on( 'pause', () => {
		console.log( 'Paused at ' + player.currentTime() + 's, furthest point ' + furthest + 's' );
	} );
} );

Play and pause from your own button

document.addEventListener( 'godamPlayerReady', ( event ) => {
	const { attachmentId, videoElement } = event.detail;
	if ( attachmentId !== '1641' ) {
		return;
	}

	const player = window.GoDAMAPI.getPlayer( attachmentId, videoElement );

	document.querySelector( '#my-video-button' ).addEventListener( 'click', () => {
		if ( player.isPaused() ) {
			player.play();
		} else {
			player.pause();
		}
	} );
} );

Several players on one page

Every player fires its own event, so one listener can handle them all. This one keeps a single video playing at a time:

const godamPlayers = new Map();

document.addEventListener( 'godamPlayerReady', ( event ) => {
	const { attachmentId, videoElement } = event.detail;
	const player = window.GoDAMAPI.getPlayer( attachmentId, videoElement );

	// Keyed by element, so two copies of the same video stay separate.
	godamPlayers.set( videoElement, player );

	player.on( 'play', () => {
		godamPlayers.forEach( ( other ) => {
			if ( other !== player && ! other.isPaused() ) {
				other.pause();
			}
		} );
	} );
} );

On GoDAM 2.7 and later, GoDAMAPI.pauseAll() can do the same in one call. See Pausing videos from your code.

godamAllPlayersReady

GoDAM also fires godamAllPlayersReady on document once it has created every player that was on the page when it started. Its event.detail.players lists them, and getPlayer() works for each of them from then on.

It can fire before the godamPlayerReady events, while GoDAM is still setting up each player’s controls and layers. A player added to the page later isn’t included. For anything that touches a player, use godamPlayerReady.