From 25f59b112423173880380f24df40b1bbf4588e69 Mon Sep 17 00:00:00 2001 From: Stanley Peng Date: Wed, 26 Feb 2025 12:23:56 -0800 Subject: [PATCH 1/2] add domlesss case --- spec/src/utils/request-queue.js | 76 +++++++++++++++++++++++++++++++++ src/utils/humanity-check.js | 5 +++ 2 files changed, 81 insertions(+) diff --git a/spec/src/utils/request-queue.js b/spec/src/utils/request-queue.js index 53f6555b..767e5bed 100644 --- a/spec/src/utils/request-queue.js +++ b/spec/src/utils/request-queue.js @@ -850,5 +850,81 @@ describe('ConstructorIO - Utils - Request Queue', function utilsRequestQueue() { }); }); }); + + describe('domless', () => { + let fetchSpy = null; + + before(() => { + helpers.clearStorage(); + }); + + beforeEach(() => { + global.CLIENT_VERSION = 'cio-mocha'; + fetchSpy = sinon.spy(fetch); + + requestQueueOptions = { + fetch: fetchSpy, + sendTrackingEvents: true, + trackingSendDelay: 1, + }; + }); + + afterEach(() => { + requestQueueOptions = {}; + helpers.clearStorage(); + }); + + it('Should add url requests to the queue if the user is domless', async () => { + const requests = new RequestQueue(requestQueueOptions); + + requests.queue('https://ac.cnstrc.com/behavior?action=session_start'); + requests.queue('https://ac.cnstrc.com/behavior?action=focus'); + requests.queue('https://ac.cnstrc.com/behavior?action=magic_number_three'); + + expect(RequestQueue.get()).to.be.an('array').length(3); + }); + + it('Should send requests from the queue if the user is domless', (done) => { + const requests1 = new RequestQueue(requestQueueOptions); + const requests2 = new RequestQueue(requestQueueOptions); + const sendSpy1 = sinon.spy(requests1, 'send'); + const sendSpy2 = sinon.spy(requests2, 'send'); + + store.local.set(storageKey, [ + { + url: 'https://ac.cnstrc.com/behavior?action=session_start', + method: 'GET', + }, + { + url: 'https://ac.cnstrc.com/behavior?action=focus', + method: 'GET', + }, + { + url: 'https://ac.cnstrc.com/behavior?action=magic_number_three', + method: 'GET', + }, + { + url: 'https://ac.cnstrc.com/behavior?action=magic_number_four', + method: 'GET', + }, + { + url: 'https://ac.cnstrc.com/behavior?action=magic_number_five', + method: 'GET', + }, + ]); + + requests1.send(); + requests2.send(); + + setTimeout(() => { + expect(sendSpy1.callCount).to.be.at.least(2 + 1); // 2 min sent + 1 finally + expect(sendSpy2.callCount).to.be.at.least(2 + 1); // 2 min sent + 1 finally + expect(sendSpy1.callCount + sendSpy2.callCount).to.equal(5 + 2); // 5 sent + 2 finally + expect(RequestQueue.get()).to.be.an('array').length(0); + expect(store.local.get(storageKey)).to.be.null; + done(); + }, waitInterval); + }); + }); } }); diff --git a/src/utils/humanity-check.js b/src/utils/humanity-check.js index 46eb7212..9a1a6d12 100644 --- a/src/utils/humanity-check.js +++ b/src/utils/humanity-check.js @@ -57,6 +57,11 @@ class HumanityCheck { return true; } + // Bypass Storage/Event check if DOM not available. + if (!helpers.canUseDOM()) { + return false; + } + // If the user hasn't performed a human event, it indicates it is a bot if (!this.getIsHumanFromSessionStorage()) { return true; From bfe9e0ae26ef2ebb8f71df598b6aaa612067b02d Mon Sep 17 00:00:00 2001 From: Stanley Peng Date: Wed, 26 Feb 2025 13:46:48 -0800 Subject: [PATCH 2/2] address comments --- src/utils/humanity-check.js | 5 ----- src/utils/request-queue.js | 3 ++- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/utils/humanity-check.js b/src/utils/humanity-check.js index 9a1a6d12..46eb7212 100644 --- a/src/utils/humanity-check.js +++ b/src/utils/humanity-check.js @@ -57,11 +57,6 @@ class HumanityCheck { return true; } - // Bypass Storage/Event check if DOM not available. - if (!helpers.canUseDOM()) { - return false; - } - // If the user hasn't performed a human event, it indicates it is a bot if (!this.getIsHumanFromSessionStorage()) { return true; diff --git a/src/utils/request-queue.js b/src/utils/request-queue.js index dc547ef3..d5b46f21 100644 --- a/src/utils/request-queue.js +++ b/src/utils/request-queue.js @@ -31,7 +31,8 @@ class RequestQueue { // Add request to queue to be dispatched queue(url, method = 'GET', body = {}, networkParameters = {}) { - if (this.sendTrackingEvents && !this.humanity.isBot()) { + // Consider user "human" if no DOM context is available + if (this.sendTrackingEvents && (!helpers.canUseDOM() || !this.humanity.isBot())) { const queue = RequestQueue.get(); // PII Detection & Obfuscation