Last active
October 27, 2020 09:25
-
-
Save vishalarora91/ad783582480ee5fdbf99067cd47fb4c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <script> | |
| (function(){ | |
| var divisor = 25; | |
| var videos_status = []; | |
| function eventHandler(e) { | |
| switch (e.type) { | |
| case 'timeupdate': | |
| videos_status[e.target.id].current = Math.round(e.target.currentTime); | |
| var pct = Math.floor(100 * videos_status[e.target.id].current / e.target.duration); | |
| for (var j in videos_status[e.target.id]._progress_markers) { | |
| if (pct >= j && j > videos_status[e.target.id].greatest_marker) { | |
| videos_status[e.target.id].greatest_marker = j; | |
| } | |
| } | |
| if (videos_status[e.target.id].greatest_marker && !videos_status[e.target.id]._progress_markers[videos_status[e.target.id].greatest_marker]) { | |
| videos_status[e.target.id]._progress_markers[videos_status[e.target.id].greatest_marker] = true; | |
| ga('send', 'event', 'VideoTrackCategory', 'Progress ' + videos_status[e.target.id].greatest_marker + '%', decodeURIComponent(e.target.currentSrc.split('/')[e.target.currentSrc.split('/').length - 1])); | |
| } | |
| break; | |
| case 'play': | |
| ga('send', 'event', 'VideoTrackCategory', 'Play', decodeURIComponent(e.target.currentSrc.split('/')[e.target.currentSrc.split('/').length - 1])); | |
| break; | |
| case 'pause': | |
| ga('send', 'event', 'VideoTrackCategory', 'Pause', decodeURIComponent(e.target.currentSrc.split('/')[e.target.currentSrc.split('/').length - 1]), videos_status[e.target.id].current); | |
| break; | |
| case 'ended': | |
| ga('send', 'event', 'VideoTrackCategory', 'Finished', decodeURIComponent(e.target.currentSrc.split('/')[e.target.currentSrc.split('/').length - 1])); | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| var videos = document.getElementsByTagName('video'); | |
| for (var i = 0; i < videos.length; i++) { | |
| var videoTagId; | |
| if (!videos[i].getAttribute('id')) { | |
| videoTagId = 'html5_video_' + Math.random().toString(36).slice(2); | |
| videos[i].setAttribute('id', videoTagId); | |
| }else { | |
| videoTagId = videos[i].getAttribute('id'); | |
| } | |
| videos_status[videoTagId] = {}; | |
| videos_status[videoTagId].greatest_marker = 0; | |
| videos_status[videoTagId]._progress_markers = {}; | |
| for (j = 0; j < 100; j++) { | |
| videos_status[videoTagId].progress_point = divisor * Math.floor(j / divisor); | |
| videos_status[videoTagId]._progress_markers[videos_status[videoTagId].progress_point] = false; | |
| } | |
| videos_status[videoTagId].current = 0; | |
| videos[i].addEventListener("play", eventHandler, false); | |
| videos[i].addEventListener("pause", eventHandler, false); | |
| videos[i].addEventListener("ended", eventHandler, false); | |
| videos[i].addEventListener("timeupdate", eventHandler, false); | |
| } | |
| })(); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment