-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcatchingEvents.html
104 lines (82 loc) · 3.1 KB
/
catchingEvents.html
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
<!DOCTYPE html>
<html>
<body>
<!-- google analytics -->
<script src="assets/js/googleanalytics.js"></script>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="youTubePlayer"></div>
<script>
//var random = Math.floor(Math.random()*1000);
//var USER_ID = 'mit-000-' + random;
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('youTubePlayer', {
height: '390',
width: '640',
videoId: 'sGbxmsDFVnE',
events: {
'onStateChange': onPlayerStateChange
}
});
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
function getCurrentTime(){
var currentdate = new Date();
var prettyTime = currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds() + ", "
+ currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear();
return prettyTime;
}
// ---------------- START: GOOGLE ANALYTICS REPORTING ----------------
// track when user clicks to Play
if (event.data == YT.PlayerState.PLAYING) {
console.log('play, ' + getCurrentTime());
ga('send', {
hitType: 'event',
eventCategory: 'Video',
eventAction: 'play',
eventLabel: 'forceAwakens'
});
}
// track when user clicks to Pause
if (event.data == YT.PlayerState.PAUSED) {
console.log('pause, ' + getCurrentTime());
ga('send', {
hitType: 'event',
eventCategory: 'Video',
eventAction: 'pause',
eventLabel: 'forceAwakens'
});
}
// track when video ends
if (event.data == YT.PlayerState.ENDED) {
console.log('end, ' + getCurrentTime());
ga('send', {
hitType: 'event',
eventCategory: 'Video',
eventAction: 'end',
eventLabel: 'forceAwakens'
});
}
// ---------------- END: GOOGLE ANALYTICS REPORTING ----------------
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>