Skip to content

Commit 3b43f64

Browse files
DiegoPinto357Diego dos Santos
andauthored
CI-4605 Truncate URL params for requests to 2048 characters max (#384)
* feat: add URL truncation on trackItemDetailLoad method * feat: add URL truncation on trackSearchResultsLoaded method * refactor: simplify parameters structure in trackItemDetailLoad test * feat: add URL truncation on trackRecommendationView method * feat: add URL truncation on trackBrowseResultsLoaded method * feat: add URL truncation on trackQuizResultsLoaded method --------- Co-authored-by: Diego dos Santos <diego.pinto-dos-santos@constructor.io>
1 parent 0b403ae commit 3b43f64

3 files changed

Lines changed: 154 additions & 5 deletions

File tree

spec/src/modules/tracker.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ function validateOriginReferrer(requestParams) {
3434
expect(requestParams).to.have.property('origin_referrer').to.contain('utm_campaign=campaign_1');
3535
}
3636

37+
function createLongUrl(length) {
38+
const baseUrl = 'https://constructor.io/product/KMH876?a=';
39+
return `${baseUrl}${'a'.repeat(length - baseUrl.length)}`;
40+
}
41+
3742
describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
3843
let fetchSpy = null;
3944
let cleanup;
@@ -1465,6 +1470,34 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
14651470
expect(tracker.trackItemDetailLoad(requiredParameters)).to.equal(true);
14661471
});
14671472
}
1473+
1474+
it('Should truncate url param to 2048 characters max', (done) => {
1475+
const longUrl = createLongUrl(3000);
1476+
const truncatedUrl = longUrl.slice(0, 2048);
1477+
1478+
const { tracker } = new ConstructorIO({
1479+
apiKey: testApiKey,
1480+
fetch: fetchSpy,
1481+
...requestQueueOptions,
1482+
});
1483+
1484+
tracker.on('success', () => {
1485+
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);
1486+
1487+
// Request
1488+
expect(fetchSpy).to.have.been.called;
1489+
expect(requestParams.url).to.equal(truncatedUrl);
1490+
1491+
done();
1492+
});
1493+
1494+
const parameters = {
1495+
...requiredParameters,
1496+
url: longUrl,
1497+
};
1498+
1499+
expect(tracker.trackItemDetailLoad(parameters)).to.equal(true);
1500+
});
14681501
});
14691502

14701503
describe('trackSearchSubmit', () => {
@@ -2566,6 +2599,34 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
25662599

25672600
expect(tracker.trackSearchResultsLoaded(term, requiredParameters)).to.equal(true);
25682601
});
2602+
2603+
it('Should truncate url param to 2048 characters max', (done) => {
2604+
const longUrl = createLongUrl(3000);
2605+
const truncatedUrl = longUrl.slice(0, 2048);
2606+
2607+
const { tracker } = new ConstructorIO({
2608+
apiKey: testApiKey,
2609+
fetch: fetchSpy,
2610+
...requestQueueOptions,
2611+
});
2612+
2613+
tracker.on('success', () => {
2614+
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);
2615+
2616+
// Request
2617+
expect(fetchSpy).to.have.been.called;
2618+
expect(requestParams.url).to.equal(truncatedUrl);
2619+
2620+
done();
2621+
});
2622+
2623+
const parameters = {
2624+
...requiredParameters,
2625+
url: longUrl,
2626+
};
2627+
2628+
expect(tracker.trackSearchResultsLoaded(term, parameters)).to.equal(true);
2629+
});
25692630
});
25702631

25712632
describe('trackSearchResultClick', () => {
@@ -4805,6 +4866,34 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
48054866

48064867
expect(tracker.trackRecommendationView(requiredParamsWithSeedItemIds)).to.equal(true);
48074868
});
4869+
4870+
it('Should truncate url param to 2048 characters max', (done) => {
4871+
const longUrl = createLongUrl(3000);
4872+
const truncatedUrl = longUrl.slice(0, 2048);
4873+
4874+
const { tracker } = new ConstructorIO({
4875+
apiKey: testApiKey,
4876+
fetch: fetchSpy,
4877+
...requestQueueOptions,
4878+
});
4879+
4880+
tracker.on('success', () => {
4881+
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);
4882+
4883+
// Request
4884+
expect(fetchSpy).to.have.been.called;
4885+
expect(requestParams.url).to.equal(truncatedUrl);
4886+
4887+
done();
4888+
});
4889+
4890+
const parameters = {
4891+
...requiredParameters,
4892+
url: longUrl,
4893+
};
4894+
4895+
expect(tracker.trackRecommendationView(parameters)).to.equal(true);
4896+
});
48084897
});
48094898

48104899
describe('trackRecommendationClick', () => {
@@ -5696,6 +5785,34 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
56965785

56975786
expect(tracker.trackBrowseResultsLoaded(requiredParameters)).to.equal(true);
56985787
});
5788+
5789+
it('Should truncate url param to 2048 characters max', (done) => {
5790+
const longUrl = createLongUrl(3000);
5791+
const truncatedUrl = longUrl.slice(0, 2048);
5792+
5793+
const { tracker } = new ConstructorIO({
5794+
apiKey: testApiKey,
5795+
fetch: fetchSpy,
5796+
...requestQueueOptions,
5797+
});
5798+
5799+
tracker.on('success', () => {
5800+
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);
5801+
5802+
// Request
5803+
expect(fetchSpy).to.have.been.called;
5804+
expect(requestParams.url).to.equal(truncatedUrl);
5805+
5806+
done();
5807+
});
5808+
5809+
const parameters = {
5810+
...requiredParameters,
5811+
url: longUrl,
5812+
};
5813+
5814+
expect(tracker.trackBrowseResultsLoaded(parameters)).to.equal(true);
5815+
});
56995816
});
57005817

57015818
describe('trackBrowseRedirect', () => {
@@ -7343,6 +7460,34 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
73437460

73447461
expect(tracker.trackQuizResultsLoaded(requiredParameters)).to.equal(true);
73457462
});
7463+
7464+
it('Should truncate url param to 2048 characters max', (done) => {
7465+
const longUrl = createLongUrl(3000);
7466+
const truncatedUrl = longUrl.slice(0, 2048);
7467+
7468+
const { tracker } = new ConstructorIO({
7469+
apiKey: testApiKey,
7470+
fetch: fetchSpy,
7471+
...requestQueueOptions,
7472+
});
7473+
7474+
tracker.on('success', () => {
7475+
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);
7476+
7477+
// Request
7478+
expect(fetchSpy).to.have.been.called;
7479+
expect(requestParams.url).to.equal(truncatedUrl);
7480+
7481+
done();
7482+
});
7483+
7484+
const parameters = {
7485+
...requiredParameters,
7486+
url: longUrl,
7487+
};
7488+
7489+
expect(tracker.trackQuizResultsLoaded(parameters)).to.equal(true);
7490+
});
73467491
});
73477492

73487493
describe('trackQuizResultClick', () => {

src/modules/tracker.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const EventEmitter = require('../utils/events');
66
const helpers = require('../utils/helpers');
77
const RequestQueue = require('../utils/request-queue');
88

9+
const MAX_URL_LENGTH = 2048;
10+
911
function applyParams(parameters, options) {
1012
const {
1113
apiKey,
@@ -270,7 +272,7 @@ class Tracker {
270272
}
271273

272274
if (url) {
273-
bodyParams.url = url;
275+
bodyParams.url = helpers.truncateString(url, MAX_URL_LENGTH);
274276
}
275277

276278
const requestURL = `${requestUrlPath}${applyParamsAsString(queryParams, this.options)}`;
@@ -737,7 +739,7 @@ class Tracker {
737739
sort_order: sortOrder,
738740
sort_by: sortBy,
739741
selected_filters: selectedFilters,
740-
url,
742+
url: helpers.truncateString(url, MAX_URL_LENGTH),
741743
section,
742744
analytics_tags: analyticsTags,
743745
};
@@ -1282,7 +1284,7 @@ class Tracker {
12821284
}
12831285

12841286
if (url) {
1285-
bodyParams.url = url;
1287+
bodyParams.url = helpers.truncateString(url, MAX_URL_LENGTH);
12861288
}
12871289

12881290
if (podId) {
@@ -1550,7 +1552,7 @@ class Tracker {
15501552
}
15511553

15521554
if (url) {
1553-
bodyParams.url = url;
1555+
bodyParams.url = helpers.truncateString(url, MAX_URL_LENGTH);
15541556
}
15551557

15561558
if (sortOrder) {
@@ -2020,7 +2022,7 @@ class Tracker {
20202022
bodyParams.quiz_id = quizId;
20212023
bodyParams.quiz_version_id = quizVersionId;
20222024
bodyParams.quiz_session_id = quizSessionId;
2023-
bodyParams.url = url;
2025+
bodyParams.url = helpers.truncateString(url, MAX_URL_LENGTH);
20242026

20252027
if (!helpers.isNil(section)) {
20262028
if (typeof section !== 'string') {

src/utils/helpers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ const utils = {
281281

282282
return url;
283283
},
284+
285+
truncateString: (string, maxLength) => string.slice(0, maxLength),
284286
};
285287

286288
module.exports = utils;

0 commit comments

Comments
 (0)