Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Session Custom Attributes for Events #559

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ import { uuid } from './utils';
* 'OnDemand' // Stream Type (OnDemand, Live, etc.)
* true, // Log Page Event Toggle (true/false)
* true, // Log Media Event Toggle (true/false)
* { // (optional) Custom Attributes object used for each media event within the Media Session
* mediaSessionAttribute1: 'value1',
* mediaSessionAttribute2: 'value2'
* };
* )
* ```
*
Expand Down Expand Up @@ -130,6 +134,7 @@ export class MediaSession {
* @param streamType A descriptor for the type of stream, i.e. live or on demand
* @param logPageEvent A flag that toggles sending mParticle Events to Core SDK
* @param logMediaEvent A flag that toggles sending Media Events to Core SDK
* @param mediaSessionAttributes (optional) A set of custom attributes to attach to all media Events created by a Session
*/
constructor(
readonly mparticleInstance: MpSDKInstance,
Expand All @@ -140,6 +145,7 @@ export class MediaSession {
readonly streamType: MediaStreamType,
public logPageEvent = false,
public logMediaEvent = true,
public mediaSessionAttributes?: ModelAttributes,
) {
this.mediaSessionStartTimestamp = Date.now();
}
Expand All @@ -156,12 +162,17 @@ export class MediaSession {
// Set event option based on options or current state
this.currentPlayheadPosition =
options?.currentPlayheadPosition || this.currentPlayheadPosition;
this.customAttributes = options?.customAttributes || {};

// Merge Session Attributes with any other optional Event Attributes.
// Event-Level Custom Attributes will override Session Custom Attributes if there is a collison.
this.customAttributes = {
...this.mediaSessionAttributes,
...(options?.customAttributes || {}),
};

options = {
currentPlayheadPosition: this.currentPlayheadPosition,
customAttributes: this.customAttributes,
...options,
};

return new MediaEvent(
Expand Down Expand Up @@ -645,7 +656,8 @@ export class MediaSession {
* contentType = ContentType.Video
*
* logPageEvents = false //optional, defaults to false anyway
* logMediaEvents = false
* logMediaEvents = false //optional, defaults to false anyway
* sessionCustomEvents = {} //optional, defaults to empty object
* );
*
* const myCallback = (event: MediaEvent): void => {
Expand Down
141 changes: 122 additions & 19 deletions test/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('MediaSession', () => {

mpMedia.logAdBreakStart(adBreak, options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);

Expand Down Expand Up @@ -178,7 +178,7 @@ describe('MediaSession', () => {

mpMedia.logAdBreakEnd(options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('MediaSession', () => {

mpMedia.logAdStart(adContent, options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -374,7 +374,7 @@ describe('MediaSession', () => {

mpMedia.logAdEnd(options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -449,7 +449,7 @@ describe('MediaSession', () => {

mpMedia.logAdSkip(options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -516,7 +516,7 @@ describe('MediaSession', () => {

mpMedia.logAdClick(adContent, options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -558,7 +558,7 @@ describe('MediaSession', () => {

mpMedia.logBufferStart(320, 20, 201, options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -600,7 +600,7 @@ describe('MediaSession', () => {

mpMedia.logBufferEnd(99, 2, 341, options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -640,7 +640,7 @@ describe('MediaSession', () => {

mpMedia.logSeekStart(341, options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -680,7 +680,7 @@ describe('MediaSession', () => {

mpMedia.logSeekEnd(111, options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -741,7 +741,7 @@ describe('MediaSession', () => {

mpMedia.logMediaSessionStart(options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -780,7 +780,7 @@ describe('MediaSession', () => {

mpMedia.logMediaSessionEnd(options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -874,7 +874,7 @@ describe('MediaSession', () => {
};
mpMedia.logPlay(options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -912,7 +912,7 @@ describe('MediaSession', () => {
};
mpMedia.logPause(options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -950,7 +950,7 @@ describe('MediaSession', () => {
};
mpMedia.logMediaContentEnd(options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -1016,7 +1016,7 @@ describe('MediaSession', () => {
};
mpMedia.logSegmentStart(segment, options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -1079,7 +1079,7 @@ describe('MediaSession', () => {

mpMedia.logSegmentEnd(options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -1141,7 +1141,7 @@ describe('MediaSession', () => {
};
mpMedia.logSegmentSkip(options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
Expand Down Expand Up @@ -1236,7 +1236,7 @@ describe('MediaSession', () => {

mpMedia.logQoS(qos, options);

expect(bond.args[0][0].options.customAttributes).to.eq(
expect(bond.args[0][0].options.customAttributes).to.eqls(
options.customAttributes,
);

Expand Down Expand Up @@ -1270,4 +1270,107 @@ describe('MediaSession', () => {
});
});
});

describe('Custom Attributes', () => {
it('should include session attributes in Events when provided', () => {
const mediaSessionAttributes = {
session_name: 'amazing-current-session',
session_start_time: 'right-now',
custom_session_value: 'this-is-custom',
};

const customSession: MediaSession = new MediaSession(
mp,
song.contentId,
song.title,
song.duration,
song.contentType,
song.streamType,
false,
true,
mediaSessionAttributes,
);

const bond = sinon.spy(mp, 'logBaseEvent');

customSession.logMediaSessionStart();

expect(bond.called).to.eq(true);

expect(bond.args[0][0].options.customAttributes).to.eqls(
mediaSessionAttributes,
);
});

it('should allow Events to override Session Custom Attributes', () => {
const customSession: MediaSession = new MediaSession(
mp,
song.contentId,
song.title,
song.duration,
song.contentType,
song.streamType,
false,
true,
{
session_name: 'amazing-current-session',
session_start_time: 'right-now',
custom_session_value: 'this-is-custom',
},
);

const bond = sinon.spy(mp, 'logBaseEvent');

customSession.logMediaSessionStart({
customAttributes: {
custom_event_value: 'start-session',
},
});

customSession.logPlay({
customAttributes: {
custom_session_value: 'override-session-attributes',
},
});

customSession.logMediaSessionEnd();

expect(bond.called).to.eq(true);

const sessionStartEventAttrs =
bond.args[0][0].options.customAttributes;
const mediaPlayEventAttrs =
bond.args[1][0].options.customAttributes;
const sessionEndEventAttrs =
bond.args[2][0].options.customAttributes;

expect(
alexs-mparticle marked this conversation as resolved.
Show resolved Hide resolved
sessionStartEventAttrs,
'Session Start: Add New Event Custom Attribute',
).to.eqls({
custom_event_value: 'start-session',
session_name: 'amazing-current-session',
session_start_time: 'right-now',
custom_session_value: 'this-is-custom',
});

expect(
mediaPlayEventAttrs,
'Media Play: Override Session Attribute',
).to.eqls({
custom_session_value: 'override-session-attributes',
session_name: 'amazing-current-session',
session_start_time: 'right-now',
});

expect(
sessionEndEventAttrs,
'Session End: Session Attributes Only',
).to.eqls({
session_name: 'amazing-current-session',
session_start_time: 'right-now',
custom_session_value: 'this-is-custom',
});
});
});
});