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