Skip to content

Instantly share code, notes, and snippets.

@cashlion
Created March 31, 2020 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cashlion/c4b73f49d0c48d3297490d2020034abd to your computer and use it in GitHub Desktop.
Save cashlion/c4b73f49d0c48d3297490d2020034abd to your computer and use it in GitHub Desktop.
GTM - HTML Video Listener
/*
* v1.0
* Created by the Data Intelligence Team at https://www.foundsm.com/
* Written by Nicholas Hoium
* Licensed under the Creative Commons 4.0 Attribution Public License
*/
(function () {
function pushVideoEventToDataLayer(video, gtmVideoStatus) {
dataLayer.push({
"event": 'gtm.video',
"gtm.videoStatus": gtmVideoStatus,
"gtm.videoUrl": video.src,
"gtm.videoTitle": video.dataset.title, // data-title attribute.
"gtm.videoProvider": 'Self-Hosted',
});
}
var videos = document.querySelectorAll('video');
videos.forEach(function (video) {
video.addEventListener('play', function (e) {
pushVideoEventToDataLayer(e.target, 'start');
});
video.addEventListener('pause', function (e) {
pushVideoEventToDataLayer(e.target, 'pause');
});
video.addEventListener('ended', function (e) {
pushVideoEventToDataLayer(e.target, 'complete');
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment