-
-
Save cashlion/c4b73f49d0c48d3297490d2020034abd to your computer and use it in GitHub Desktop.
GTM - HTML Video Listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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