lightbox.min.js
12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
|-------------------------------------------
| Simple Lightbox Video jQuery
|-------------------------------------------
|
|
| Author: Pedro Marcelo
| Author URI: https://github.com/pedromarcelojava
| Plugin URI: https://github.com/pedromarcelojava/Simple-Lightbox-Video-jQuery
| Version: 2.0
*/
var vimeoGAJS = (window.vimeoGAJS) ? window.vimeoGAJS : {};
(function ($) {
vimeoGAJS = {
iframes : [],
gaTracker : undefined,
eventMarker : {},
init: function () {
vimeoGAJS.iframes = $('iframe#slvj-video-embed');
$.each(vimeoGAJS.iframes, function (index, iframe) {
var iframeId = $(iframe).attr('id');
vimeoGAJS.eventMarker[iframeId] = {
'progress25' : false,
'progress50' : false,
'progress75' : false,
'videoPlayed' : false,
'videoPaused' : false,
'videoResumed' : false,
'videoSeeking' : false,
'videoCompleted' : false,
'timePercentComplete' : 0
};
});
// Check which version of Google Analytics is used
if (typeof ga === "function") {
vimeoGAJS.gaTracker = 'ua'; // Universal Analytics (universal.js)
//console.info('Universal Analytics');
}
if (typeof _gaq !== "undefined" && typeof _gaq.push === "function") {
vimeoGAJS.gaTracker = 'ga'; // Classic Analytics (ga.js)
//console.info('Classic Analytics');
}
if (typeof dataLayer !== "undefined" && typeof dataLayer.push === "function") {
vimeoGAJS.gaTracker = 'gtm'; // Google Tag Manager (dataLayer)
//console.info('Google Tag Manager');
}
// Listen for messages from the player
if (window.addEventListener) {
window.addEventListener('message', vimeoGAJS.onMessageReceived, false);
} else {
window.attachEvent('onmessage', vimeoGAJS.onMessageReceived, false);
}
},
// Handle messages received from the player
onMessageReceived: function(e) {
if (e.origin.replace('https:', 'http:') !== "http://player.vimeo.com" || typeof vimeoGAJS.gaTracker === 'undefined') {
//console.warn('Tracker is missing!');
return;
}
var data = JSON.parse(e.data),
iframeEl = $("#"+data.player_id),
iframeId = iframeEl.attr('id');
switch (data.event) {
case 'ready':
vimeoGAJS.onReady();
break;
case 'playProgress':
vimeoGAJS.onPlayProgress(data.data, iframeEl);
break;
case 'seek':
if (iframeEl.data('seek') && !vimeoGAJS.eventMarker[iframeId].videoSeeking) {
vimeoGAJS.sendEvent(iframeEl, 'Skipped video forward or backward');
vimeoGAJS.eventMarker[iframeId].videoSeeking = true; // Avoid subsequent seek trackings
}
break;
case 'play':
if (!vimeoGAJS.eventMarker[iframeId].videoPlayed) {
vimeoGAJS.sendEvent(iframeEl, 'Started video');
vimeoGAJS.eventMarker[iframeId].videoPlayed = true; // Avoid subsequent play trackings
} else if (!vimeoGAJS.eventMarker[iframeId].videoResumed && vimeoGAJS.eventMarker[iframeId].videoPaused) {
vimeoGAJS.sendEvent(iframeEl, 'Resumed video');
vimeoGAJS.eventMarker[iframeId].videoResumed = true; // Avoid subsequent resume trackings
}
break;
case 'pause':
vimeoGAJS.onPause(iframeEl);
break;
case 'finish':
if (!vimeoGAJS.eventMarker[iframeId].videoCompleted) {
vimeoGAJS.sendEvent(iframeEl, 'Completed video');
vimeoGAJS.eventMarker[iframeId].videoCompleted = true; // Avoid subsequent finish trackings
}
break;
}
},
getLabel : function(iframeEl) {
var iframeSrc = iframeEl.attr('src').split('?')[0];
var label = iframeSrc;
if (iframeEl.data('title')) {
label += ' (' + iframeEl.data('title') + ')';
} else if (iframeEl.attr('title')) {
label += ' (' + iframeEl.attr('title') + ')';
}
return label;
},
// Helper function for sending a message to the player
post : function (action, value, iframe) {
var data = {
method: action
};
if (value) {
data.value = value;
}
// Source URL
var iframeSrc = $(iframe).attr('src').split('?')[0];
console.log(iframeSrc);
iframe.contentWindow.postMessage(JSON.stringify(data), iframeSrc);
},
onReady :function() {
$.each(vimeoGAJS.iframes, function(index, iframe) {
vimeoGAJS.post('addEventListener', 'play', iframe);
vimeoGAJS.post('addEventListener', 'seek', iframe);
vimeoGAJS.post('addEventListener', 'pause', iframe);
vimeoGAJS.post('addEventListener', 'finish', iframe);
vimeoGAJS.post('addEventListener', 'playProgress', iframe);
});
},
onPause: function(iframeEl) {
var iframeId = iframeEl.attr('id');
if (vimeoGAJS.eventMarker[iframeId].timePercentComplete < 99 && !vimeoGAJS.eventMarker[iframeId].videoPaused) {
vimeoGAJS.sendEvent(iframeEl, 'Paused video');
vimeoGAJS.eventMarker[iframeId].videoPaused = true; // Avoid subsequent pause trackings
}
},
// Tracking video progress
onPlayProgress: function(data, iframeEl) {
var progress,
iframeId = iframeEl.attr('id');
vimeoGAJS.eventMarker[iframeId].timePercentComplete = Math.round((data.percent) * 100); // Round to a whole number
if (!iframeEl.data('progress')) {
return;
}
if (vimeoGAJS.eventMarker[iframeId].timePercentComplete > 24 && !vimeoGAJS.eventMarker[iframeId].progress25) {
progress = 'Played video: 25%';
vimeoGAJS.eventMarker[iframeId].progress25 = true;
}
if (vimeoGAJS.eventMarker[iframeId].timePercentComplete > 49 && !vimeoGAJS.eventMarker[iframeId].progress50) {
progress = 'Played video: 50%';
vimeoGAJS.eventMarker[iframeId].progress50 = true;
}
if (vimeoGAJS.eventMarker[iframeId].timePercentComplete > 74 && !vimeoGAJS.eventMarker[iframeId].progress75) {
progress = 'Played video: 75%';
vimeoGAJS.eventMarker[iframeId].progress75 = true;
}
if (progress) {
vimeoGAJS.sendEvent(iframeEl, progress);
}
},
// Send event to Classic Analytics, Universal Analytics or Google Tag Manager
sendEvent: function (iframeEl, action) {
var bounce = iframeEl.data('bounce');
var label = vimeoGAJS.getLabel(iframeEl);
switch (vimeoGAJS.gaTracker) {
case 'gtm':
dataLayer.push({'event': 'Vimeo', 'eventCategory': 'Vimeo', 'eventAction': action, 'eventLabel': label, 'eventValue': undefined, 'eventNonInteraction': (bounce) ? false : true });
break;
case 'ua':
ga('send', 'event', 'Vimeo', action, label, undefined, {'nonInteraction': (bounce) ? 0 : 1});
break;
case 'ga':
_gaq.push(['_trackEvent', 'Vimeo', action, label, undefined, (bounce) ? false : true]);
break;
}
}
};
$.extend(
$.fn, {
simpleLightboxVideo: function () {
var defaults = {
delayAnimation: 300,
keyCodeClose: 27
};
$.simpleLightboxVideo.vars = $.extend({}, defaults);
var video = this;
video.click(
function () {
if (window.innerHeight > 540) {
var margintop = (window.innerHeight - 540) / 2;
}
else {
var margintop = 0;
}
var ifr = '<iframe src="" width="640" height="480" id="slvj-video-embed" style="border:0;" data-progress="true" data-seek="true" data-bounce="true"></iframe>';
var close = '<div id="slvj-close-icon"></div>';
var lightbox = '<div class="slvj-lightbox" style="margin-top:' + margintop + 'px">';
var back = '<div id="slvj-back-lightbox">';
var bclo = '<div id="slvj-background-close"></div>';
var win = '<div id="slvj-window">';
var end = '</div></div></div>';
$('body').append(win + bclo + back + lightbox + close + ifr + end);
$('#slvj-window').hide();
if ($(this).data('videosite') == "youtube") {
var url = 'https://www.youtube.com/embed/' + $(this).data('videoid') + '?autoplay=1';
} else if ($(this).data('videosite') == "vimeo") {
var url = 'https://player.vimeo.com/video/' + $(this).data('videoid') + '?autoplay=1&api=1&player_id=slvj-video-embed';
}
$('#slvj-window').fadeIn();
$('#slvj-video-embed').attr('src', url);
$('#slvj-close-icon').click(
function () {
$('#slvj-window').fadeOut(
$.simpleLightboxVideo.vars.delayAnimation, function () {
$(this).remove();
}
);
}
);
$('#slvj-background-close').click(
function () {
$('#slvj-window').fadeOut(
$.simpleLightboxVideo.vars.delayAnimation, function () {
$(this).remove();
}
);
}
);
vimeoGAJS.init();
return false;
}
);
$(document).keyup(
function (e) {
if (e.keyCode == 27) {
$('#slvj-window').fadeOut(
$.simpleLightboxVideo.vars.delayAnimation, function () {
$(this).remove();
}
);
}
}
);
$(window).resize(
function () {
if (window.innerHeight > 540) {
var margintop = (window.innerHeight - 540) / 2;
}
else {
var margintop = 0;
}
$('.slvj-lightbox').css(
{
marginTop: margintop + 'px'
}
);
}
);
return false;
}
}
);
})(jQuery);
(function ($) {
$.simpleLightboxVideo = function (options, video) {
return $(video).simpleLightboxVideo();
}
})(jQuery);