diff --git a/spec/src/modules/tracker.js b/spec/src/modules/tracker.js index 955bee1e..15f43423 100644 --- a/spec/src/modules/tracker.js +++ b/spec/src/modules/tracker.js @@ -34,6 +34,11 @@ function validateOriginReferrer(requestParams) { expect(requestParams).to.have.property('origin_referrer').to.contain('utm_campaign=campaign_1'); } +function createLongUrl(length) { + const baseUrl = 'https://constructor.io/product/KMH876?a='; + return `${baseUrl}${'a'.repeat(length - baseUrl.length)}`; +} + describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { let fetchSpy = null; let cleanup; @@ -1465,6 +1470,34 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect(tracker.trackItemDetailLoad(requiredParameters)).to.equal(true); }); } + + it('Should truncate url param to 2048 characters max', (done) => { + const longUrl = createLongUrl(3000); + const truncatedUrl = longUrl.slice(0, 2048); + + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', () => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams.url).to.equal(truncatedUrl); + + done(); + }); + + const parameters = { + ...requiredParameters, + url: longUrl, + }; + + expect(tracker.trackItemDetailLoad(parameters)).to.equal(true); + }); }); describe('trackSearchSubmit', () => { @@ -2566,6 +2599,34 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect(tracker.trackSearchResultsLoaded(term, requiredParameters)).to.equal(true); }); + + it('Should truncate url param to 2048 characters max', (done) => { + const longUrl = createLongUrl(3000); + const truncatedUrl = longUrl.slice(0, 2048); + + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', () => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams.url).to.equal(truncatedUrl); + + done(); + }); + + const parameters = { + ...requiredParameters, + url: longUrl, + }; + + expect(tracker.trackSearchResultsLoaded(term, parameters)).to.equal(true); + }); }); describe('trackSearchResultClick', () => { @@ -4805,6 +4866,34 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect(tracker.trackRecommendationView(requiredParamsWithSeedItemIds)).to.equal(true); }); + + it('Should truncate url param to 2048 characters max', (done) => { + const longUrl = createLongUrl(3000); + const truncatedUrl = longUrl.slice(0, 2048); + + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', () => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams.url).to.equal(truncatedUrl); + + done(); + }); + + const parameters = { + ...requiredParameters, + url: longUrl, + }; + + expect(tracker.trackRecommendationView(parameters)).to.equal(true); + }); }); describe('trackRecommendationClick', () => { @@ -5696,6 +5785,34 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect(tracker.trackBrowseResultsLoaded(requiredParameters)).to.equal(true); }); + + it('Should truncate url param to 2048 characters max', (done) => { + const longUrl = createLongUrl(3000); + const truncatedUrl = longUrl.slice(0, 2048); + + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', () => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams.url).to.equal(truncatedUrl); + + done(); + }); + + const parameters = { + ...requiredParameters, + url: longUrl, + }; + + expect(tracker.trackBrowseResultsLoaded(parameters)).to.equal(true); + }); }); describe('trackBrowseRedirect', () => { @@ -7343,6 +7460,34 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { expect(tracker.trackQuizResultsLoaded(requiredParameters)).to.equal(true); }); + + it('Should truncate url param to 2048 characters max', (done) => { + const longUrl = createLongUrl(3000); + const truncatedUrl = longUrl.slice(0, 2048); + + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', () => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams.url).to.equal(truncatedUrl); + + done(); + }); + + const parameters = { + ...requiredParameters, + url: longUrl, + }; + + expect(tracker.trackQuizResultsLoaded(parameters)).to.equal(true); + }); }); describe('trackQuizResultClick', () => { diff --git a/src/modules/tracker.js b/src/modules/tracker.js index 2a2b9922..8abb62fe 100644 --- a/src/modules/tracker.js +++ b/src/modules/tracker.js @@ -6,6 +6,8 @@ const EventEmitter = require('../utils/events'); const helpers = require('../utils/helpers'); const RequestQueue = require('../utils/request-queue'); +const MAX_URL_LENGTH = 2048; + function applyParams(parameters, options) { const { apiKey, @@ -270,7 +272,7 @@ class Tracker { } if (url) { - bodyParams.url = url; + bodyParams.url = helpers.truncateString(url, MAX_URL_LENGTH); } const requestURL = `${requestUrlPath}${applyParamsAsString(queryParams, this.options)}`; @@ -737,7 +739,7 @@ class Tracker { sort_order: sortOrder, sort_by: sortBy, selected_filters: selectedFilters, - url, + url: helpers.truncateString(url, MAX_URL_LENGTH), section, analytics_tags: analyticsTags, }; @@ -1282,7 +1284,7 @@ class Tracker { } if (url) { - bodyParams.url = url; + bodyParams.url = helpers.truncateString(url, MAX_URL_LENGTH); } if (podId) { @@ -1550,7 +1552,7 @@ class Tracker { } if (url) { - bodyParams.url = url; + bodyParams.url = helpers.truncateString(url, MAX_URL_LENGTH); } if (sortOrder) { @@ -2020,7 +2022,7 @@ class Tracker { bodyParams.quiz_id = quizId; bodyParams.quiz_version_id = quizVersionId; bodyParams.quiz_session_id = quizSessionId; - bodyParams.url = url; + bodyParams.url = helpers.truncateString(url, MAX_URL_LENGTH); if (!helpers.isNil(section)) { if (typeof section !== 'string') { diff --git a/src/utils/helpers.js b/src/utils/helpers.js index e76788f0..6c138c51 100644 --- a/src/utils/helpers.js +++ b/src/utils/helpers.js @@ -281,6 +281,8 @@ const utils = { return url; }, + + truncateString: (string, maxLength) => string.slice(0, maxLength), }; module.exports = utils;