diff --git a/spec/src/modules/tracker.js b/spec/src/modules/tracker.js index 836e2a44..f6555b13 100644 --- a/spec/src/modules/tracker.js +++ b/spec/src/modules/tracker.js @@ -1076,7 +1076,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackAutocompleteSelect(term, Object.assign(requiredParameters, optionalParameters))) + expect(tracker.trackAutocompleteSelect(term, { ...requiredParameters, ...optionalParameters })) .to.equal(true); }); @@ -1475,7 +1475,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackItemDetailLoad(Object.assign(requiredParameters, optionalParameters))) + expect(tracker.trackItemDetailLoad({ ...requiredParameters, ...optionalParameters })) .to.equal(true); }); @@ -1501,7 +1501,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackItemDetailLoad(Object.assign(requiredParameters, optionalParameters))) + expect(tracker.trackItemDetailLoad({ ...requiredParameters, ...optionalParameters })) .to.equal(true); }); @@ -1886,7 +1886,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackSearchSubmit(term, Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackSearchSubmit(term, { ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should respond with a valid response when term, required and optional parameters are provided', (done) => { @@ -1914,7 +1914,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackSearchSubmit(term, Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackSearchSubmit(term, { ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when invalid term is provided', () => { @@ -2473,7 +2473,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackSearchResultsLoaded(term, Object.assign(requiredParameters, optionalParameters))) + expect(tracker.trackSearchResultsLoaded(term, { ...requiredParameters, ...optionalParameters })) .to.equal(true); }); @@ -2784,6 +2784,87 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect(tracker.trackSearchResultsLoaded(term, parameters)).to.equal(true); }); + + it('Should return valid response when resultOffset is passed as an integer', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_offset').to.equal(1); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackSearchResultsLoaded(term, { + ...requiredParameters, + resultOffset: 1, + })).to.equal(true); + }); + + it('Should return valid response and omit result_offset when resultOffset is not provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('result_offset'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackSearchResultsLoaded(term, { + ...requiredParameters, + })).to.equal(true); + }); + + it('Should receive error from backend when both resultPage and resultOffset are passed', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('error', (error) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_page'); + expect(requestParams).to.have.property('result_offset'); + + // Response + expect(error).to.have.property('message').to.equal('Invalid parameters'); + + done(); + }); + + expect(tracker.trackSearchResultsLoaded(term, { + ...requiredParameters, + resultPage: 10, + resultOffset: 1, + })).to.equal(true); + }); }); describe('trackSearchResultClick', () => { @@ -2920,6 +3001,87 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect(tracker.trackSearchResultClickV2(term, { ...requiredParameters, ...optionalParameters, ...v2Parameters })).to.equal(true); }); + it('V2 Should return valid response when resultOffset is passed as an integer', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_offset').to.equal(1); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackSearchResultClickV2(term, { + ...requiredParameters, + resultOffset: 1, + })).to.equal(true); + }); + + it('V2 Should return valid response and omit result_offset when resultOffset is not provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('result_offset'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackSearchResultClickV2(term, { + ...requiredParameters, + })).to.equal(true); + }); + + it('V2 Should receive error from backend when both resultPage and resultOffset are passed', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('error', (error) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_page'); + expect(requestParams).to.have.property('result_offset'); + + // Response + expect(error).to.have.property('message').to.equal('Invalid parameters'); + + done(); + }); + + expect(tracker.trackSearchResultClickV2(term, { + ...requiredParameters, + resultPage: 10, + resultOffset: 1, + })).to.equal(true); + }); + it('Backwards Compatibility - Should respond with a valid response when term and snake cased parameters are provided', (done) => { const { tracker } = new ConstructorIO({ apiKey: testApiKey, @@ -4205,7 +4367,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackPurchase(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackPurchase({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should respond with a valid response when optional parameters are provided', (done) => { @@ -4232,7 +4394,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackPurchase(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackPurchase({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should respond with a valid response when required parameters and segments are provided', (done) => { @@ -4439,10 +4601,11 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { addOrderIdRecord({ orderId, apiKey }); - expect(tracker.trackPurchase(Object.assign(requiredParameters, { + expect(tracker.trackPurchase({ + ...requiredParameters, ...optionalParameters, orderId, - }))).to.equal(false); + })).to.equal(false); setTimeout(() => { // Request @@ -4479,10 +4642,11 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { addOrderIdRecord({ orderId: '239402919', apiKey: testApiKey }); addOrderIdRecord({ orderId: '482039192', apiKey: testApiKey }); - expect(tracker.trackPurchase(Object.assign(requiredParameters, { + expect(tracker.trackPurchase({ + ...requiredParameters, ...optionalParameters, orderId: '328192019', - }))).to.equal(true); + })).to.equal(true); }); it('Should throw an error when invalid parameters are provided', () => { @@ -4862,7 +5026,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackRecommendationView(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackRecommendationView({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when invalid parameters are provided', () => { @@ -5187,6 +5351,87 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect(tracker.trackRecommendationView(parameters)).to.equal(true); }); + + it('Should return valid response when resultOffset is passed as an integer', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_offset').to.equal(1); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackRecommendationView({ + ...requiredParameters, + resultOffset: 1, + })).to.equal(true); + }); + + it('Should return valid response and omit result_offset when resultOffset is not provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('result_offset'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackRecommendationView({ + ...requiredParameters, + })).to.equal(true); + }); + + it('Should receive error from backend when both resultPage and resultOffset are passed', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('error', (error) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_page'); + expect(requestParams).to.have.property('result_offset'); + + // Response + expect(error).to.have.property('message').to.equal('Invalid parameters'); + + done(); + }); + + expect(tracker.trackRecommendationView({ + ...requiredParameters, + resultPage: 10, + resultOffset: 1, + })).to.equal(true); + }); }); describe('trackRecommendationClick', () => { @@ -5516,7 +5761,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackRecommendationClick(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackRecommendationClick({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when invalid parameters are provided', () => { @@ -5783,6 +6028,87 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); expect(tracker.trackRecommendationClick(parametersWithSeedItemIds)).to.equal(true); }); + + it('Should return valid response when resultOffset is passed as an integer', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_offset').to.equal(1); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackRecommendationClick({ + ...requiredParameters, + resultOffset: 1, + })).to.equal(true); + }); + + it('Should return valid response and omit result_offset when resultOffset is not provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('result_offset'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackRecommendationClick({ + ...requiredParameters, + })).to.equal(true); + }); + + it('Should receive error from backend when both resultPage and resultOffset are passed', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('error', (error) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_page'); + expect(requestParams).to.have.property('result_offset'); + + // Response + expect(error).to.have.property('message').to.equal('Invalid parameters'); + + done(); + }); + + expect(tracker.trackRecommendationClick({ + ...requiredParameters, + resultPage: 10, + resultOffset: 1, + })).to.equal(true); + }); }); describe('trackBrowseResultsLoaded', () => { @@ -6061,7 +6387,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackBrowseResultsLoaded(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackBrowseResultsLoaded({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should send all items when more than 100 items are provided', (done) => { @@ -6271,6 +6597,88 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect(tracker.trackBrowseResultsLoaded(parameters)).to.equal(true); }); + + it('Should return valid response when resultOffset is passed as an integer', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_offset').to.equal(1); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackBrowseResultsLoaded({ + ...requiredParameters, + resultOffset: 1, + })).to.equal(true); + }); + + it('Should return valid response and omit result_offset when resultOffset is not provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('result_offset'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackBrowseResultsLoaded({ + ...requiredParameters, + })).to.equal(true); + }); + + it('Should receive error from backend when both resultPage and resultOffset are passed', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('error', (error) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_page'); + expect(requestParams).to.have.property('result_offset'); + + // Response + expect(error).to.have.property('message').to.equal('Invalid parameters'); + + done(); + }); + + expect(tracker.trackBrowseResultsLoaded({ + ...requiredParameters, + ...optionalParameters, + resultPage: 10, + resultOffset: 1, + })).to.equal(true); + }); }); describe('trackBrowseRedirect', () => { @@ -6474,7 +6882,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackBrowseRedirect(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackBrowseRedirect({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when invalid parameters are provided', () => { @@ -6894,7 +7302,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackBrowseResultClick(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackBrowseResultClick({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should respond with a valid response when required parameters and non-existent item id are provided', (done) => { @@ -7025,14 +7433,66 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect(tracker.trackBrowseResultClick(requiredParameters)).to.equal(true); }); - } + } + + it('Should not encode body parameters', (done) => { + const specialCharacters = '+[]&'; + const userId = `user-id ${specialCharacters}`; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackBrowseResultClick(requiredParameters)).to.equal(true); + }); + + it('Should properly transform non-breaking spaces in parameters', (done) => { + const breakingSpaces = '   '; + const userId = `user-id ${breakingSpaces} user-id`; + const userIdExpected = 'user-id user-id'; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userIdExpected); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); - it('Should not encode body parameters', (done) => { - const specialCharacters = '+[]&'; - const userId = `user-id ${specialCharacters}`; + expect(tracker.trackBrowseResultClick(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when legacy parameters are provided', (done) => { const { tracker } = new ConstructorIO({ apiKey: testApiKey, - userId, fetch: fetchSpy, ...requestQueueOptions, }); @@ -7042,7 +7502,8 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { // Request expect(fetchSpy).to.have.been.called; - expect(requestParams).to.have.property('ui').to.equal(userId); + expect(requestParams).to.have.property('item_id').to.equal(legacyParameters.customerId); + expect(requestParams).to.have.property('item_name').to.equal(legacyParameters.name); // Response expect(responseParams).to.have.property('method').to.equal('POST'); @@ -7051,16 +7512,12 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackBrowseResultClick(requiredParameters)).to.equal(true); + expect(tracker.trackBrowseResultClick(legacyParameters)).to.equal(true); }); - it('Should properly transform non-breaking spaces in parameters', (done) => { - const breakingSpaces = '   '; - const userId = `user-id ${breakingSpaces} user-id`; - const userIdExpected = 'user-id user-id'; + it('Should return valid response when resultOffset is passed as an integer', (done) => { const { tracker } = new ConstructorIO({ apiKey: testApiKey, - userId, fetch: fetchSpy, ...requestQueueOptions, }); @@ -7070,19 +7527,22 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { // Request expect(fetchSpy).to.have.been.called; - expect(requestParams).to.have.property('ui').to.equal(userIdExpected); + expect(requestParams).to.have.property('result_offset').to.equal(1); // Response expect(responseParams).to.have.property('method').to.equal('POST'); - expect(responseParams).to.have.property('message').to.equal('ok'); + expect(responseParams).to.have.property('message'); done(); }); - expect(tracker.trackBrowseResultClick(requiredParameters)).to.equal(true); + expect(tracker.trackBrowseResultClick({ + ...requiredParameters, + resultOffset: 1, + })).to.equal(true); }); - it('Should respond with a valid response when legacy parameters are provided', (done) => { + it('Should return valid response and omit result_offset when resultOffset is not provided', (done) => { const { tracker } = new ConstructorIO({ apiKey: testApiKey, fetch: fetchSpy, @@ -7094,17 +7554,46 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { // Request expect(fetchSpy).to.have.been.called; - expect(requestParams).to.have.property('item_id').to.equal(legacyParameters.customerId); - expect(requestParams).to.have.property('item_name').to.equal(legacyParameters.name); + expect(requestParams).to.not.have.property('result_offset'); // Response expect(responseParams).to.have.property('method').to.equal('POST'); - expect(responseParams).to.have.property('message').to.equal('ok'); + expect(responseParams).to.have.property('message'); done(); }); - expect(tracker.trackBrowseResultClick(legacyParameters)).to.equal(true); + expect(tracker.trackBrowseResultClick({ + ...requiredParameters, + })).to.equal(true); + }); + + it('Should receive error from backend when both resultPage and resultOffset are passed', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('error', (error) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_page'); + expect(requestParams).to.have.property('result_offset'); + + // Response + expect(error).to.have.property('message').to.equal('Invalid parameters'); + + done(); + }); + + expect(tracker.trackBrowseResultClick({ + ...requiredParameters, + resultPage: 10, + resultOffset: 1, + })).to.equal(true); }); }); @@ -7328,7 +7817,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { // Request expect(fetchSpy).to.have.been.called; - expect(requestParams).to.have.property('section').to.equal(requiredParameters.section); + expect(requestParams).to.have.property('section').to.equal(optionalParameters.section); expect(requestParams).to.have.property('item_id').to.equal(requiredParameters.itemId); expect(requestParams).to.have.property('item_name').to.equal(optionalParameters.itemName); expect(requestParams).to.have.property('analytics_tags').to.deep.equal(testAnalyticsTag); @@ -7340,7 +7829,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackGenericResultClick(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackGenericResultClick({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should respond with a valid response when required parameters and non-existent item id are provided', (done) => { @@ -7523,6 +8012,59 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect(tracker.trackGenericResultClick(requiredParameters)).to.equal(true); }); + + it('Should return valid response when resultOffset is passed as an integer', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_offset').to.equal(1); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackGenericResultClick({ + ...requiredParameters, + resultOffset: 1, + })).to.equal(true); + }); + + it('Should return valid response and omit result_offset when resultOffset is not provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('result_offset'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackGenericResultClick({ + ...requiredParameters, + })).to.equal(true); + }); }); describe('trackQuizResultsLoaded', () => { @@ -7793,7 +8335,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackQuizResultsLoaded(Object.assign(requiredParameters, optionalParameters))) + expect(tracker.trackQuizResultsLoaded({ ...requiredParameters, ...optionalParameters })) .to.equal(true); }); @@ -8046,6 +8588,100 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect(tracker.trackQuizResultsLoaded(parameters)).to.equal(true); }); + + it('Should return valid response when resultOffset is passed as an integer', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_offset').to.equal(1); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackQuizResultsLoaded({ + ...requiredParameters, + resultOffset: 1, + })).to.equal(true); + }); + + it('Should return valid response and omit result_offset when resultOffset is not provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('result_offset'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackQuizResultsLoaded({ + ...requiredParameters, + })).to.equal(true); + }); + + it('Should return an error when resultOffset is not a number', () => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + expect(tracker.trackQuizResultsLoaded({ + ...requiredParameters, + resultOffset: 'not-a-number', + })).to.be.an('error'); + }); + + it('Should receive error from backend when both resultPage and resultOffset are passed', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('error', (error) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_page'); + expect(requestParams).to.have.property('result_offset'); + + // Response + expect(error).to.have.property('message').to.equal('Invalid parameters'); + + done(); + }); + + expect(tracker.trackQuizResultsLoaded({ + ...requiredParameters, + resultPage: 10, + resultOffset: 1, + })).to.equal(true); + }); }); describe('trackQuizResultClick', () => { @@ -8268,7 +8904,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackQuizResultClick(Object.assign(requiredParameters, optionalParameters))) + expect(tracker.trackQuizResultClick({ ...requiredParameters, ...optionalParameters })) .to.equal(true); }); @@ -8511,6 +9147,100 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect(tracker.trackQuizResultClick(requiredParameters)).to.equal(true); }); + + it('Should return valid response when resultOffset is passed as an integer', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_offset').to.equal(1); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackQuizResultClick({ + ...requiredParameters, + resultOffset: 1, + })).to.equal(true); + }); + + it('Should return valid response and omit result_offset when resultOffset is not provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('result_offset'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackQuizResultClick({ + ...requiredParameters, + })).to.equal(true); + }); + + it('Should return an error when resultOffset is not a number', () => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + expect(tracker.trackQuizResultClick({ + ...requiredParameters, + resultOffset: 'not-a-number', + })).to.be.an('error'); + }); + + it('Should receive error from backend when both resultPage and resultOffset are passed', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('error', (error) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('result_page'); + expect(requestParams).to.have.property('result_offset'); + + // Response + expect(error).to.have.property('message').to.equal('Invalid parameters'); + + done(); + }); + + expect(tracker.trackQuizResultClick({ + ...requiredParameters, + resultPage: 10, + resultOffset: 1, + })).to.equal(true); + }); }); describe('trackQuizConversion', () => { @@ -8732,7 +9462,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackQuizConversion(Object.assign(requiredParameters, optionalParameters))) + expect(tracker.trackQuizConversion({ ...requiredParameters, ...optionalParameters })) .to.equal(true); }); @@ -9216,7 +9946,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackAgentSubmit(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackAgentSubmit({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when no parameters are provided', () => { @@ -9533,7 +10263,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); // eslint-disable-next-line max-len - expect(tracker.trackAgentResultLoadStarted(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackAgentResultLoadStarted({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when no parameters are provided', () => { @@ -9851,7 +10581,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); // eslint-disable-next-line max-len - expect(tracker.trackAgentResultLoadFinished(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackAgentResultLoadFinished({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when no parameters are provided', () => { @@ -10177,7 +10907,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackAgentResultClick(Object.assign(requiredParameters, optionalParameters))) + expect(tracker.trackAgentResultClick({ ...requiredParameters, ...optionalParameters })) .to.equal(true); }); @@ -10521,7 +11251,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackAgentResultView(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackAgentResultView({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when invalid parameters are provided', () => { @@ -10839,7 +11569,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackAgentSearchSubmit(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackAgentSearchSubmit({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when no parameters are provided', () => { @@ -11153,7 +11883,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackAssistantSubmit(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackAssistantSubmit({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when no parameters are provided', () => { @@ -11470,7 +12200,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); // eslint-disable-next-line max-len - expect(tracker.trackAssistantResultLoadStarted(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackAssistantResultLoadStarted({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when no parameters are provided', () => { @@ -11788,7 +12518,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); // eslint-disable-next-line max-len - expect(tracker.trackAssistantResultLoadFinished(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackAssistantResultLoadFinished({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when no parameters are provided', () => { @@ -12114,7 +12844,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackAssistantResultClick(Object.assign(requiredParameters, optionalParameters))) + expect(tracker.trackAssistantResultClick({ ...requiredParameters, ...optionalParameters })) .to.equal(true); }); @@ -12458,7 +13188,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackAssistantResultView(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackAssistantResultView({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when invalid parameters are provided', () => { @@ -12776,7 +13506,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { done(); }); - expect(tracker.trackAssistantSearchSubmit(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + expect(tracker.trackAssistantSearchSubmit({ ...requiredParameters, ...optionalParameters })).to.equal(true); }); it('Should throw an error when no parameters are provided', () => { @@ -13115,7 +13845,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); expect(tracker.trackProductInsightsAgentViews( - Object.assign(requiredParameters, optionalParameters), + { ...requiredParameters, ...optionalParameters }, )).to.equal(true); }); @@ -13444,7 +14174,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); expect(tracker.trackProductInsightsAgentView( - Object.assign(requiredParameters, optionalParameters), + { ...requiredParameters, ...optionalParameters }, )).to.equal(true); }); @@ -13766,7 +14496,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); expect(tracker.trackProductInsightsAgentOutOfView( - Object.assign(requiredParameters, optionalParameters), + { ...requiredParameters, ...optionalParameters }, )).to.equal(true); }); @@ -14086,7 +14816,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); expect(tracker.trackProductInsightsAgentFocus( - Object.assign(requiredParameters, optionalParameters), + { ...requiredParameters, ...optionalParameters }, )).to.equal(true); }); @@ -14407,7 +15137,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); expect(tracker.trackProductInsightsAgentQuestionClick( - Object.assign(requiredParameters, optionalParameters), + { ...requiredParameters, ...optionalParameters }, )).to.equal(true); }); @@ -14728,7 +15458,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); expect(tracker.trackProductInsightsAgentQuestionSubmit( - Object.assign(requiredParameters, optionalParameters), + { ...requiredParameters, ...optionalParameters }, )).to.equal(true); }); @@ -15052,7 +15782,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); expect(tracker.trackProductInsightsAgentAnswerView( - Object.assign(requiredParameters, optionalParameters), + { ...requiredParameters, ...optionalParameters }, )).to.equal(true); }); @@ -15374,7 +16104,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); expect(tracker.trackProductInsightsAgentAnswerFeedback( - Object.assign(requiredParameters, optionalParameters), + { ...requiredParameters, ...optionalParameters }, )).to.equal(true); }); @@ -15611,7 +16341,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect( tracker.trackMediaImpressionView( - Object.assign(requiredParameters, optionalParameters), + { ...requiredParameters, ...optionalParameters }, ), ).to.equal(true); }); diff --git a/src/modules/tracker.js b/src/modules/tracker.js index ce95e8a5..1475a618 100644 --- a/src/modules/tracker.js +++ b/src/modules/tracker.js @@ -676,6 +676,7 @@ class Tracker { * @param {object[]} parameters.items - List of product item unique identifiers in search results listing * @param {number} [parameters.resultCount] - Total number of results * @param {number} [parameters.resultPage] - Current page of search results + * @param {number} [parameters.resultOffset] - Current offset of results, used on scrolling sites. Cannot be used with `resultPage` * @param {string} [parameters.resultId] - Browse result identifier (returned in response from Constructor) * @param {object} [parameters.selectedFilters] - Selected filters * @param {string} [parameters.sortOrder] - Sort order ('ascending' or 'descending') @@ -716,6 +717,8 @@ class Tracker { items = customer_ids || item_ids, result_page, resultPage = result_page, + result_offset, + resultOffset = result_offset, result_id, resultId = result_id, sort_order, @@ -749,6 +752,7 @@ class Tracker { result_count: resultCount, items: transformedItems, result_page: resultPage, + result_offset: resultOffset, result_id: resultId, sort_order: sortOrder, sort_by: sortBy, @@ -796,6 +800,7 @@ class Tracker { * @param {string} [parameters.resultId] - Search result identifier (returned in response from Constructor) * @param {number} [parameters.resultCount] - Number of results in total * @param {number} [parameters.resultPage] - Current page of results + * @param {number} [parameters.resultOffset] - Current offset of results, used on scrolling sites. Cannot be used with `resultPage` * @param {string} [parameters.resultPositionOnPage] - Position of selected items on page * @param {string} [parameters.numResultsPerPage] - Number of results per page * @param {object} [parameters.selectedFilters] - Key - Value map of selected filters @@ -841,6 +846,8 @@ class Tracker { resultCount = num_results || result_count, result_page, resultPage = result_page, + result_offset, + resultOffset = result_offset, result_position_on_page, resultPositionOnPage = result_position_on_page, num_results_per_page, @@ -861,6 +868,7 @@ class Tracker { result_id: resultId, result_count: resultCount, result_page: resultPage, + result_offset: resultOffset, result_position_on_page: resultPositionOnPage, num_results_per_page: numResultsPerPage, selected_filters: selectedFilters, @@ -1240,6 +1248,7 @@ class Tracker { * @param {object[]} [parameters.items] - List of Product Item Objects * @param {number} [parameters.resultCount] - Total number of results * @param {number} [parameters.resultPage] - Page number of results + * @param {number} [parameters.resultOffset] - Current offset of results, used on scrolling sites. Cannot be used with `resultPage` * @param {string} [parameters.resultId] - Recommendation result identifier (returned in response from Constructor) * @param {string} [parameters.section="Products"] - Results section * @param {object} [parameters.analyticsTags] - Pass additional analytics data @@ -1270,6 +1279,8 @@ class Tracker { result_count, result_page, resultPage = result_page, + result_offset, + resultOffset = result_offset, result_id, resultId = result_id, section, @@ -1292,6 +1303,10 @@ class Tracker { bodyParams.result_page = resultPage; } + if (!helpers.isNil(resultOffset)) { + bodyParams.result_offset = resultOffset; + } + if (resultId) { bodyParams.result_id = resultId; } @@ -1496,6 +1511,7 @@ class Tracker { * @param {string} [parameters.resultId] - Recommendation result identifier (returned in response from Constructor) * @param {number} [parameters.resultCount] - Total number of results * @param {number} [parameters.resultPage] - Page number of results + * @param {number} [parameters.resultOffset] - Current offset of results, used on scrolling sites. Cannot be used with `resultPage` * @param {number} [parameters.resultPositionOnPage] - Position of result on page * @param {number} [parameters.numResultsPerPage] - Number of results on page * @param {object} [parameters.analyticsTags] - Pass additional analytics data @@ -1538,6 +1554,8 @@ class Tracker { resultCount = result_count, result_page, resultPage = result_page, + result_offset, + resultOffset = result_offset, result_position_on_page, resultPositionOnPage = result_position_on_page, num_results_per_page, @@ -1576,6 +1594,10 @@ class Tracker { bodyParams.result_page = resultPage; } + if (!helpers.isNil(resultOffset)) { + bodyParams.result_offset = resultOffset; + } + if (!helpers.isNil(resultPositionOnPage)) { bodyParams.result_position_on_page = resultPositionOnPage; } @@ -1652,6 +1674,7 @@ class Tracker { * @param {string} [parameters.section="Products"] - Index section * @param {number} [parameters.resultCount] - Total number of results * @param {number} [parameters.resultPage] - Page number of results + * @param {number} [parameters.resultOffset] - Current offset of results, used on scrolling sites. Cannot be used with `resultPage` * @param {string} [parameters.resultId] - Browse result identifier (returned in response from Constructor) * @param {object} [parameters.selectedFilters] - Selected filters * @param {string} [parameters.sortOrder] - Sort order ('ascending' or 'descending') @@ -1687,6 +1710,8 @@ class Tracker { result_count, result_page, resultPage = result_page, + result_offset, + resultOffset = result_offset, result_id, resultId = result_id, selected_filters, @@ -1717,6 +1742,10 @@ class Tracker { bodyParams.result_page = resultPage; } + if (!helpers.isNil(resultOffset)) { + bodyParams.result_offset = resultOffset; + } + if (resultId) { bodyParams.result_id = resultId; } @@ -1787,6 +1816,7 @@ class Tracker { * @param {string} [parameters.resultId] - Browse result identifier (returned in response from Constructor) * @param {number} [parameters.resultCount] - Total number of results * @param {number} [parameters.resultPage] - Page number of results + * @param {number} [parameters.resultOffset] - Current offset of results, used on scrolling sites. Cannot be used with `resultPage` * @param {number} [parameters.resultPositionOnPage] - Position of clicked item * @param {number} [parameters.numResultsPerPage] - Number of results shown * @param {object} [parameters.selectedFilters] - Selected filters @@ -1830,6 +1860,8 @@ class Tracker { resultCount = result_count, result_page, resultPage = result_page, + result_offset, + resultOffset = result_offset, result_position_on_page, resultPositionOnPage = result_position_on_page, num_results_per_page, @@ -1872,6 +1904,10 @@ class Tracker { bodyParams.result_page = resultPage; } + if (!helpers.isNil(resultOffset)) { + bodyParams.result_offset = resultOffset; + } + if (!helpers.isNil(resultPositionOnPage)) { bodyParams.result_position_on_page = resultPositionOnPage; } @@ -2051,6 +2087,7 @@ class Tracker { * @param {string} [parameters.itemName] - Product item name * @param {string} [parameters.variationId] - Product item variation unique identifier * @param {string} [parameters.section="Products"] - Index section + * @param {number} [parameters.resultOffset] - Current offset of results, used on scrolling sites * @param {object} [parameters.analyticsTags] - Pass additional analytics data * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) @@ -2078,6 +2115,8 @@ class Tracker { variation_id, variationId = variation_id, section = 'Products', + result_offset, + resultOffset = result_offset, analyticsTags, } = parameters; if (itemId) { @@ -2092,6 +2131,10 @@ class Tracker { bodyParams.variation_id = variationId; } + if (!helpers.isNil(resultOffset)) { + bodyParams.result_offset = resultOffset; + } + if (analyticsTags) { bodyParams.analytics_tags = analyticsTags; } @@ -2130,6 +2173,7 @@ class Tracker { * @param {string} [parameters.section='Products'] - Index section * @param {number} [parameters.resultCount] - Total number of results * @param {number} [parameters.resultPage] - The page of the results + * @param {number} [parameters.resultOffset] - Current offset of results, used on scrolling sites. Cannot be used with `resultPage` * @param {string} [parameters.resultId] - Quiz result identifier (returned in response from Constructor) * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) @@ -2167,6 +2211,8 @@ class Tracker { resultId = result_id, result_page, resultPage = result_page, + result_offset, + resultOffset = result_offset, items, } = parameters; const queryParams = {}; @@ -2226,6 +2272,13 @@ class Tracker { bodyParams.result_page = resultPage; } + if (!helpers.isNil(resultOffset)) { + if (typeof resultOffset !== 'number') { + return new Error('"resultOffset" must be a number'); + } + bodyParams.result_offset = resultOffset; + } + const requestURL = `${requestPath}${applyParamsAsString(queryParams, this.options)}`; const requestMethod = 'POST'; const requestBody = applyParams(bodyParams, { ...this.options, requestMethod }); @@ -2260,6 +2313,7 @@ class Tracker { * @param {string} [parameters.section='Products'] - Index section * @param {number} [parameters.resultCount] - Total number of results * @param {number} [parameters.resultPage] - The page of the results + * @param {number} [parameters.resultOffset] - Current offset of results, used on scrolling sites. Cannot be used with `resultPage` * @param {string} [parameters.resultId] - Quiz result identifier (returned in response from Constructor) * @param {number} [parameters.resultPositionOnPage] - Position of clicked item * @param {number} [parameters.numResultsPerPage] - Number of results shown @@ -2301,6 +2355,8 @@ class Tracker { resultId = result_id, result_page, resultPage = result_page, + result_offset, + resultOffset = result_offset, num_results_per_page, numResultsPerPage = num_results_per_page, result_position_on_page, @@ -2381,6 +2437,13 @@ class Tracker { bodyParams.result_page = resultPage; } + if (!helpers.isNil(resultOffset)) { + if (typeof resultOffset !== 'number') { + return new Error('"resultOffset" must be a number'); + } + bodyParams.result_offset = resultOffset; + } + if (!helpers.isNil(numResultsPerPage)) { if (typeof numResultsPerPage !== 'number') { return new Error('"numResultsPerPage" must be a number'); diff --git a/src/types/tracker.d.ts b/src/types/tracker.d.ts index e04cfb85..6e71b7b4 100644 --- a/src/types/tracker.d.ts +++ b/src/types/tracker.d.ts @@ -1,5 +1,5 @@ +import { ConstructorClientOptions, ItemTracked, ItemTrackedPurchase, NetworkParameters, Question, TimeSpan } from '.'; import { EventEmitter } from './events'; -import { ConstructorClientOptions, ItemTracked, ItemTrackedPurchase, Question, TimeSpan, NetworkParameters } from '.'; import RequestQueue from './request-queue'; export default Tracker; @@ -81,6 +81,7 @@ declare class Tracker { items: ItemTracked[]; resultCount?: number; resultPage?: number; + resultOffset?: number; resultId?: string; selectedFilters?: Record; sortOrder?: string; @@ -143,6 +144,7 @@ declare class Tracker { items?: ItemTracked[]; resultCount?: number; resultPage?: number; + resultOffset?: number; resultId?: string; section?: string; analyticsTags?: Record; @@ -161,6 +163,7 @@ declare class Tracker { resultId?: string; resultCount?: number; resultPage?: number; + resultOffset?: number; resultPositionOnPage?: number; numResultsPerPage?: number; analyticsTags?: Record; @@ -179,6 +182,7 @@ declare class Tracker { section?: string; resultCount?: number; resultPage?: number; + resultOffset?: number; resultId?: string; selectedFilters?: object; sortOrder?: string; @@ -200,6 +204,7 @@ declare class Tracker { resultId?: string; resultCount?: number; resultPage?: number; + resultOffset?: number; resultPositionOnPage?: number; numResultsPerPage?: number; selectedFilters?: object; @@ -216,6 +221,7 @@ declare class Tracker { itemName?: string; variationId?: string; section?: string; + resultOffset?: number; analyticsTags?: Record; }, networkParameters?: NetworkParameters @@ -230,6 +236,7 @@ declare class Tracker { section?: string; resultCount?: number; resultPage?: number; + resultOffset?: number; resultId?: string; items: ItemTracked[]; }, @@ -247,6 +254,7 @@ declare class Tracker { section?: string; resultCount?: number; resultPage?: number; + resultOffset?: number; resultId?: string; resultPositionOnPage?: number; numResultsPerPage?: number;