@segment/analytics.js-video-plugins
Advanced tools
Comparing version
{ | ||
"name": "@segment/analytics.js-video-plugins", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -20,6 +20,6 @@ import fetch from 'unfetch' | ||
} | ||
this.metadata = [] | ||
this.metadata = [{ playback: { video_player: 'youtube' }, content: {} }] | ||
this.playlistIndex = 0 | ||
} | ||
initialize() { | ||
@@ -37,13 +37,3 @@ // Youtube API requires listeners to exist as top-level props on window object | ||
onPlayerReady(event) { // this fires when the player html element loads | ||
const self = this | ||
if (this.player.getPlaylistId()) { | ||
this.player.cuePlaylist({ | ||
listType: 'playlist', | ||
list: this.player.getPlaylistId() | ||
}) | ||
} else { | ||
const videoData = this.player.getVideoData() | ||
this.player.cueVideoById(videoData.video_id) | ||
} | ||
this.retrieveMetadata() | ||
} | ||
@@ -95,6 +85,7 @@ | ||
let videoData = this.player.getVideoData() | ||
console.log(this.player.getPlaylist()) | ||
let playlist = this.player.getPlaylist() || [videoData.video_id] | ||
const videoIds = playlist.join() | ||
fetch(`https://www.googleapis.com/youtube/v3/videos?id=${videoIds}&part=snippet,contentDetails&key=${this.apiKey}`) | ||
@@ -109,2 +100,3 @@ .then(res => { | ||
}).then(json => { | ||
this.metadata = [] | ||
let total_length = 0 | ||
@@ -122,5 +114,5 @@ for (var i=0; i< playlist.length; i++) { | ||
}) | ||
total_length += YTDurationToSeconds(videoInfo.contentDetails.duration) | ||
total_length += YTDurationToSeconds(videoInfo.contentDetails.duration) | ||
} | ||
for (var i=0; i<playlist.length; i++) { | ||
@@ -187,3 +179,3 @@ this.metadata[i].playback = { | ||
} | ||
if (this.isPaused) { | ||
@@ -199,3 +191,3 @@ this.track('Video Playback Resumed', this.metadata[this.playlistIndex].playback) | ||
if (this.isBuffering) { | ||
this.track('Video Playback Buffer Completed', this.metadata[this.playlistIndex].playback) | ||
this.track('Video Playback Buffer Completed', this.metadata[this.playlistIndex].playback) | ||
this.isBuffering = false | ||
@@ -224,3 +216,3 @@ } | ||
const playlist = this.player.getPlaylist() | ||
if ((playlist && playlistIndex === playlist.length-1) || (playlistIndex === -1)) { | ||
@@ -227,0 +219,0 @@ this.track('Video Playback Completed', this.metadata[this.playlistIndex].playback) |
@@ -9,3 +9,3 @@ import YoutubeAnalytics from '.' | ||
items: [ { kind: 'youtube#video', | ||
etag: '"XI7nbFXulYBIpL0ayR_gDh3eu1k/l8T3Ps_vgKQV_OC2bHkLGQ44KAk"', | ||
etag: '"XI7nbFXulYBIpL0ayR_gDh3eu1k/l8T3Ps_vgKQV_OC2bHkLGQ44KAk"', | ||
id: 'M7lc1UVf-VE', | ||
@@ -168,19 +168,8 @@ snippet: | ||
describe('onPlayerReady', () => { | ||
it('should cue a playlist if a playlist id has been provided', () => { | ||
playlistId = '1234' | ||
const stub = sinon.stub(youtubeAnalytics.player, 'cuePlaylist') | ||
it('should call retrieveMetdata', () => { | ||
const stub = sinon.stub(youtubeAnalytics, 'retrieveMetadata') | ||
youtubeAnalytics.onPlayerReady() | ||
sinon.assert.calledWith(stub, { | ||
listType: 'playlist', | ||
list: playlistId | ||
}) | ||
sinon.assert.called(stub) | ||
}) | ||
it('should cue a single video if no playlist id has been provided', () => { | ||
const stub = sinon.stub(youtubeAnalytics.player, 'cueVideoById') | ||
youtubeAnalytics.onPlayerReady() | ||
sinon.assert.calledWith(stub, videoId) | ||
}) | ||
}) | ||
@@ -211,3 +200,3 @@ | ||
}, | ||
playback: { total_length: 1344, video_player: 'youtube' } | ||
playback: { total_length: 1344, video_player: 'youtube' } | ||
} | ||
@@ -265,3 +254,3 @@ ]) | ||
youtubeAnalytics.initialize() | ||
youtubeAnalytics.metadata = [{ playback: {}, content: {} }] | ||
youtubeAnalytics.metadata = [{ playback: {}, content: {} }] | ||
}) | ||
@@ -298,3 +287,3 @@ it('should update playback metadata', () => { | ||
youtubeAnalytics.metadata = [{ playback: {}, content: {} }] | ||
stub = sinon.stub(youtubeAnalytics, 'track') | ||
stub = sinon.stub(youtubeAnalytics, 'track') | ||
playback = { position: playerTime, quality: playbackQuality, sound: volume } | ||
@@ -315,3 +304,3 @@ content = { | ||
} | ||
youtubeAnalytics.metadata = [{playback, content}] | ||
youtubeAnalytics.metadata = [{playback, content}] | ||
}) | ||
@@ -340,3 +329,3 @@ describe('Singular Video Playback', () => { | ||
sinon.assert.calledWith(stub, 'Video Playback Seek Started', playback) | ||
sinon.assert.calledWith(stub, 'Video Playback Seek Completed', playback) | ||
sinon.assert.calledWith(stub, 'Video Playback Seek Completed', playback) | ||
sinon.assert.calledWith(stub, 'Video Playback Buffer Started', playback) | ||
@@ -348,3 +337,3 @@ }) | ||
youtubeAnalytics.playbackStarted = true | ||
window.segmentYoutubeOnStateChange({data: YT.PlayerState.BUFFERING}) | ||
window.segmentYoutubeOnStateChange({data: YT.PlayerState.BUFFERING}) | ||
@@ -363,3 +352,3 @@ assert(youtubeAnalytics.isBuffering) | ||
assert(youtubeAnalytics.contentStarted) | ||
sinon.assert.calledWith(stub, 'Video Content Started', content) | ||
sinon.assert.calledWith(stub, 'Video Content Started', content) | ||
}) | ||
@@ -380,3 +369,3 @@ | ||
youtubeAnalytics.isPaused = true | ||
window.segmentYoutubeOnStateChange({data: YT.PlayerState.PLAYING}) | ||
@@ -394,3 +383,3 @@ assert(!youtubeAnalytics.isPaused) | ||
assert(!youtubeAnalytics.isBuffering) | ||
sinon.assert.calledWith(stub, 'Video Playback Buffer Completed', playback) | ||
sinon.assert.calledWith(stub, 'Video Playback Buffer Completed', playback) | ||
}) | ||
@@ -406,3 +395,3 @@ | ||
assert(youtubeAnalytics.isSeeking) | ||
sinon.assert.calledWith(stub, 'Video Playback Seek Started', playback) | ||
sinon.assert.calledWith(stub, 'Video Playback Seek Started', playback) | ||
}) | ||
@@ -412,5 +401,5 @@ | ||
window.segmentYoutubeOnStateChange({data: YT.PlayerState.PAUSED}) | ||
assert(youtubeAnalytics.isPaused) | ||
sinon.assert.calledWith(stub, 'Video Playback Paused', playback) | ||
sinon.assert.calledWith(stub, 'Video Playback Paused', playback) | ||
}) | ||
@@ -417,0 +406,0 @@ }) |
278255
235.29%21
5%1620
-0.98%