Skip to content

Commit

Permalink
fix: Check for window.screen when setting device_info (#936)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmi22186 authored Oct 15, 2024
1 parent 7bf090d commit 7ce742f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/sdkToEventsApiConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,16 @@ export function convertEvents(

device_info: {
platform: EventsApi.DeviceInformationPlatformEnum.web,
screen_width: typeof window !== 'undefined' ? window.screen.width : 0,
screen_height: typeof window !== 'undefined' ? window.screen.height : 0,
screen_width:
typeof window !== 'undefined' &&
typeof window.screen !== 'undefined'
? window.screen.width
: 0,
screen_height:
typeof window !== 'undefined' &&
typeof window.screen !== 'undefined'
? window.screen.height
: 0,
},
user_attributes: lastEvent.UserAttributes,
user_identities: convertUserIdentities(lastEvent.UserIdentities),
Expand Down
52 changes: 52 additions & 0 deletions test/src/tests-runtimeToBatchEventsDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,56 @@ describe('Old model to batch model conversion', () => {

done();
});

it('Set width and height to 0 when window is defined but screen is not defined', done => {
const originalScreen = window.screen;
delete window.screen;

const sdkEvent: SDKEvent = {
EventName: "Pause Event",
EventCategory: 8,
ExpandedEventCount: 0,
EventDataType: 4,
EventAttributes: {
content_duration: '120000',
content_id: "1234567",
content_title: "My sweet sweet media",
content_type: "Video",
media_session_id: "07be2e14-7e05-4053-bcb5-94950365822d",
playhead_position: '7023.335999999999',
stream_type: "OnDemand",
},
ConsentState: null,
CurrencyCode: null,
CustomFlags: {},
DataPlan: {},
Debug: true,
DeviceId: "0edd580e-d887-44e4-89ae-cd65aa0ee933",
Location: null,
MPID: "-8433569646818451201",
OptOut: null,
SDKVersion: "2.11.15",
SourceMessageId: 'testSMID',
SessionId: "64102C03-592F-440D-8BCC-1D27AAA6B188",
SessionStartDate: 1603211322698,
Timestamp: 1603212299414,
UserAttributes: {},
UserIdentities: [],
IsFirstRun: true,
}

const batch = Converter.convertEvents(
'-8433569646818451201',
[sdkEvent],
window.mParticle.getInstance()
);

expect(batch).to.be.ok;
expect(batch.device_info.screen_height).to.equal(0);
expect(batch.device_info.screen_width).to.equal(0);

// set screen back on
window.screen = originalScreen;
done();
});
});

0 comments on commit 7ce742f

Please sign in to comment.