Skip to content

Commit 0409a37

Browse files
committed
[REM-1773] Add mediaServiceUrl to js module, add tracking event for media impression view and tests
1 parent 0f32e6c commit 0409a37

File tree

4 files changed

+379
-0
lines changed

4 files changed

+379
-0
lines changed

spec/src/modules/tracker.js

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14539,4 +14539,307 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
1453914539
expect(tracker.trackProductInsightsAgentAnswerFeedback(requiredParameters)).to.equal(true);
1454014540
});
1454114541
});
14542+
14543+
describe('trackMediaImpressionView', () => {
14544+
const requiredParameters = {
14545+
bannerAdId: 'banner_ad_id',
14546+
placementId: 'placement_id',
14547+
};
14548+
14549+
const optionalParameters = {
14550+
analyticsTags: testAnalyticsTag,
14551+
};
14552+
14553+
it('Backwards Compatibility - Should respond with a valid response when snake cased parameters are provided', (done) => {
14554+
const { tracker } = new ConstructorIO({
14555+
apiKey: testApiKey,
14556+
fetch: fetchSpy,
14557+
mediaServiceUrl: 'https://behavior.media-cnstrc.com',
14558+
...requestQueueOptions,
14559+
});
14560+
14561+
const snakeCaseParameters = {
14562+
banner_ad_id: 'banner_ad_id',
14563+
placement_id: 'placement_id',
14564+
};
14565+
14566+
tracker.on('success', (responseParams) => {
14567+
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);
14568+
14569+
// Request
14570+
expect(fetchSpy).to.have.been.called;
14571+
expect(requestParams).to.have.property('key');
14572+
expect(requestParams).to.have.property('i');
14573+
expect(requestParams).to.have.property('s');
14574+
expect(requestParams).to.have.property('c').to.equal(clientVersion);
14575+
expect(requestParams).to.have.property('_dt');
14576+
expect(requestParams)
14577+
.to.have.property('banner_ad_id')
14578+
.to.equal(snakeCaseParameters.banner_ad_id);
14579+
expect(requestParams)
14580+
.to.have.property('placement_id')
14581+
.to.equal(snakeCaseParameters.placement_id);
14582+
validateOriginReferrer(requestParams);
14583+
14584+
// Response
14585+
expect(responseParams).to.have.property('method').to.equal('POST');
14586+
expect(responseParams).to.have.property('message');
14587+
14588+
done();
14589+
});
14590+
14591+
expect(tracker.trackMediaImpressionView(snakeCaseParameters)).to.equal(
14592+
true,
14593+
);
14594+
});
14595+
14596+
it('Should respond with a valid response when required parameters are provided', (done) => {
14597+
const { tracker } = new ConstructorIO({
14598+
apiKey: testApiKey,
14599+
fetch: fetchSpy,
14600+
mediaServiceUrl: 'https://behavior.media-cnstrc.com',
14601+
...requestQueueOptions,
14602+
});
14603+
14604+
tracker.on('success', (responseParams) => {
14605+
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);
14606+
// Request
14607+
expect(fetchSpy).to.have.been.called;
14608+
expect(requestParams).to.have.property('key');
14609+
expect(requestParams).to.have.property('i');
14610+
expect(requestParams).to.have.property('s');
14611+
expect(requestParams).to.have.property('c').to.equal(clientVersion);
14612+
expect(requestParams).to.have.property('_dt');
14613+
expect(requestParams)
14614+
.to.have.property('banner_ad_id')
14615+
.to.equal(requiredParameters.bannerAdId);
14616+
expect(requestParams)
14617+
.to.have.property('placement_id')
14618+
.to.equal(requiredParameters.placementId);
14619+
validateOriginReferrer(requestParams);
14620+
14621+
// Response
14622+
expect(responseParams).to.have.property('method').to.equal('POST');
14623+
expect(responseParams).to.have.property('message');
14624+
14625+
done();
14626+
});
14627+
14628+
expect(tracker.trackMediaImpressionView(requiredParameters)).to.equal(
14629+
true,
14630+
);
14631+
});
14632+
14633+
it('Should respond with a valid response when required and optional parameters are provided', (done) => {
14634+
const { tracker } = new ConstructorIO({
14635+
apiKey: testApiKey,
14636+
fetch: fetchSpy,
14637+
mediaServiceUrl: 'https://behavior.media-cnstrc.com',
14638+
...requestQueueOptions,
14639+
});
14640+
14641+
tracker.on('success', (responseParams) => {
14642+
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);
14643+
14644+
// Request
14645+
expect(fetchSpy).to.have.been.called;
14646+
expect(requestParams)
14647+
.to.have.property('analytics_tags')
14648+
.to.deep.equal(testAnalyticsTag);
14649+
14650+
// Response
14651+
expect(responseParams).to.have.property('method').to.equal('POST');
14652+
expect(responseParams).to.have.property('message');
14653+
14654+
done();
14655+
});
14656+
14657+
expect(
14658+
tracker.trackMediaImpressionView(
14659+
Object.assign(requiredParameters, optionalParameters),
14660+
),
14661+
).to.equal(true);
14662+
});
14663+
14664+
it('Should throw an error when invalid parameters are provided', () => {
14665+
const { tracker } = new ConstructorIO({ apiKey: testApiKey });
14666+
14667+
expect(tracker.trackMediaImpressionView([])).to.be.an('error');
14668+
});
14669+
14670+
it('Should throw an error when no parameters are provided', () => {
14671+
const { tracker } = new ConstructorIO({ apiKey: testApiKey });
14672+
14673+
expect(tracker.trackMediaImpressionView()).to.be.an('error');
14674+
});
14675+
14676+
it('Should send along origin_referrer query param if sendReferrerWithTrackingEvents is true', (done) => {
14677+
const { tracker } = new ConstructorIO({
14678+
apiKey: testApiKey,
14679+
fetch: fetchSpy,
14680+
sendReferrerWithTrackingEvents: true,
14681+
mediaServiceUrl: 'https://behavior.media-cnstrc.com',
14682+
...requestQueueOptions,
14683+
});
14684+
14685+
tracker.on('success', (responseParams) => {
14686+
const requestParams = helpers.extractUrlParamsFromFetch(fetchSpy);
14687+
14688+
// Request
14689+
expect(fetchSpy).to.have.been.called;
14690+
validateOriginReferrer(requestParams);
14691+
14692+
// Response
14693+
expect(responseParams).to.have.property('method').to.equal('POST');
14694+
expect(responseParams).to.have.property('message').to.equal('ok');
14695+
14696+
done();
14697+
});
14698+
14699+
expect(tracker.trackMediaImpressionView(requiredParameters)).to.equal(
14700+
true,
14701+
);
14702+
});
14703+
14704+
it('Should not send along origin_referrer query param if sendReferrerWithTrackingEvents is false', (done) => {
14705+
const { tracker } = new ConstructorIO({
14706+
apiKey: testApiKey,
14707+
fetch: fetchSpy,
14708+
sendReferrerWithTrackingEvents: false,
14709+
mediaServiceUrl: 'https://behavior.media-cnstrc.com',
14710+
...requestQueueOptions,
14711+
});
14712+
14713+
tracker.on('success', (responseParams) => {
14714+
const requestParams = helpers.extractUrlParamsFromFetch(fetchSpy);
14715+
14716+
// Request
14717+
expect(fetchSpy).to.have.been.called;
14718+
expect(requestParams).to.not.have.property('origin_referrer');
14719+
14720+
// Response
14721+
expect(responseParams).to.have.property('method').to.equal('POST');
14722+
expect(responseParams).to.have.property('message').to.equal('ok');
14723+
14724+
done();
14725+
});
14726+
14727+
expect(tracker.trackMediaImpressionView(requiredParameters)).to.equal(
14728+
true,
14729+
);
14730+
});
14731+
14732+
if (!skipNetworkTimeoutTests) {
14733+
it('Should be rejected when network request timeout is provided and reached', (done) => {
14734+
const { tracker } = new ConstructorIO({
14735+
apiKey: testApiKey,
14736+
mediaServiceUrl: 'https://behavior.media-cnstrc.com',
14737+
...requestQueueOptions,
14738+
});
14739+
14740+
tracker.on('error', ({ message }) => {
14741+
expect(message).to.equal(timeoutRejectionMessage);
14742+
done();
14743+
});
14744+
14745+
expect(
14746+
tracker.trackMediaImpressionView(requiredParameters, { timeout: 10 }),
14747+
).to.equal(true);
14748+
});
14749+
14750+
it('Should be rejected when global network request timeout is provided and reached', (done) => {
14751+
const { tracker } = new ConstructorIO({
14752+
apiKey: testApiKey,
14753+
mediaServiceUrl: 'https://behavior.media-cnstrc.com',
14754+
networkParameters: {
14755+
timeout: 20,
14756+
},
14757+
...requestQueueOptions,
14758+
});
14759+
14760+
tracker.on('error', ({ message }) => {
14761+
expect(message).to.equal(timeoutRejectionMessage);
14762+
done();
14763+
});
14764+
14765+
expect(tracker.trackMediaImpressionView(requiredParameters)).to.equal(
14766+
true,
14767+
);
14768+
});
14769+
}
14770+
14771+
it('Should not encode body parameters', (done) => {
14772+
const specialCharacters = '+[]&';
14773+
const userId = `user-id ${specialCharacters}`;
14774+
const bannerAdId = `banner_ad_id ${specialCharacters}`;
14775+
const { tracker } = new ConstructorIO({
14776+
apiKey: testApiKey,
14777+
userId,
14778+
fetch: fetchSpy,
14779+
mediaServiceUrl: 'https://behavior.media-cnstrc.com',
14780+
...requestQueueOptions,
14781+
});
14782+
14783+
tracker.on('success', (responseParams) => {
14784+
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);
14785+
14786+
// Request
14787+
expect(fetchSpy).to.have.been.called;
14788+
expect(requestParams).to.have.property('ui').to.equal(userId);
14789+
expect(requestParams)
14790+
.to.have.property('banner_ad_id')
14791+
.to.equal(bannerAdId);
14792+
14793+
// Response
14794+
expect(responseParams).to.have.property('method').to.equal('POST');
14795+
expect(responseParams).to.have.property('message').to.equal('ok');
14796+
14797+
done();
14798+
});
14799+
14800+
expect(
14801+
tracker.trackMediaImpressionView({ ...requiredParameters, bannerAdId }),
14802+
).to.equal(true);
14803+
});
14804+
14805+
it('Should properly transform non-breaking spaces in parameters', (done) => {
14806+
const breakingSpaces = '   ';
14807+
const userId = `user-id ${breakingSpaces} user-id`;
14808+
const bannerAdId = `banner_ad_id ${breakingSpaces} banner_ad_id`;
14809+
const bannerAdIdExpected = 'banner_ad_id banner_ad_id';
14810+
const userIdExpected = 'user-id user-id';
14811+
const { tracker } = new ConstructorIO({
14812+
apiKey: testApiKey,
14813+
userId,
14814+
mediaServiceUrl: 'https://behavior.media-cnstrc.com',
14815+
fetch: fetchSpy,
14816+
...requestQueueOptions,
14817+
});
14818+
14819+
tracker.on('success', (responseParams) => {
14820+
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);
14821+
14822+
// Request
14823+
expect(fetchSpy).to.have.been.called;
14824+
expect(requestParams).to.have.property('ui').to.equal(userIdExpected);
14825+
expect(requestParams)
14826+
.to.have.property('banner_ad_id')
14827+
.to.equal(bannerAdIdExpected);
14828+
14829+
// Response
14830+
expect(responseParams).to.have.property('method').to.equal('POST');
14831+
expect(responseParams).to.have.property('message').to.equal('ok');
14832+
14833+
done();
14834+
});
14835+
14836+
expect(
14837+
tracker.trackMediaImpressionView({
14838+
...requiredParameters,
14839+
userId,
14840+
bannerAdId,
14841+
}),
14842+
).to.equal(true);
14843+
});
14844+
});
1454214845
});

src/constructorio.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class ConstructorIO {
4040
* @param {string} [parameters.serviceUrl='https://ac.cnstrc.com'] - API URL endpoint
4141
* @param {string} [parameters.quizzesServiceUrl='https://quizzes.cnstrc.com'] - Quizzes API URL endpoint
4242
* @param {string} [parameters.agentServiceUrl='https://agent.cnstrc.com'] - AI Shopping Agent API URL endpoint
43+
* @param {string} [parameters.mediaServiceUrl='https://behavior.media-cnstrc.com'] - Media API URL endpoint
4344
* @param {string} [parameters.assistantServiceUrl='https://assistant.cnstrc.com'] - AI Shopping Assistant API URL endpoint @deprecated This parameter is deprecated and will be removed in a future version. Use parameters.agentServiceUrl instead.
4445
* @param {array} [parameters.segments] - User segments
4546
* @param {object} [parameters.testCells] - User test cells
@@ -74,6 +75,7 @@ class ConstructorIO {
7475
quizzesServiceUrl,
7576
agentServiceUrl,
7677
assistantServiceUrl,
78+
mediaServiceUrl,
7779
segments,
7880
testCells,
7981
clientId,
@@ -121,6 +123,7 @@ class ConstructorIO {
121123
quizzesServiceUrl: (quizzesServiceUrl && quizzesServiceUrl.replace(/\/$/, '')) || 'https://quizzes.cnstrc.com',
122124
agentServiceUrl: (agentServiceUrl && agentServiceUrl.replace(/\/$/, '')) || 'https://agent.cnstrc.com',
123125
assistantServiceUrl: (assistantServiceUrl && assistantServiceUrl.replace(/\/$/, '')) || 'https://assistant.cnstrc.com',
126+
mediaServiceUrl: (mediaServiceUrl && mediaServiceUrl.replace(/\/$/, '')) || 'https://behavior.media-cnstrc.com',
124127
sessionId: sessionId || session_id,
125128
clientId: clientId || client_id,
126129
userId,

0 commit comments

Comments
 (0)