UNPKG

@aller/blink

Version:

A library for tracking user behaviour.

243 lines 7.99 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); var prepare_video_watch_event_1 = __importStar(require("../prepare-video-watch-event")); var config_1 = require("../../config/config"); function mockBlinkEvent(_a) { var id = _a.id, playReason = _a.playReason, stopReason = _a.stopReason, activeTime = _a.activeTime; return { type: 'videoWatch', pageView: 'some-pageview-uuid', site: 'www.dagbladet.no', referrer: 'www.dinside.no', userId: 'user5', id: id, videoId: id, videoPlayReason: playReason, videoStopReason: stopReason, version: config_1.VERSION, activeTime: activeTime, videoPlayMuted: true, videoStopMuted: false, videoPlayPosition: 15, videoStopPosition: 50.3, videoPlayVolume: 30, videoStopVolume: 40, }; } function mockWatchEvent(_a) { var type = _a.type, time = _a.time, reason = _a.reason, videoId = _a.videoId; var isWatchStartEvent = type === 'start' || type === 'shown'; return { type: type, time: time, muted: isWatchStartEvent, position: isWatchStartEvent ? 15 : 50.3, reason: reason, videoId: videoId, volume: isWatchStartEvent ? 30 : 40, playerId: 'test-player-id', }; } describe('prepareVideoWatchEvents', function () { it('should format properly based on page state', function () { var videoEvents = [ { type: 'start', time: new Date(5), muted: true, position: 11.2, reason: 'autostart', videoId: '123', volume: 30, playerId: 'test-player-id', }, { type: 'stop', time: new Date(50), muted: false, position: 50.3, reason: 'exit', videoId: '123', volume: 40, playerId: 'test-player-id', }, ]; var input = { page: { id: 'default', state: { general: { pageView: 'some-pageview-uuid', site: 'www.dagbladet.no', referrer: 'www.dinside.no', userId: 'user5', }, video: { events: videoEvents, }, player: { 'test-player-id': [ { type: 'shown', time: new Date(10), muted: true, position: 11.2, reason: 'viewable', volume: 30, }, ], }, }, }, playerId: 'test-player-id', videoId: '123', time: new Date(2), }; var expected = [ { type: 'videoWatch', pageView: 'some-pageview-uuid', site: 'www.dagbladet.no', referrer: 'www.dinside.no', userId: 'user5', version: config_1.VERSION, id: '123', videoId: '123', videoPlayMuted: true, videoStopMuted: false, videoPlayPosition: 11.2, videoStopPosition: 50.3, videoPlayVolume: 30, videoStopVolume: 40, videoPlayReason: 'viewable', videoStopReason: 'exit', activeTime: 40, }, ]; expect(prepare_video_watch_event_1.default(input)).toEqual(expected); }); it('should format properly when no events in page state', function () { var videoEvents = []; var input = { page: { id: 'default', state: { general: { pageView: 'some-pageview-uuid', site: 'www.dagbladet.no', referrer: 'www.dinside.no', userId: 'user5', }, video: { events: videoEvents, }, player: { 'test-player-id': [{}], }, }, }, videoId: '123', playerId: 'test-player-id', time: new Date(2), }; expect(prepare_video_watch_event_1.default(input)).toEqual([]); }); }); describe('prepareAllVideoWatchEvents', function () { it('should format properly based on page state', function () { var videoEvents = [ mockWatchEvent({ type: 'start', time: new Date(9), reason: 'autostart', videoId: '123', }), mockWatchEvent({ type: 'start', time: new Date(11), reason: 'autostart', videoId: 'ABC', }), mockWatchEvent({ type: 'stop', time: new Date(50), reason: 'exit', videoId: '123', }), mockWatchEvent({ type: 'stop', time: new Date(55), reason: 'pause', videoId: 'ABC', }), // A second event for id 123 mockWatchEvent({ type: 'start', time: new Date(100), reason: 'interaction', videoId: '123', }), mockWatchEvent({ type: 'stop', time: new Date(120), reason: 'pause', videoId: '123', }), ]; var page = { id: 'default', state: { general: { pageView: 'some-pageview-uuid', site: 'www.dagbladet.no', referrer: 'www.dinside.no', userId: 'user5', }, video: { events: videoEvents, }, player: { 'test-player-id': [ { type: 'shown', time: new Date(10), reason: 'viewable', muted: true, volume: 30, position: 15, }, ], }, }, }; var expected = [ mockBlinkEvent({ id: '123', playReason: 'viewable', stopReason: 'exit', activeTime: 40, }), mockBlinkEvent({ id: '123', playReason: 'interaction', stopReason: 'pause', activeTime: 20, }), mockBlinkEvent({ id: 'ABC', activeTime: 44, playReason: 'autostart', stopReason: 'pause', }), ]; expect(prepare_video_watch_event_1.getAllVideoWatchEventsPrepared(page)).toEqual(expected); }); }); //# sourceMappingURL=prepare-video-watch-event.test.js.map