132 lines
4.2 KiB
JavaScript
132 lines
4.2 KiB
JavaScript
jQuery(function($)
|
|
{
|
|
|
|
// Default time delay before checking location
|
|
var call_back_delay = 500;
|
|
|
|
// Set some flags for tracking & execution
|
|
var scroll_timer = null;
|
|
var scroller = false; // indicate the user has begun scrolling
|
|
|
|
scroll_points_in_page = jQuery(".scroll_point");
|
|
video_screenshots = jQuery(".video-screenshot");
|
|
video_parents = jQuery(".video-parent");
|
|
|
|
var apply_video_click = function (video) {
|
|
$(video).click(function (e) {
|
|
var v = $(this)[0];
|
|
if(v.paused) {
|
|
v.play();
|
|
$(v).attr("data-paused", "false");
|
|
}
|
|
else {
|
|
v.pause();
|
|
$(v).attr("data-paused", "true");
|
|
}
|
|
});
|
|
}
|
|
|
|
for(var i = 0; i < video_parents.length; i++) {
|
|
let video_with_event = $(video_parents[i]).children("video")[0];
|
|
let play_icon = $($(video_parents[i]).children(".play-pause")[0]).children("i");
|
|
let parent = $(video_parents[i]);
|
|
|
|
parent.addClass("playing");
|
|
|
|
video_with_event.onpause = function () {
|
|
parent.addClass("paused");
|
|
parent.removeClass("playing");
|
|
play_icon.addClass("fa-play");
|
|
play_icon.removeClass("fa-pause");
|
|
}
|
|
video_with_event.onplay = function () {
|
|
parent.removeClass("paused");
|
|
parent.addClass("playing");
|
|
play_icon.removeClass("fa-play");
|
|
play_icon.addClass("fa-pause");
|
|
}
|
|
|
|
}
|
|
|
|
for(var i = 0; i < video_screenshots.length; i++) {
|
|
apply_video_click(video_screenshots[i]);
|
|
}
|
|
|
|
$(".play-pause").click(function (e) { $(this).next("video").click();});
|
|
|
|
|
|
// Check the location and track user
|
|
function track_location()
|
|
{
|
|
view_bottom = $(window).height() + $(window).scrollTop();// the bottom of the visible area
|
|
|
|
// loop through the scroll points and send events for any within the visible part of the page
|
|
for(var i = 0; i < scroll_points_in_page.length; i++)
|
|
{
|
|
check_scroll_point(scroll_points_in_page[i]);
|
|
}
|
|
for(var i = 0; i < video_screenshots.length; i++)
|
|
{
|
|
video_screenshots_play_pause(video_screenshots[i]);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var check_scroll_point = function(current_value)
|
|
{
|
|
var current_object = $(current_value);
|
|
|
|
if (view_bottom >= current_object.offset().top && $(window).scrollTop() <= current_object.offset().top && (current_object.attr("hit") === undefined || current_object.attr("hit") === null))
|
|
{
|
|
if (!debug_mode)
|
|
{
|
|
ga('send', 'event', current_object.attr("data-eventCategory") + "_scroll_points", "in_view", current_object.attr("data-eventLabel"));
|
|
}
|
|
else
|
|
{
|
|
console.log(current_object.attr("data-eventCategory") + "_scroll_points, " + current_object.attr("data-eventLabel"));
|
|
}
|
|
|
|
$("[data-eventLabel='" + current_object.attr("data-eventLabel")+ "']").attr("hit", "1"); // after the point has been hit don't count it again
|
|
}
|
|
|
|
}
|
|
|
|
var video_screenshots_play_pause = function(current_value)
|
|
{
|
|
var current_object = $(current_value);
|
|
var video_middle = current_object.offset().top + (current_object.height() / 2);
|
|
var video_on_screen = view_bottom > video_middle && video_middle > $(window).scrollTop();
|
|
|
|
if (
|
|
window.innerWidth > 600
|
|
&& video_on_screen
|
|
&& current_value.paused
|
|
&& current_object.attr("data-paused") === "false"
|
|
)
|
|
{
|
|
current_value.play();
|
|
}
|
|
else if (!current_value.paused && !video_on_screen) {
|
|
current_value.pause();
|
|
}
|
|
|
|
}
|
|
|
|
// Track the scrolling and track location
|
|
$(".page").scroll(function()
|
|
{
|
|
if (scroll_timer) {
|
|
clearTimeout(scroll_timer);
|
|
}
|
|
|
|
// Use a buffer so we don't call track_location too often.
|
|
scroll_timer = setTimeout(track_location, call_back_delay);
|
|
|
|
});
|
|
|
|
|
|
});
|