The VASTTracker class provides methods to track the execution of an Ad.
The constructor signature is:
constructor(client, ad, creative, variation = null)client: VASTClient- An optional instance of VASTClient that can be updated by the tracker.ad: Ad- The ad to trackcreative: Creative- The creative to trackvariation: CompanionAd|NonLinearAd- An optional variation of the creative, for Companion and NonLinear Ads
To get an instance of VASTTracker, simply import it and create one using the constructor:
import {
VASTClient,
VASTTracker
} from 'vast-client'
// With a client instance
const vastClient = new VASTClient();
const vastTracker = new VASTTracker(vastClient, ad, creative);
// For a companion ad
const vastTracker = new VASTTracker(vastClient, ad, creative, companion);
// Without a client instance
const vastTracker = new VASTTracker(null, ad, creative);VASTTracker extends EventEmitter, therefore is possible to add event listeners like this:
vastTracker.on('exitFullscreen', () => {
// Deal with the event
});Here is the list of events emitted by the class:
complete- Only for linear ads with a duration. Emitted aftercomplete()has been called.clickthrough- Emitted when callingclick()if there is at least one element. A URL will be passed as a dataclose- Only for non-linear ads, emitted whenclose()is calledcloseLinear- Only for linear ads, emitted whenclose()is calledcreativeView- Emitted afterload()method has been called.exitFullscreen- Emitted when callingsetFullscreen(fullscreen)and changing the fullscreen state from true to falsefirstQuartile- Only for linear ads with a duration. Emitted when the adunit has reached 25% of its durationfullscreen- Emitted when callingsetFullscreen(fullscreen)and changing the fullscreen state from false to truemidpoint- Only for linear ads with a duration. Emitted when the adunit has reached 50% of its durationmute- Emitted when callingsetMuted(muted)and changing the mute state from false to truepause- Emitted when callingsetPaused(paused)and changing the pause state from false to trueprogress-[0-100]%- Only for linear ads with a duration. Emitted on everysetProgress(duration)calls, where [0-100] is the adunit percentage completionprogress-[currentTime]- Only for linear ads with a duration. Emitted on everysetProgress(duration)calls, where [currentTime] is the adunit current timeresume- Emitted when callingsetPaused(paused)and changing the pause state from true to falserewind- Emitted whensetProgress(duration)is called with a smaller duration than the previous oneskip- Emitted after callingskip()skip-countdown- Only for linear ads with a duration. Emitted on everysetProgress(duration)calls, the updated countdown will be passed as a datastart- Only for linear ads with a duration. Emitted on the 1st non-nullsetProgress(duration)callthirdQuartile- Only for linear ads with a duration. Emitted when the adunit has reached 75% of its durationunmute- Emitted when callingsetMuted(muted)and changing the mute state from true to false
Sends a request to the URI provided by the VAST <Error> element. If an [ERRORCODE] macro is included, it will be substituted with errorCode.
errorCode: String- Replaces[ERRORCODE]macro.[ERRORCODE]values are listed in the VAST specification
const MEDIAFILE_PLAYBACK_ERROR = '405';
// Bind error listener to the player
player.on('error', () => {
vastTracker.errorWithCode(MEDIAFILE_PLAYBACK_ERROR);
});Must be called when the user watched the linear creative until its end. Calls the complete tracking URLs.
complete
// Bind ended listener to the player
player.on('ended', () => {
vastTracker.complete();
});
vastTracker.on('complete', () => {
// complete tracking URLs have been called
});Must be called when the user clicks on the creative. Calls the tracking URLs.
clickthrough
// Bind click listener to the player
player.on('click', () => {
vastTracker.click();
});
vastTracker.on('clickthrough', url => {
// Open the resolved clickThrough url
document.location.href = url;
});Must be called when the player or the window is closed during the ad. Calls the closeLinear (in VAST 3.0) and close tracking URLs
closeLinearclose
// When user exits the page
window.onbeforeunload = () => {
vastTracker.close();
};
// event for VAST 3.0 linear ads
vastTracker.on('closeLinear', () => {
// ...
});
// event for VAST 2.0 linear ads or companion ads
vastTracker.on('close', () => {
// ...
});Must be called when the skip button is clicked. Calls the skip tracking URLs.
skip
// Bind click listener to the skip button
skipButton.on('click', () => {
vastTracker.skip();
});
vastTracker.on('skip', () => {
// skip tracking URLs have been called
});Sets the duration of the ad and updates the quartiles based on that.
duration: Number- The duration of the ad
Updates the expand state and calls the expand/collapse tracking URLs.
expanded: Boolean- Indicates if the video is expanded or not
expandcollapse
// Sample function for a button that increase/decrease player size
let playerExpanded = false;
expandButton.addEventListener('click', e => {
playerExpanded = !playerExpanded
if (playerExpanded) {
increasePlayerSize()
} else {
decreasePlayerSize()
}
vastTracker.setExpand(playerExpanded);
});
vastTracker.on('expand', () => {
// expand tracking URLs have been called
});
vastTracker.on('collapse', () => {
// collapse tracking URLs have been called
});Updates the fullscreen state and calls the fullscreen tracking URLs.
fullscreen: Boolean- Indicates if the video is in fulscreen mode or not
fullscreenexitFullscreen
// Bind fullscreenchange listener to the player
// Note that the fullscreen API is still vendor-prefixed in browsers
player.addEventListener('fullscreenchange', e => {
const isFullscreen = !!document.fullscreenElement;
vastTracker.setFullscreen(isFullscreen);
});
vastTracker.on('fullscreen', () => {
// fullscreen tracking URLs have been called
});
vastTracker.on('exitFullscreen', () => {
// exitFullscreen tracking URLs have been called
});Updates the mute state and calls the mute/unmute tracking URLs.
muted: Boolean- Indicates if the video is muted or not
muteunmute
// Bind a volumechange listener to the player
player.addEventListener('volumechange', e => {
vastTracker.setMuted(e.target.muted);
});
vastTracker.on('mute', () => {
// mute tracking URLs have been called
});
vastTracker.on('unmute', () => {
// unmute tracking URLs have been called
});Update the pause state and call the resume/pause tracking URLs.
paused: Boolean- Indicates if the video is paused or not
pauseresume
// Bind play/pause listeners to the player
player.addEventListener('play', () => vastTracker.setPaused(false) );
player.addEventListener('pause', () => vastTracker.setPaused(true) );
vastTracker.on('resume', () => {
// resume tracking URLs have been called
});
vastTracker.on('pause', () => {
// pause tracking URLs have been called
});Sets the duration of the ad and updates the quartiles based on that. This is required for tracking time related events such as start, firstQuartile, midpoint, thirdQuartile or rewind.
progress: Number- Current playback time in seconds
startskip-countdownprogress-[0-100]%progress-[currentTime]rewindmidpointfirstQuartilethirdQuartile
// Bind a timeupdate listener to the player
player.addEventListener('timeupdate', e => {
vastTracker.setProgress(e.target.currentTime);
});
vastTracker.on('firstQuartile', () => {
// firstQuartile tracking URLs have been called
});Must be called if you want to overwrite the <Linear> Skipoffset value. This will init the skip countdown duration. Then, every time setProgress() is called, it will decrease the countdown and emit a skip-countdown event with the remaining time.
Do not call this method if you want to keep the original Skipoffset value.
duration: Number- The time in seconds until the skip button is displayed
// Overwrite linear Skipoffset value – 5s countdown
vastTracker.setSkipDelay(5);Reports the impression URI. Can only be called once. Will report the following URI:
- All
<Impression>URI from the<InLine>and<Wrapper>tracking elements. - The
creativeViewURI from the<Tracking>events
creativeView
// Bind canplay listener to the player
player.on('canplay', () => {
vastTracker.trackImpression();
});
vastTracker.on('creativeView', () => {
// impression tracking URLs have been called
});