forked from tylrholmes1/Vimeo-Only-Video-Tracking-Container
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVimeo Tracking Script GA.js
193 lines (163 loc) · 4.81 KB
/
Vimeo Tracking Script GA.js
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
/*!
* vimeo.ga.js | v0.4
* Copyright (c) 2014 Sander Heilbron (http://www.sanderheilbron.nl)
* MIT licensed
*/
(function($) {
"use strict";
// Check for iframe
if (!$('#vimeo-player').length) {
// console.info('iframe not available');
return;
}
var f = $('#vimeo-player'),
url = f.attr('src').split('?')[0], // Source URL
protocol = document.URL.split(':')[0], // Domain protocol (http or https)
trackProgress = f.data('progress'), // Data attribute to enable progress tracking
trackSeeking = f.data('seek'), // Data attribute to enable seek tracking
gaTracker,
progress25 = false,
progress50 = false,
progress75 = false,
videoPlayed = false,
videoPaused = false,
videoResumed = false,
videoSeeking = false,
videoCompleted = false,
timePercentComplete = 0;
// Match protocol with what is in document.URL
if (url.match(/^http/) === null) {
url = protocol + ':' + url;
}
// Check which version of Google Analytics is used
// Universal Analytics (universal.js)
if (typeof ga === "function") {
gaTracker = 'ua';
// console.info('Universal Analytics');
}
// Classic Analytics (ga.js)
if (typeof _gaq !== "undefined" && typeof _gaq.push === "function") {
gaTracker = 'ga';
// console.info('Classic Analytics');
}
// Google Tag Manager (dataLayer)
if (typeof dataLayer !== "undefined" && typeof dataLayer.push === "function") {
gaTracker = 'gtm';
// console.info('Google Tag Manager');
}
// Listen for messages from the player
if (window.addEventListener) {
window.addEventListener('message', onMessageReceived, false);
} else {
window.attachEvent('onmessage', onMessageReceived, false);
}
// Send event to Classic Analytics, Universal Analytics or Google Tag Manager
function sendEvent(action) {
switch (gaTracker) {
case 'gtm':
dataLayer.push({
'event' : 'Vimeo',
'eventCategory' : 'Vimeo',
'eventAction' : action,
'eventLabel' : url,
'eventValue' : undefined,
'eventNonInteraction' : true
});
break;
case 'ua':
ga('send', 'event', 'Vimeo', action, url, undefined, {
"nonInteraction" : 1
});
break;
case 'ga':
_gaq.push(['_trackEvent', 'Vimeo', action, url, undefined, true]);
break;
}
}
// Handle messages received from the player
function onMessageReceived(e) {
if (e.origin.replace('https:', 'http:') !== "http://player.vimeo.com" || typeof gaTracker === 'undefined') {
// console.warn('Tracker is missing!');
return;
}
var data = JSON.parse(e.data);
switch (data.event) {
case 'ready':
onReady();
break;
case 'playProgress':
onPlayProgress(data.data);
break;
case 'seek':
if (trackSeeking && !videoSeeking) {
sendEvent('Skipped video forward or backward');
videoSeeking = true; // Avoid subsequent seek trackings
}
break;
case 'play':
if (!videoPlayed) {
sendEvent('Started video');
videoPlayed = true; // Avoid subsequent play trackings
} else if (!videoResumed && videoPaused) {
sendEvent('Resumed video');
videoResumed = true; // Avoid subsequent resume trackings
}
break;
case 'pause':
onPause();
break;
case 'finish':
if (!videoCompleted) {
sendEvent('Completed video');
videoCompleted = true; // Avoid subsequent finish trackings
}
break;
}
}
// Helper function for sending a message to the player
function post(action, value) {
var data = {
method: action
};
if (value) {
data.value = value;
}
f[0].contentWindow.postMessage(JSON.stringify(data), url);
}
function onReady() {
post('addEventListener', 'play');
post('addEventListener', 'seek');
post('addEventListener', 'pause');
post('addEventListener', 'finish');
post('addEventListener', 'playProgress');
}
function onPause() {
if (timePercentComplete < 99 && !videoPaused) {
sendEvent('Paused video');
videoPaused = true; // Avoid subsequent pause trackings
}
}
// Tracking video progress
function onPlayProgress(data) {
timePercentComplete = Math.round((data.percent) * 100); // Round to a whole number
if (!trackProgress) {
return;
}
var progress;
if (timePercentComplete > 24 && !progress25) {
progress = 'Played video: 25%';
progress25 = true;
}
if (timePercentComplete > 49 && !progress50) {
progress = 'Played video: 50%';
progress50 = true;
}
if (timePercentComplete > 74 && !progress75) {
progress = 'Played video: 75%';
progress75 = true;
}
if (progress) {
sendEvent(progress);
}
}
})(jQuery);