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

fix: exclude bot traffic from page views and opportunities ctr calculations #446

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ const ONE_DAY = ONE_HOUR * HOURS_IN_DAY;

const CHUNK_SIZE = 31;

function filterBundles(checkpoints = []) {
function isBotTraffic(bundle) {
return bundle?.userAgent?.includes('bot');
}

function filterEvents(checkpoints = []) {
return (bundle) => {
if (checkpoints.length > 0) {
const events = bundle.events.filter((event) => checkpoints.includes(event.checkpoint));
Expand Down Expand Up @@ -160,6 +164,7 @@ async function fetchBundles(opts = {}) {
interval = 7,
granularity = GRANULARITY.DAILY,
checkpoints = [],
filterBotTraffic = true,
} = opts;

if (!hasText(domain) || !hasText(domainkey)) {
Expand All @@ -185,7 +190,11 @@ async function fetchBundles(opts = {}) {
for (const chunk of chunks) {
const responses = await Promise.all(chunk.map((url) => fetch(url)));
const bundles = await Promise.all(responses.map((response) => response.json()));
result.push(...bundles.flatMap((b) => b.rumBundles.map(filterBundles(checkpoints))));
result.push(...bundles.flatMap(
(b) => b.rumBundles.filter(
(bundle) => !filterBotTraffic || !isBotTraffic(bundle),
).map(filterEvents(checkpoints)),
));
}
return mergeBundlesWithSameId(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
describe('Rum bundler client', () => {
let sandbox;

before('setup', function () {

Check warning on line 27 in packages/spacecat-shared-rum-api-client/test/common/rum-bundler-client.test.js

View workflow job for this annotation

GitHub Actions / Test

Unexpected unnamed function
const mockDate = '2024-06-02T12:30:01.124Z';
sandbox = sinon.createSandbox();
this.clock = sandbox.useFakeTimers({
Expand All @@ -32,7 +32,7 @@
});
});

after('clean', function () {

Check warning on line 35 in packages/spacecat-shared-rum-api-client/test/common/rum-bundler-client.test.js

View workflow job for this annotation

GitHub Actions / Test

Unexpected unnamed function
this.clock.uninstall();
});

Expand Down Expand Up @@ -72,9 +72,69 @@
const containsCorrectData = result.every((item) => item.events.length === 1 && item.events[0].checkpoint === 'good');

expect(result.length).to.equal(dates.length);
// eslint-disable-next-line no-unused-expressions
expect(containsCorrectData).to.be.true;
});

it('should filter bot traffic by default', async () => {
const domain = 'some-domain.com';
const domainkey = 'testkey';
const granularity = 'HOURLY';
const interval = 7;
const allCheckpoints = ['good', 'bad'];

const dates = generateHourlyDates(7);
const rumBundles = generateRumBundles(dates, allCheckpoints);
// make the first bundle a bot traffic
rumBundles[Object.keys(rumBundles)[0]].rumBundles[0].userAgent = 'bot';

for (const date of dates) {
nock(BASE_URL)
.get(`/bundles/${domain}/${date[0]}/${date[1]}/${date[2]}/${date[3]}?domainkey=${domainkey}`)
.reply(200, rumBundles[date.join()]);
}

const result = await fetchBundles({
domain,
domainkey,
granularity,
interval,
checkpoints: ['good'],
});

expect(result.length).to.equal(dates.length - 1);
});

it('should not filter bot traffic when disabled', async () => {
const domain = 'some-domain.com';
const domainkey = 'testkey';
const granularity = 'HOURLY';
const interval = 7;
const allCheckpoints = ['good', 'bad'];

const dates = generateHourlyDates(7);
const rumBundles = generateRumBundles(dates, allCheckpoints);
// make the first bundle a bot traffic
rumBundles[Object.keys(rumBundles)[0]].rumBundles[0].userAgent = 'bot';

for (const date of dates) {
nock(BASE_URL)
.get(`/bundles/${domain}/${date[0]}/${date[1]}/${date[2]}/${date[3]}?domainkey=${domainkey}`)
.reply(200, rumBundles[date.join()]);
}

const result = await fetchBundles({
domain,
domainkey,
granularity,
interval,
checkpoints: ['good'],
filterBotTraffic: false,
});

expect(result.length).to.equal(dates.length);
});

it('should fetch correct daily bundles and filter by checkpoint', async () => {
const domain = 'some-domain.com';
const domainkey = 'testkey';
Expand Down Expand Up @@ -102,6 +162,7 @@
const containsCorrectData = result.every((item) => item.events.length === 1 && item.events[0].checkpoint === 'good');

expect(result.length).to.equal(dates.length);
// eslint-disable-next-line no-unused-expressions
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

somehow eslint is blocking me from committing, so I've to add these, even though this is not related to my change.

expect(containsCorrectData).to.be.true;
});

Expand Down Expand Up @@ -132,6 +193,7 @@
const containsCorrectData = result.every((item) => item.events.length === 2);

expect(result.length).to.equal(dates.length);
// eslint-disable-next-line no-unused-expressions
expect(containsCorrectData).to.be.true;
});
});
2 changes: 1 addition & 1 deletion packages/spacecat-shared-rum-api-client/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('RUMAPIClient', () => {
interval: 0,
};
const result = await rumApiClient.query('404', opts);

// eslint-disable-next-line no-unused-expressions
expect(result).to.be.empty;
});

Expand Down
Loading