270 lines
11 KiB
TypeScript
270 lines
11 KiB
TypeScript
// Metro module 14158 (f14159).
|
|
import { useRef, useEffect, useCallback } from "react";
|
|
import { AppState } from "react-native";
|
|
import NetworkStore from "../../../../../stores/NetworkStore";
|
|
import { AnalyticEvents } from "../../../../../Constants";
|
|
import { useQuestImpression } from "../../../lib/analytics/ContentImpressionTrackerHooks";
|
|
import { trackQuestEvent } from "../../../lib/analytics/AnalyticsActions";
|
|
import { formatVideoProgressRatio } from "../../../utils/VideoQuestUtils";
|
|
import useVideoSegmentAnalytics, { SEGMENT_ANALYTICS_EMIT_INTERVAL_MS, SEGMENT_ANALYTICS_MIN_DURATION_MS } from "../../../../video_player/useVideoSegmentAnalytics";
|
|
import { PlayerState } from "../VideoQuestPlayer";
|
|
import { VideoPlayerState } from "../../../../../_bundle/Module5988";
|
|
import { VideoPauseReason } from "../../../QuestTypes";
|
|
import { isSourceError } from "../../AdsVideoUtils";
|
|
import MonitoringAgent from "../../../../monitoring/MonitoringAgent";
|
|
import { MetricEvents } from "../../../../../_bundle/Module5872";
|
|
export default function useVideoQuestPlayerAnalytics({ duration, isQuestCompleted, playerState, questId, sourceQuestContent, videoAssetId, videoSessionId }) {
|
|
let handlePlayerStateChange, handleLoadEnd, handleFirstFrame, handleSeek, ref3, ref4, ref5;
|
|
const ref = useRef(null);
|
|
const ref2 = useRef(null);
|
|
const questImpression = useQuestImpression();
|
|
useEffect(() => {
|
|
ref2.current = Date.now();
|
|
}, []);
|
|
useEffect(() => {
|
|
let c27646_0 = AppState.addEventListener("change", (a0) => {
|
|
if (ref.current != null) {
|
|
trackQuestEvent({
|
|
questId,
|
|
event: a0 === "active" ? AnalyticEvents.QUEST_VIDEO_APP_FOCUSED : AnalyticEvents.QUEST_VIDEO_APP_UNFOCUSED,
|
|
properties: {
|
|
video_timestamp_seconds: ref.current,
|
|
video_state: playerState,
|
|
video_session_id: videoSessionId,
|
|
impression_id: questImpression == null ? void 0 : questImpression.getId()
|
|
},
|
|
sourceQuestContent
|
|
});
|
|
}
|
|
});
|
|
return () => {
|
|
c27646_0.remove();
|
|
};
|
|
}, [questId, videoSessionId, playerState, questImpression, sourceQuestContent]);
|
|
const callback2 = useCallback((a0) => {
|
|
if (ref.current != null) {
|
|
trackQuestEvent({
|
|
questId,
|
|
event: AnalyticEvents.QUEST_VIDEO_SEGMENT_WATCHED,
|
|
properties: {
|
|
...a0,
|
|
video_asset_id: videoAssetId,
|
|
quest_completed: isQuestCompleted,
|
|
video_duration_sec: duration,
|
|
video_progress: formatVideoProgressRatio(a0.segment_end_sec, ref.current),
|
|
video_session_id: videoSessionId,
|
|
impression_id: questImpression == null ? void 0 : questImpression.getId()
|
|
},
|
|
sourceQuestContent
|
|
});
|
|
}
|
|
}, [questId, videoAssetId, isQuestCompleted, videoSessionId, duration, questImpression, sourceQuestContent]);
|
|
const videoSegmentAnalytics = useVideoSegmentAnalytics({
|
|
getCurrentVideoTime: useCallback(() => ref.current, []),
|
|
onAnalytics: callback2,
|
|
emitIntervalMs: SEGMENT_ANALYTICS_EMIT_INTERVAL_MS,
|
|
minSegmentDurationMs: SEGMENT_ANALYTICS_MIN_DURATION_MS
|
|
});
|
|
handlePlayerStateChange = videoSegmentAnalytics.handlePlayerStateChange;
|
|
handleLoadEnd = videoSegmentAnalytics.handleLoadEnd;
|
|
handleFirstFrame = videoSegmentAnalytics.handleFirstFrame;
|
|
handleSeek = videoSegmentAnalytics.handleSeek;
|
|
useEffect(() => {
|
|
if (PlayerState.PLAYING === playerState) {
|
|
handlePlayerStateChange(VideoPlayerState.PLAYING, null);
|
|
} else if (PlayerState.PAUSED === playerState) {
|
|
handlePlayerStateChange(VideoPlayerState.PAUSED, null);
|
|
} else if (PlayerState.ENDED === playerState) {
|
|
handlePlayerStateChange(VideoPlayerState.ENDED, null);
|
|
} else if (PlayerState.ERRORED === playerState) {
|
|
handlePlayerStateChange(VideoPlayerState.PAUSED, null);
|
|
}
|
|
}, [playerState, handlePlayerStateChange]);
|
|
ref3 = useRef(null);
|
|
const callback3 = useCallback(() => {
|
|
const duration2 = ref3.current != null ? Date.now() - ref3.current : null;
|
|
handleLoadEnd(duration2);
|
|
handleFirstFrame(0);
|
|
trackQuestEvent({
|
|
questId,
|
|
event: AnalyticEvents.QUEST_VIDEO_LOADING_ENDED,
|
|
properties: {
|
|
video_asset_id: videoAssetId,
|
|
network_connection_speed: NetworkStore.getEffectiveConnectionSpeed(),
|
|
duration: duration2,
|
|
video_session_id: videoSessionId,
|
|
impression_id: questImpression == null ? void 0 : questImpression.getId()
|
|
},
|
|
sourceQuestContent
|
|
});
|
|
}, [handleLoadEnd, handleFirstFrame, questId, videoAssetId, videoSessionId, questImpression, sourceQuestContent]);
|
|
const callback4 = useCallback(() => {
|
|
handlePlayerStateChange(VideoPlayerState.ENDED, null);
|
|
}, [handlePlayerStateChange]);
|
|
const callback5 = useCallback(() => {
|
|
ref3.current = Date.now();
|
|
trackQuestEvent({
|
|
questId,
|
|
event: AnalyticEvents.QUEST_VIDEO_LOADING_STARTED,
|
|
properties: {
|
|
video_asset_id: videoAssetId,
|
|
network_connection_speed: NetworkStore.getEffectiveConnectionSpeed(),
|
|
video_session_id: videoSessionId,
|
|
is_hls_supported: true,
|
|
impression_id: questImpression == null ? void 0 : questImpression.getId()
|
|
},
|
|
sourceQuestContent
|
|
});
|
|
}, [questId, videoSessionId, videoAssetId, questImpression, sourceQuestContent]);
|
|
ref4 = useRef(null);
|
|
ref5 = useRef(-1);
|
|
const callback = useCallback((a0) => {
|
|
const effectiveConnectionSpeed = NetworkStore.getEffectiveConnectionSpeed();
|
|
if (a0) {
|
|
ref4.current = Date.now();
|
|
ref5.current = ref5.current + 1;
|
|
trackQuestEvent({
|
|
questId,
|
|
event: AnalyticEvents.QUEST_VIDEO_BUFFERING_STARTED,
|
|
properties: {
|
|
video_asset_id: videoAssetId,
|
|
network_connection_speed: effectiveConnectionSpeed,
|
|
buffer_index: ref5.current,
|
|
video_session_id: videoSessionId,
|
|
impression_id: questImpression == null ? void 0 : questImpression.getId()
|
|
},
|
|
sourceQuestContent
|
|
});
|
|
} else {
|
|
trackQuestEvent({
|
|
questId,
|
|
event: AnalyticEvents.QUEST_VIDEO_BUFFERING_ENDED,
|
|
properties: {
|
|
video_asset_id: videoAssetId,
|
|
network_connection_speed: effectiveConnectionSpeed,
|
|
duration: ref4.current != null ? Date.now() - ref4.current : null,
|
|
buffer_index: ref5.current,
|
|
video_session_id: videoSessionId,
|
|
impression_id: questImpression == null ? void 0 : questImpression.getId()
|
|
},
|
|
sourceQuestContent
|
|
});
|
|
}
|
|
}, [questId, videoAssetId, videoSessionId, questImpression, sourceQuestContent]);
|
|
const callback6 = useCallback(() => {
|
|
if (ref2.current != null) {
|
|
trackQuestEvent({
|
|
questId,
|
|
event: AnalyticEvents.QUEST_VIDEO_TIME_TO_FIRST_FRAME,
|
|
properties: {
|
|
duration_ms: Date.now() - ref2.current,
|
|
video_session_id: videoSessionId,
|
|
video_asset_id: videoAssetId,
|
|
impression_id: questImpression == null ? void 0 : questImpression.getId()
|
|
},
|
|
sourceQuestContent
|
|
});
|
|
}
|
|
}, [questId, videoSessionId, videoAssetId, questImpression, sourceQuestContent]);
|
|
const callback7 = useCallback(() => {
|
|
trackQuestEvent({
|
|
questId,
|
|
event: AnalyticEvents.QUEST_VIDEO_RESUMED,
|
|
properties: {
|
|
video_timestamp_seconds: ref.current,
|
|
pause_reason: VideoPauseReason.PAUSE_BUTTON,
|
|
video_session_id: videoSessionId,
|
|
impression_id: questImpression == null ? void 0 : questImpression.getId()
|
|
},
|
|
sourceQuestContent
|
|
});
|
|
}, [questId, videoSessionId, questImpression, sourceQuestContent]);
|
|
const callback8 = useCallback(() => {
|
|
trackQuestEvent({
|
|
questId,
|
|
event: AnalyticEvents.QUEST_VIDEO_PAUSED,
|
|
properties: {
|
|
video_timestamp_seconds: ref.current,
|
|
reason: VideoPauseReason.PAUSE_BUTTON,
|
|
video_session_id: videoSessionId,
|
|
impression_id: questImpression == null ? void 0 : questImpression.getId()
|
|
},
|
|
sourceQuestContent
|
|
});
|
|
}, [questId, videoSessionId, questImpression, sourceQuestContent]);
|
|
return {
|
|
handleBufferAnalytics: callback,
|
|
handleEndAnalytics: callback4,
|
|
handleErrorAnalytics: useCallback((a0) => {
|
|
let current = ref.current;
|
|
if (current == null) {
|
|
current = 0;
|
|
}
|
|
const properties = {
|
|
video_progress: formatVideoProgressRatio(duration, current),
|
|
video_error_type: null,
|
|
video_asset_id: videoAssetId,
|
|
network_connection_speed: null,
|
|
video_session_id: videoSessionId,
|
|
video_error_code: a0 == null ? void 0 : a0.error.code,
|
|
video_error_message: null,
|
|
video_network_state: null,
|
|
impression_id: null
|
|
};
|
|
let video_error_message = a0 == null ? void 0 : a0.error.localizedDescription;
|
|
if (video_error_message == null) {
|
|
video_error_message = a0 == null ? void 0 : a0.error.errorString;
|
|
}
|
|
properties.video_error_message = video_error_message;
|
|
properties.impression_id = questImpression == null ? void 0 : questImpression.getId();
|
|
trackQuestEvent({
|
|
questId,
|
|
event: AnalyticEvents.QUEST_VIDEO_ERROR,
|
|
properties,
|
|
sourceQuestContent
|
|
});
|
|
if (isSourceError(a0)) {
|
|
MonitoringAgent.increment({
|
|
name: MetricEvents.QUEST_VIDEO_ERROR,
|
|
tags: [`quest_id:${questId}`, "error_type:SOURCE_ERROR"]
|
|
});
|
|
}
|
|
}, [duration, questId, videoAssetId, videoSessionId, questImpression, sourceQuestContent]),
|
|
handleLoadAnalytics: callback3,
|
|
handleLoadStartAnalytics: callback5,
|
|
handlePausePlaybackAnalytics: callback8,
|
|
handleProgressAnalytics: useCallback((a0, a1, a2) => {
|
|
ref.current = a2;
|
|
trackQuestEvent({
|
|
questId,
|
|
event: AnalyticEvents.QUEST_VIDEO_PROGRESSED,
|
|
properties: {
|
|
progress: a0,
|
|
video_timestamp_seconds: a1,
|
|
video_session_id: videoSessionId,
|
|
impression_id: questImpression == null ? void 0 : questImpression.getId()
|
|
},
|
|
sourceQuestContent
|
|
});
|
|
}, [questId, videoSessionId, questImpression, sourceQuestContent]),
|
|
handleReadyForDisplayAnalytics: callback6,
|
|
handleResumePlaybackAnalytics: callback7,
|
|
handleSeekAnalytics: useCallback((a0, a1) => {
|
|
handleSeek();
|
|
if (a0 != null) {
|
|
trackQuestEvent({
|
|
questId,
|
|
event: AnalyticEvents.QUEST_VIDEO_SEEKED,
|
|
properties: {
|
|
from_time_sec: a0,
|
|
to_time_sec: a1,
|
|
video_session_id: videoSessionId,
|
|
impression_id: questImpression == null ? void 0 : questImpression.getId()
|
|
},
|
|
sourceQuestContent
|
|
});
|
|
}
|
|
}, [handleSeek, questId, videoSessionId, questImpression, sourceQuestContent])
|
|
};
|
|
};
|