Skip to content
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
6 changes: 6 additions & 0 deletions src/Rokt-Kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,12 @@ var constructor = function () {

function setUserAttribute(key, value) {
self.userAttributes[key] = value;
_sendEventStream(
_buildIdentityEvent(
'set_user_attributes',
self.filters.filteredUser || {}
)
);
}

function removeUserAttribute(key) {
Expand Down
122 changes: 122 additions & 0 deletions test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3111,6 +3111,128 @@ describe('Rokt Forwarder', () => {
'test-attribute': 'test-value',
});
});

describe('event stream', () => {
beforeEach(() => {
window.mParticle.forwarder.eventStreamQueue = [];
window.mParticle.sessionManager = {
getSession: function () {
return 'test-mp-session-id';
},
};
});

afterEach(() => {
delete window.Rokt.__event_stream__;
window.mParticle.forwarder.eventStreamQueue = [];
});

it('should send a set_user_attributes event to window.Rokt.__event_stream__', () => {
var receivedEvents = [];
window.Rokt.__event_stream__ = function (event) {
receivedEvents.push(event);
};

window.mParticle.forwarder.setUserAttribute(
'test-attribute',
'test-value'
);

receivedEvents.length.should.equal(1);
receivedEvents[0].EventName.should.equal('set_user_attributes');
receivedEvents[0].EventDataType.should.equal(14);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this actually be 14? (Did someone decide that and I'm just unaware)
https://github.com/mParticle/mparticle-web-sdk/blob/master/src/types.ts#L16-L20
when there's a user attribute change, MP server expects 17
when there's a user identity change, MP server expects 18. I actually don't know when "Profile" or 14 is used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I discussed with Alex when we were implementing login, logout, identify, and modify_user events, and we decided to go with Profile (14). I assumed we'd want to keep it consistent for this event as well, but if theres a specific type for UserAttributeChange (17) then that probably fits better.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, this isn't a user attribute change, but a pseudo "new" event, so Profile better aligns with the expected behavior, like we did with the Identity methods.

});

it('should include updated UserAttributes in the event', () => {
var receivedEvents = [];
window.Rokt.__event_stream__ = function (event) {
receivedEvents.push(event);
};

window.mParticle.forwarder.setUserAttribute(
'new-attr',
'new-value'
);

receivedEvents[0].UserAttributes.should.deepEqual({
'new-attr': 'new-value',
});
});

it('should include MPID from filteredUser and SessionId from sessionManager', () => {
var receivedEvents = [];
window.Rokt.__event_stream__ = function (event) {
receivedEvents.push(event);
};

window.mParticle.forwarder.filters.filteredUser = {
getMPID: function () {
return 'attr-mpid-123';
},
getUserIdentities: function () {
return {
userIdentities: { email: 'user@example.com' },
};
},
};

window.mParticle.forwarder.setUserAttribute('k', 'v');

receivedEvents[0].MPID.should.equal('attr-mpid-123');
receivedEvents[0].SessionId.should.equal('test-mp-session-id');
receivedEvents[0].UserIdentities.should.deepEqual({
email: 'user@example.com',
});
});

it('should include null MPID and null UserIdentities when filteredUser is not set', () => {
var receivedEvents = [];
window.Rokt.__event_stream__ = function (event) {
receivedEvents.push(event);
};

window.mParticle.forwarder.filters.filteredUser = null;
window.mParticle.forwarder.setUserAttribute('k', 'v');

(receivedEvents[0].MPID === null).should.be.true();
(receivedEvents[0].UserIdentities === null).should.be.true();
});

it('should queue event when window.Rokt.__event_stream__ is not available', () => {
window.mParticle.forwarder.setUserAttribute(
'queued-attr',
'queued-value'
);

window.mParticle.forwarder.eventStreamQueue.length.should.equal(
1
);
window.mParticle.forwarder.eventStreamQueue[0].EventName.should.equal(
'set_user_attributes'
);
window.mParticle.forwarder.eventStreamQueue[0].EventDataType.should.equal(
14
);
});

it('should not throw when window.Rokt is undefined', () => {
var savedRokt = window.Rokt;
window.Rokt = undefined;

(function () {
window.mParticle.forwarder.setUserAttribute('k', 'v');
}).should.not.throw();

window.mParticle.forwarder.eventStreamQueue.length.should.equal(
1
);
window.mParticle.forwarder.eventStreamQueue[0].EventName.should.equal(
'set_user_attributes'
);

window.Rokt = savedRokt;
});
});
});

describe('#removeUserAttribute', () => {
Expand Down
Loading