Skip to content

Commit

Permalink
Merge pull request #1 from stevan-borus/fix/use-local-referers
Browse files Browse the repository at this point in the history
fix/use local referers
  • Loading branch information
stevan-borus authored Oct 11, 2024
2 parents 08980be + 189adcf commit b6a63b2
Show file tree
Hide file tree
Showing 8 changed files with 3,532 additions and 71 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A React-compatible referer parser library inspired by [nodejs-referer-parser](https://github.com/snowplow-referer-parser/nodejs-referer-parser).

The implementation uses the shared 'database' of known referers found in [file](https://s3-eu-west-1.amazonaws.com/snowplow-hosted-assets/third-party/referer-parser/referers-latest.yaml) as it says in the [snowplow-referer-parser/referer-parser](https://github.com/snowplow-referer-parser/referer-parser) README.
The implementation uses the shared 'database' of known referers found in [file](https://s3-eu-west-1.amazonaws.com/snowplow-hosted-assets/third-party/referer-parser/referers-latest.json) as it says in the [snowplow-referer-parser/referer-parser](https://github.com/snowplow-referer-parser/referer-parser) README.

## Installation

Expand Down Expand Up @@ -45,7 +45,7 @@ async function example() {
);

console.log("Search engine referral:", result);
// Output: Search engine referral: { medium: 'search', referer: 'google', term: 'gateway oracle cards denise linn' }
// Output: Search engine referral: { medium: 'search', referer: 'Google', term: 'gateway oracle cards denise linn' }
}

example();
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
"type": "git",
"url": "https://github.com/stevan-borus/react-referer-parser.git"
},
"dependencies": {
"js-yaml": "^4.1.0"
},
"devDependencies": {
"@biomejs/biome": "1.9.3",
"@types/js-yaml": "^4.0.9",
Expand Down
17 changes: 0 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('parse function', () => {
const result = await parse('http://www.google.com/search?q=test');
expect(result).toEqual({
medium: 'search',
referer: 'google',
referer: 'Google',
term: 'test',
});
});
Expand All @@ -88,7 +88,7 @@ describe('parse function', () => {
const result = await parse('http://www.google.co.uk/search?q=test');
expect(result).toEqual({
medium: 'search',
referer: 'google',
referer: 'Google',
term: 'test',
});
});
Expand All @@ -97,7 +97,7 @@ describe('parse function', () => {
const result = await parse('http://www.bing.com/search?q=test');
expect(result).toEqual({
medium: 'search',
referer: 'bing',
referer: 'Bing',
term: 'test',
});
});
Expand All @@ -106,7 +106,7 @@ describe('parse function', () => {
const result = await parse('http://search.yahoo.com/search?p=test');
expect(result).toEqual({
medium: 'search',
referer: 'yahoo',
referer: 'Yahoo!',
term: 'test',
});
});
Expand All @@ -115,7 +115,7 @@ describe('parse function', () => {
const result = await parse('http://www.facebook.com/page');
expect(result).toEqual({
medium: 'social',
referer: 'facebook',
referer: 'Facebook',
term: null,
});
});
Expand All @@ -124,7 +124,7 @@ describe('parse function', () => {
const result = await parse('http://t.co/abcde');
expect(result).toEqual({
medium: 'social',
referer: 'twitter',
referer: 'Twitter',
term: null,
});
});
Expand All @@ -133,7 +133,7 @@ describe('parse function', () => {
const result = await parse('http://www.linkedin.com/share');
expect(result).toEqual({
medium: 'social',
referer: 'linkedin',
referer: 'LinkedIn',
term: null,
});
});
Expand All @@ -142,7 +142,7 @@ describe('parse function', () => {
const result = await parse('http://lnkd.in/abcde');
expect(result).toEqual({
medium: 'social',
referer: 'linkedin',
referer: 'LinkedIn',
term: null,
});
});
Expand All @@ -151,7 +151,7 @@ describe('parse function', () => {
const result = await parse('http://www.instagram.com/p/abcde');
expect(result).toEqual({
medium: 'social',
referer: 'instagram',
referer: 'Instagram',
term: null,
});
});
Expand All @@ -160,7 +160,7 @@ describe('parse function', () => {
const result = await parse('http://www.pinterest.com/pin/abcde');
expect(result).toEqual({
medium: 'social',
referer: 'pinterest',
referer: 'Pinterest',
term: null,
});
});
Expand All @@ -169,7 +169,7 @@ describe('parse function', () => {
const result = await parse('http://mail.google.com/mail/u/0');
expect(result).toEqual({
medium: 'email',
referer: 'gmail',
referer: 'Gmail',
term: null,
});
});
Expand All @@ -187,7 +187,7 @@ describe('parse function', () => {
const result = await parse('http://www.google.com/');
expect(result).toEqual({
medium: 'search',
referer: 'google',
referer: 'Google',
term: null,
});
});
Expand All @@ -196,7 +196,7 @@ describe('parse function', () => {
const result = await parse('http://www.google.com/search?q=');
expect(result).toEqual({
medium: 'search',
referer: 'google',
referer: 'Google',
term: null,
});
});
Expand All @@ -205,7 +205,7 @@ describe('parse function', () => {
const result = await parse('http://www.google.com/search?q=test&hl=en');
expect(result).toEqual({
medium: 'search',
referer: 'google',
referer: 'Google',
term: 'test',
});
});
Expand All @@ -221,7 +221,7 @@ describe('parse function', () => {
const result = await parse('http://WWW.GOOGLE.COM/search?q=test');
expect(result).toEqual({
medium: 'search',
referer: 'google',
referer: 'Google',
term: 'test',
});
});
Expand All @@ -230,7 +230,7 @@ describe('parse function', () => {
const result = await parse('http://www.google.com:8080/search?q=test');
expect(result).toEqual({
medium: 'search',
referer: 'google',
referer: 'Google',
term: 'test',
});
});
Expand All @@ -239,7 +239,7 @@ describe('parse function', () => {
const result = await parse('http://user:[email protected]/search?q=test');
expect(result).toEqual({
medium: 'search',
referer: 'google',
referer: 'Google',
term: 'test',
});
});
Expand Down
33 changes: 6 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
import { load } from 'js-yaml';
import type { Medium, Referer, RefererData } from './types';

let cachedRefererData: RefererData | null = null;
let lastFetchTime = 0;
let CACHE_DURATION = 24 * 60 * 60 * 1000;

async function fetchAndParseRefererData(): Promise<RefererData> {
let response = await fetch(
'https://s3-eu-west-1.amazonaws.com/snowplow-hosted-assets/third-party/referer-parser/referers-latest.yaml',
);

let yamlText = await response.text();

return load(yamlText) as RefererData;
}
import type { Referer, RefererData } from './types';
import refererData from './utils/latest-referers.json';

export async function parse(
refererUrl: string,
refererUrl: string | null,
pageUrl?: string,
internalDomains: string[] = [],
): Promise<Referer> {
let currentTime = Date.now();

if (!cachedRefererData || currentTime - lastFetchTime > CACHE_DURATION) {
cachedRefererData = await fetchAndParseRefererData();
lastFetchTime = currentTime;
}

if (!refererUrl) {
return { medium: 'direct', referer: null, term: null };
}
Expand All @@ -41,7 +20,7 @@ export async function parse(
return { medium: 'internal', referer: null, term: null };
}

let bestMatch = findBestMatch(refererParsed, cachedRefererData);
let bestMatch = findBestMatch(refererParsed, refererData as RefererData);

return (
bestMatch || {
Expand Down Expand Up @@ -103,8 +82,8 @@ function findBestMatch(refererParsed: URL, cachedRefererData: RefererData): Refe
return null;
}

function findTerm(refererParsed: URL, parameters: string[]): string | null {
if (parameters.length === 0) {
function findTerm(refererParsed: URL, parameters?: string[]): string | null {
if (!parameters || parameters.length === 0) {
return null;
}

Expand Down
77 changes: 73 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,86 @@ export type Medium =
| 'direct'
| (string & {});

export interface Referer {
type RefererSource =
| 'Google'
| 'Yandex Maps'
| 'Yahoo!'
| 'TalkTalk'
| '1.cz'
| 'Softonic'
| 'GAIS'
| 'Freecause'
| '360.cn'
| 'RPMFind'
| 'Comcast'
| 'Voila'
| 'Nifty'
| 'Atlas'
| 'Lo.st'
| 'DasTelefonbuch'
| 'Fireball'
| '1und1'
| 'Virgilio'
| 'Telstra'
| 'Web.nl'
| 'Plazoo'
| 'Goyellow.de'
| 'AOL'
| 'Acoon'
| 'Free'
| 'Apollo Latvia'
| 'HighBeam'
| 'I-play'
| 'FriendFeed'
| 'Yasni'
| 'Gigablast'
| 'Arcor'
| 'arama'
| 'Fixsuche'
| 'Apontador'
| 'Search.com'
| 'Monstercrawler'
| 'Google Images'
| 'ABCsøk'
| 'Google Product Search'
| 'Blogpulse'
| 'Hooseek.com'
| 'Dalesearch'
| 'Alice Adsl'
| 'T-Online'
| 'Sogou'
| 'Hit-Parade'
| 'SearchCanvas'
| 'Jungle Key'
| 'Interia'
| 'Genieo'
| 'Tiscali'
| 'Gomeo'
| 'Facebook'
| 'Twitter'
| 'LinkedIn'
| 'Instagram'
| 'Pinterest'
| 'YouTube'
| 'Reddit'
| 'Tumblr'
| 'WhatsApp'
| 'Telegram'
| 'Snapchat'
| 'TikTok'
| (string & {});

export type Referer = {
medium: Medium;
referer: string | null;
referer: RefererSource | null;
term: string | null;
}
};

export interface RefererData {
[medium: string]: {
[source: string]: {
domains: string[];
parameters: string[];
parameters?: string[];
};
};
}
Loading

0 comments on commit b6a63b2

Please sign in to comment.