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

Optimized Normalization of # in URLs for referrer classification #38

Merged
merged 2 commits into from
Jan 10, 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
14 changes: 13 additions & 1 deletion facets.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,18 @@ export const facets = {
.filter((evt) => evt.checkpoint === 'enter')
.map((evt) => evt.source)
.filter((source) => source)
.map((source) => source.replace(/\/#$/, ''))
.map((source) => {
try {
const url = new URL(source);
url.hash = ''; // Remove the hash
return url.href;
} catch (e) {
// eslint-disable-next-line no-console
console.error(`Invalid URL: ${source}`);
return null;
}
})
.filter((source) => source)
.map((source) => {
const referrerClass = classifyReferrer(source);
return referrerClass ? [
Expand All @@ -169,6 +180,7 @@ export const facets = {
] : source;
})
.flat(),

/**
* Extracts the target of the media view event from the bundle. This
* is typically the URL of an image or video, and the URL is stripped
Expand Down
19 changes: 10 additions & 9 deletions test/facets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@ describe('facets:acquisitionSource', () => {

describe('facets:enterSource', () => {
it('enterSource:bare', () => {
assert.deepEqual(facets.enterSource({ events: [{ checkpoint: 'enter', source: 'https://www.example.com' }] }), ['https://www.example.com']);
assert.deepEqual(facets.enterSource({ events: [{ checkpoint: 'enter', source: 'https://www.google.com' }] }), ['https://www.google.com', 'search:google', 'search', '*:google']);
assert.deepEqual(facets.enterSource({ events: [{ checkpoint: 'enter', source: 'https://www.example.com/' }] }), ['https://www.example.com/']);
assert.deepEqual(facets.enterSource({ events: [{ checkpoint: 'enter', source: 'https://www.google.com/' }] }), ['https://www.google.com/', 'search:google', 'search', '*:google']);
});

it('enterSource:normalizeUrl', () => {
assert.deepEqual(facets.enterSource({ events: [{ checkpoint: 'enter', source: 'https://www.example.com/#' }] }), ['https://www.example.com']);
assert.deepEqual(facets.enterSource({ events: [{ checkpoint: 'enter', source: 'https://www.google.com/#' }] }), ['https://www.google.com', 'search:google', 'search', '*:google']);
assert.deepEqual(facets.enterSource({ events: [{ checkpoint: 'enter', source: 'https://www.example.com/#' }] }), ['https://www.example.com/']);
assert.deepEqual(facets.enterSource({ events: [{ checkpoint: 'enter', source: 'https://www.google.com/#' }] }), ['https://www.google.com/', 'search:google', 'search', '*:google']);
});

it('enterSource:DataChunks', () => {
Expand All @@ -227,11 +227,12 @@ describe('facets:enterSource', () => {
d.addSeries('pageViews', pageViews);
d.addFacet('enterSource', facets.enterSource);

assert.equal(d.facets.enterSource.length, 46);
assert.equal(d.facets.enterSource[2].value, 'search'); // all search engines
assert.equal(d.facets.enterSource[3].value, 'search:google'); // google search
assert.equal(d.facets.enterSource[4].value, '*:google'); // all google properties
assert.equal(d.facets.enterSource[5].value, 'https://www.google.com/'); // that one specific google page
assert.equal(d.facets.enterSource.length, 44);

assert.equal(d.facets.enterSource[0].value, 'search'); // all search engines
assert.equal(d.facets.enterSource[1].value, 'search:google'); // google search
assert.equal(d.facets.enterSource[2].value, '*:google'); // all google properties
assert.equal(d.facets.enterSource[3].value, 'https://www.google.com/'); // that one specific google page
});
});

Expand Down
Loading