Skip to content

Commit

Permalink
Merge pull request #1115 from adobe/media-fix
Browse files Browse the repository at this point in the history
Fix - instantiate instance  tracker state on session start only
  • Loading branch information
jonsnyder authored May 17, 2024
2 parents beac464 + 587dbc3 commit ec00b7b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
20 changes: 10 additions & 10 deletions src/components/LegacyMediaAnalytics/createGetInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { EVENT, MEDIA_EVENTS_INTERNAL } from "./constants/constants.js";
import {
includes,
isEmptyObject,
isNil,
isNonEmptyArray,
isNumber,
} from "../../utils/index.js";
Expand All @@ -22,10 +23,13 @@ import {
} from "./constants/mediaKeysToXdmConverter.js";

export default ({ logger, trackMediaSession, trackMediaEvent, uuid }) => {
let trackerState = {
qoe: null,
lastPlayhead: 0,
playerId: uuid(),
let trackerState = null;
const instantiateTracker = () => {
trackerState = {
qoe: null,
lastPlayhead: 0,
playerId: uuid(),
};
};
const getEventType = ({ eventType }) => {
if (
Expand Down Expand Up @@ -98,19 +102,15 @@ export default ({ logger, trackMediaSession, trackMediaEvent, uuid }) => {

return {
trackSessionStart: (mediaObject, contextData = {}) => {
if (isEmptyObject(mediaObject)) {
if (isNil(mediaObject) || isEmptyObject(mediaObject)) {
logger.warn("Invalid media object");
return {};
}
if (trackerState === null) {
logger.warn(
"The Media Session was completed. Restarting a new session.",
);
trackerState = {
qoe: null,
lastPlayhead: 0,
playerId: uuid(),
};
instantiateTracker();
}
const xdm = createXdmObject({
eventType: MEDIA_EVENTS_INTERNAL.SessionStart,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe("createGetInstance", () => {
trackMediaEvent,
uuid,
});
result.trackSessionStart({ sessionDetails: {} });
result.trackPlay();

expect(trackMediaEvent).toHaveBeenCalledWith({
Expand All @@ -66,6 +67,7 @@ describe("createGetInstance", () => {
trackMediaEvent,
uuid,
});
result.trackSessionStart({ sessionDetails: {} });
result.trackPause();

expect(trackMediaEvent).toHaveBeenCalledWith({
Expand Down Expand Up @@ -129,6 +131,7 @@ describe("createGetInstance", () => {
trackMediaEvent,
uuid,
});
result.trackSessionStart({ sessionDetails: {} });
result.trackError("error");

expect(trackMediaEvent).toHaveBeenCalledWith({
Expand All @@ -150,6 +153,7 @@ describe("createGetInstance", () => {
trackMediaEvent,
uuid,
});
result.trackSessionStart({ sessionDetails: {} });
result.trackComplete();

expect(trackMediaEvent).toHaveBeenCalledWith({
Expand All @@ -167,6 +171,7 @@ describe("createGetInstance", () => {
trackMediaEvent,
uuid,
});
result.trackSessionStart({ sessionDetails: {} });
result.trackSessionEnd();

expect(trackMediaEvent).toHaveBeenCalledWith({
Expand All @@ -187,6 +192,7 @@ describe("createGetInstance", () => {
const state = {
name: "muted",
};
result.trackSessionStart({ sessionDetails: {} });
result.trackEvent("stateStart", state);

expect(trackMediaEvent).toHaveBeenCalledWith({
Expand All @@ -209,6 +215,7 @@ describe("createGetInstance", () => {
const state = {
name: "muted",
};
result.trackSessionStart({ sessionDetails: {} });
result.trackEvent("stateEnd", state);

expect(trackMediaEvent).toHaveBeenCalledWith({
Expand Down Expand Up @@ -241,7 +248,7 @@ describe("createGetInstance", () => {
"a.media.ad.advertiser": "Sample Advertiser 2",
"a.media.ad.campaign": "csmpaign2",
};

result.trackSessionStart({ sessionDetails: {} });
result.trackEvent("adStart", { advertisingDetails }, adContextData);

expect(trackMediaEvent).toHaveBeenCalledWith({
Expand Down

0 comments on commit ec00b7b

Please sign in to comment.