Untitled

 avatar
unknown
javascript
2 years ago
17 kB
11
Indexable
$(document).ready(function() {
    var videoInterval = 10;
    var videoStatus = {};

    function gtmDataLayerPush(video) {
        dataLayer.push({
            event: video.gtmData.e,
            //category: video.gtmData.c,
            //action: video.gtmData.a,
            video_name: video.gtmData.l,
            video_elapsed_time: video.gtmData.v,
            video_link: video.gtmData.i,
            videoSource: video.gtmData.s
        });
    }

    function getTitles(videoSrc, videoLoc) {
        if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/635983894/rendition/1080p/file.mp4?loc=external&signature=9652911bd6ce7ad4ac7f48720f5ef59d9d94b4969e49b735b246314e822d659b"
            && videoLoc == "/myelofibrosis-mf")
            return "performs spleen assessment at baseline  and consistently throughout management as part of his patient care";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/635983894/rendition/1080p/file.mp4?loc=external&signature=9652911bd6ce7ad4ac7f48720f5ef59d9d94b4969e49b735b246314e822d659b"
                && videoLoc == "/myelofibrosis-mf/spleen-assessment")
            return "on how spleen assessment is a marker of disease progression in patients with myelofibrosis and see his technique for assessment";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/635983894/rendition/1080p/file.mp4?loc=external&signature=9652911bd6ce7ad4ac7f48720f5ef59d9d94b4969e49b735b246314e822d659b"
                && videoLoc == "/myelofibrosis-mf/resources")
            return "proactive spleen assessment in patients with myelofibrosis";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/659335151/rendition/1080p/file.mp4?loc=external&signature=a0840a4eb37fc80dac0fcc56c54e1fd3ac25db00c9ffa5843bd9bcb61dc29433"
                && videoLoc == "/polycythemia-vera-pv")
            return "characteristics that indicate advanced polycythemia vera (pv)";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/659335151/rendition/1080p/file.mp4?loc=external&signature=a0840a4eb37fc80dac0fcc56c54e1fd3ac25db00c9ffa5843bd9bcb61dc29433"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "identifying patients with advanced pv";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/662520193/rendition/1080p/file.mp4?loc=external&signature=8efe6d9fb1ae9ae020111711fe90bead6901183971091b855c8c3888ad23f26b"
                && videoLoc == "/polycythemia-vera-pv/symptoms")
            return "30 nights spent sweating since dave's last pv appointment";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/680290801/rendition/1080p?loc=external&signature=fdbb568d69cbe71bb0bff55e9605f95a3ba44c765c58078030b6bd51722a1b9f"
                && videoLoc == "/polycythemia-vera-pv/symptoms")
            return "36 days of itching since julie's last pv appointment";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/664120868/rendition/1080p/file.mp4?loc=external&signature=27a0637a03047549183fed5ff59f4ca9aef51480fd4a52865eb6fc7cdb438773"
                && videoLoc == "/polycythemia-vera-pv/symptoms")
            return "40 days lost to fatigue since nick's last pv appointment";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/659338062/rendition/1080p/file.mp4?loc=external&signature=91235a6f79485f5ffa77137b624bb5361af319aca258ad989bd57793e5c23e73"
                && videoLoc == "/polycythemia-vera-pv/symptoms")
            return "about the importance of looking beyond blood counts and actively monitoring pv symptoms";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/659339141/rendition/1080p/file.mp4?loc=external&signature=0a20f2e5beb8d2460a9ae1ddd85ada13361eddd5a2fe6d6648b34b03fe83cc72"
                && videoLoc == "/polycythemia-vera-pv/symptoms")
            return "investigating the causes of increased symptom burden in patient with pv, including looking at blood counts and current treatment";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/664120352/rendition/1080p/file.mp4?loc=external&signature=459ede07f2117f519d3838a134dc7067851d744bdd41016fed6244f69df6dae1"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "managing white blood cell counts in patients with pv";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/771450901/rendition/1080p/file.mp4?loc=external&signature=f3e403d7c64339174600500eecc18a0020818ac1006acaecf57229a33159869f"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "symptoms of pv: identification and classification";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/656960026/rendition/1080p/file.mp4?loc=external&signature=8cbfad2c756d09e88d3db6274f58bb70d4cdc9695ba462d02d24ca919060c6aa"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "why we need to assess symptoms in our patients";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/664120997/rendition/1080p/file.mp4?loc=external&signature=65691e71db44ca80bcdefcf84bdec526c72c5639d88fe03569971c276a6fadcf"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "hematocrit control <45% and risk of thrombosis in patients with pv";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/664120713/rendition/1080p/file.mp4?loc=external&signature=3a9787b74451d7f9da595598ddc2e2bed65f78b47ee2becefb1f0c50bfdac0e6"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "what is our role in improving patients care in pv";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/664120595/rendition/1080p/file.mp4?loc=external&signature=3543bd5c921cb35f0b3568fba114ab7b0c5297f62706991cf09118bec331fd19"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "assessing and managing disease related symptoms in patients with pv";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/656958128/rendition/1080p/file.mp4?loc=external&signature=ef16dba9cf3a7e86dbf2a1c01aef0ee4b62933df1ae70a6192263e4775a00413"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "why blood counts may be a sign that it is time for a change in clinical management";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/662520193/rendition/1080p/file.mp4?loc=external&signature=8efe6d9fb1ae9ae020111711fe90bead6901183971091b855c8c3888ad23f26b"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "30 days spent sweating since dave's last pv appointment";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/664120868/rendition/1080p/file.mp4?loc=external&signature=27a0637a03047549183fed5ff59f4ca9aef51480fd4a52865eb6fc7cdb438773"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "40 days lost to fatigue since nick's last pv appointment";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/659336590/rendition/1080p/file.mp4?loc=external&signature=ccb44e7698620f2695d3573df852bf70ee93c14a5e2fe8f68209d4b66650a20a"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "elevated hematocrit and thrombotic risk";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/639036140/rendition/1080p/file.mp4?loc=external&signature=55bfee84e2bdd0305cf6acf19c486009772fe929c00ff58c6db7363296e7fd72"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "what contributes to thrombotic risk in pv";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/771450996/rendition/1080p/file.mp4?loc=external&signature=787bbab484a3fc1f3f5f2117a9395cf70f5a131a9fa1c9f5637d93cb76f3a704"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "progressive symptoms in pv: monitoring for signs of advanced disease";

        else if (videoSrc == "https://player.vimeo.com/progressive_redirect/playback/680290801/rendition/1080p?loc=external&signature=fdbb568d69cbe71bb0bff55e9605f95a3ba44c765c58078030b6bd51722a1b9f"
                && videoLoc == "/polycythemia-vera-pv/resources")
            return "36 days of itching since julies last pv";

        else
            return "Failed to get video title";
        }


    function analyticsTimeUpdate(videoId, event) {
        var currentVideo = document.getElementById(videoId);
        var videoData = videoStatus[videoId];

        var titleTemp = jQuery(event).attr("poster");
        var videoSrc = jQuery(event).find('source').attr("src");
        var videoLoc = window.location.pathname;
        var mainTitle = getTitles(videoSrc, videoLoc);

        videoData.current = Math.round(currentVideo.currentTime);
        var pct = Math.floor(100 * videoData.current / currentVideo.duration);
        
        for (var marker in videoData._progressMarkers) {
            if (pct >= marker && marker > videoData.greatestWatchedPosition) {
                videoData.greatestWatchedPosition = marker;
            }
        }
        if (videoData.current >= 60 && !videoData.watched60 ) {
            videoData.gtmData.eventName = "video_progression_60s";
            videoData.gtmData.categoryVal = "Video";
            videoData.gtmData.actionVal = "video_progression_60s";
            videoData.gtmData.videoName = mainTitle;
            videoData.gtmData.videoLink1 = videoSrc;
            videoData.gtmData.videoSource = "vimeo";
            videoData.gtmData.videoElapsedTime1 = "60";
            gtmDataLayerPush(videoData);
            videoData.watched60 = true
        }
        if (videoData.current >= 30 && !videoData.watched30 ) {
            videoData.gtmData.eventName = "video_progression_30s";
            videoData.gtmData.categoryVal = "Video";
            videoData.gtmData.actionVal = "video_progression_30s";
            videoData.gtmData.videoName = mainTitle;
            videoData.gtmData.videoLink1 = videoSrc;
            videoData.gtmData.videoSource = "vimeo";
            videoData.gtmData.videoElapsedTime1 = "30";
            gtmDataLayerPush(videoData);
            videoData.watched30 = true
        }

        if (pct >= 25 && !videoData._progressMarkers[25]) {
            videoData.gtmData.eventName = "video_progression_25%";
            videoData.gtmData.categoryVal = "Video Milestones";
            videoData.gtmData.actionVal = "video_progression_25%";
            videoData.gtmData.videoLink1 = videoSrc;
            videoData.gtmData.videoSource = "vimeo";
            videoData.gtmData.videoElapsedTime1 = Math.round(currentVideo.duration) * (25 / 100);
            videoData.gtmData.videoName = mainTitle;
            gtmDataLayerPush(videoData);
            videoData._progressMarkers[25] = true;
        }
        if (pct >= 50 && !videoData._progressMarkers[50]) {
            videoData.gtmData.eventName = "video_progression_50%";
            videoData.gtmData.categoryVal = "Video Milestones";
            videoData.gtmData.actionVal = "video_progression_50%";
            videoData.gtmData.videoLink1 = videoSrc;
            videoData.gtmData.videoSource = "vimeo";
            videoData.gtmData.videoElapsedTime1 = Math.round(currentVideo.duration) * (50 / 100);
            videoData.gtmData.videoName = mainTitle;
            gtmDataLayerPush(videoData);
            videoData._progressMarkers[50] = true;
        }
        if (pct >= 75 && !videoData._progressMarkers[75]) {
            videoData.gtmData.eventName = "video_progression_75%";
            videoData.gtmData.categoryVal = "Video Milestones";
            videoData.gtmData.actionVal = "video_progression_75%";
            videoData.gtmData.videoLink1 = videoSrc;
            videoData.gtmData.videoSource = "vimeo";
            videoData.gtmData.videoElapsedTime1 = Math.round(currentVideo.duration) * (75 / 100);
            videoData.gtmData.videoName = mainTitle;
            gtmDataLayerPush(videoData);
            videoData._progressMarkers[75] = true;
        }
    }

    function analyticsPlay(videoId, event) {
        var currentVideo = document.getElementById(videoId);
        var videoData = videoStatus[videoId];
        videoData.current = Math.round(currentVideo.currentTime);

        var titleTemp = jQuery(event).attr("poster");
        var videoSrc = jQuery(event).find('source').attr("src");
        var videoLoc = window.location.pathname;
        var videoTitle = getTitles(videoSrc, videoLoc);

        if (videoData.current >= 0 && videoData.current <= 2) {
            var ct = document.getElementsByTagName("video").currentTime;
            videoData.gtmData.eventName = "video_begins";
            videoData.gtmData.categoryVal = "Video";
            videoData.gtmData.actionVal = "video_begins";
            videoData.gtmData.videoName = mainTitle;
            videoData.gtmData.videoLink1 = videoSrc;
            videoData.gtmData.videoSource = "vimeo";
            videoData.gtmData.videoElapsedTime1 = "1";
            gtmDataLayerPush(videoData);
        }
    }

    function analyticspause(videoId, event) {
        var currentVideo = document.getElementById(videoId);
        var videoData = videoStatus[videoId];
        videoData.current = Math.round(currentVideo.currentTime);
        var pct = Math.floor(100 * videoData.current / currentVideo.duration);

        var titleTemp = jQuery(event).attr("poster");
        var videoSrc = jQuery(event).find('source').attr("src");
        var videoLoc = window.location.pathname;
        var mainTitle = getTitles(videoSrc, videoLoc);
        var video_elapsed = Math.round(currentVideo.currentTime);

        // Not setting <=99. At last pause will true, not taking 99 it should be 98
        if (pct > 1 && pct < 99) {
            videoData.gtmData.eventName = "video_paused";
            videoData.gtmData.categoryVal = "Video";
            videoData.gtmData.actionVal = "video_paused";
            videoData.gtmData.videoName = mainTitle;
            videoData.gtmData.videoLink1 = videoSrc;
            videoData.gtmData.videoSource = "vimeo";
            videoData.gtmData.videoElapsedTime1 = video_elapsed;
            gtmDataLayerPush(videoData);
        }
    }

    function analyticsEnd(videoId, event) {
        var currentVideo = document.getElementById(videoId);
        var videoData = videoStatus[videoId];
        
        var titleTemp = jQuery(event).attr("poster");
        var videoSrc = jQuery(event).find('source').attr("src");
        var videoLoc = window.location.pathname;
        var mainTitle = getTitles(videoSrc, videoLoc);

        videoData.gtmData.eventName = "video_ends";
        videoData.gtmData.categoryVal = "Video";
        videoData.gtmData.actionVal = "video_ends";
        videoData.gtmData.videoName = mainTitle;
        videoData.gtmData.videoLink1 = videoSrc;
        videoData.gtmData.videoElapsedTime1 = Math.round(currentVideo.currentTime);
        videoData.gtmData.videoSource = "vimeo";
        gtmDataLayerPush(videoData);

        // reset all the progress at the end, for better refresh.
        for (var marker = 0; marker < 100; marker += videoInterval) {
            videoData._progressMarkers[marker] = false;
        }
    }

    function eventHandler(e) {
        var videoId = $(this).attr('id');
        switch (e.type) {
            case 'timeupdate':
                analyticsTimeUpdate(videoId, e);
                break;
            case 'play':
                analyticsPlay(videoId, e);
                break;
            case 'pause':
                analyticspause(videoId, e);
                break;
            case 'ended':
                analyticsEnd(videoId, e)
                break;
            default:
                break;
        }
    }

    $('video').each(function() {
        var videoTagId = $(this).attr('id');
        if (!videoTagId) {
            videoTagId = 'gtm_video_' + Math.random().toString(36).slice(2);
            $(this).attr('id', videoTagId);
        }

        videoStatus[videoTagId] = {
            greatestWatchedPosition: 0,
            _progressMarkers: {},
            watched30: false,
            watched60: false,
            gtmData: {}
        };

        for (var marker = 0; marker < 100; marker += videoInterval) {
            videoStatus[videoTagId]._progressMarkers[marker] = false;
        }

        videoStatus[videoTagId].current = 0;
        $(this).on("play pause ended timeupdate", eventHandler);
    });
});
Editor is loading...
Leave a Comment