Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NOCSL] Request Queue - handle domlesss case #368

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions spec/src/utils/request-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}
});
3 changes: 2 additions & 1 deletion src/utils/request-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading