Skip to content

Commit

Permalink
Merge pull request #54 from Wikia/IW-130
Browse files Browse the repository at this point in the history
IW-130 | User-Intended plays recognition module
  • Loading branch information
bkoval authored Nov 19, 2018
2 parents 7168bb2 + 1ee9437 commit 4ef4431
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/wikiajwplayer.js

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ function wikiaJWPlayerEvents(playerInstance, willAutoplay, logger) {
});

relatedPlugin.on('play', function (data) {
logger.info('related plugin play');
depth++;

playerInstance.trigger('relatedVideoPlay', {
Expand All @@ -106,7 +105,6 @@ function wikiaJWPlayerEvents(playerInstance, willAutoplay, logger) {
playerInstance.on('play', function (data) {
if (isPlayerPaused) {
playerInstance.trigger('videoResumed', data);
logger.info('videoResumed triggered');
}

isPlayerPaused = false;
Expand All @@ -119,11 +117,9 @@ function wikiaJWPlayerEvents(playerInstance, willAutoplay, logger) {
playerInstance.on('firstFrame', function () {
if (depth === 0) {
playerInstance.trigger('playerStart', { auto: willAutoplay });
logger.info('playerStart triggered');
}

playerInstance.trigger('videoStart');
logger.info('videoStart triggered');
});

playerInstance.on('mute', function () {
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ window.wikiaJWPlayer = function (elementId, options, callback) {

wikiaJWPlayerAllowControllOnTouchDevices(playerInstance);

wikiaJWPlayerUserIntendedPlayControl(
options.shouldForceUserIntendedPlay,
playerInstance,
options.tracking,
options.autoplay
);

if(options.watermark !== false) {
WikiaJWPlayerWatermarkPlugin.register();
}
Expand Down
87 changes: 87 additions & 0 deletions src/user-intended-play-control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
function wikiaJWPlayerUserIntendedPlayControl(isInitiallyUserIntendedPlay, playerInstance, tracker, willAutoplay) {
var isUserIntendedPlay = null;
var isUserIntendedByUnmuting = false;
var wasPausedByUserInteraction = false;
var customDimensionNumber = 39;
var customDimensionValueWhenIntended = 'user-intended';
var customDimensionValueWhenNotIntended = 'not-user-intended';

function setUserIntendedPlay(isUserIntended) {
if (isUserIntendedPlay === isUserIntended) {
return;
}

isUserIntendedPlay = isUserIntended;

if (typeof tracker.setCustomDimension !== 'function') {
return;
}

tracker.setCustomDimension(
customDimensionNumber, isUserIntended ?customDimensionValueWhenIntended : customDimensionValueWhenNotIntended
);
}

function onPause(data) {
if (data.pauseReason === 'interaction') {
wasPausedByUserInteraction = true;
}
}

function onPlay() {
if (wasPausedByUserInteraction) {
setUserIntendedPlay(true);
}
}

function onFullScreen() {
setUserIntendedPlay(true);
}

function onUnmute() {
setUserIntendedPlay(true);
isUserIntendedByUnmuting = true;
}

function onMute() {
if (isUserIntendedPlay && isUserIntendedByUnmuting) {
setUserIntendedPlay(false);
}
}

function onVideoThumbnailInsidePlayerClicked() {
setUserIntendedPlay(true);
}

function init() {
playerInstance.on('mute', function () {
if (playerInstance.getMute()) {
onMute();
} else {
onUnmute();
}
});

playerInstance.on('pause', onPause);
playerInstance.on('play', onPlay);
playerInstance.on('fullscreen', onFullScreen);

playerInstance.on('relatedVideoPlay', function (data) {
if (!data.auto) {
onVideoThumbnailInsidePlayerClicked();
}
});

if (!willAutoplay) {
setUserIntendedPlay(true);
} else if (isInitiallyUserIntendedPlay) {
setUserIntendedPlay(true);
} else {
setUserIntendedPlay(false);
}
}

playerInstance.once('ready', init);
}

window.wikiaJWPlayerUserIntendedPlayControl = wikiaJWPlayerUserIntendedPlayControl;

0 comments on commit 4ef4431

Please sign in to comment.