mirror of https://github.com/artifacthub/hub.git
Track views only when no bots
Signed-off-by: Cintia Sánchez García <cynthiasg@icloud.com>
This commit is contained in:
parent
e7d3d58337
commit
d888ab6ed7
|
|
@ -5,6 +5,7 @@
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@analytics/google-analytics-v3": "^0.7.0",
|
"@analytics/google-analytics-v3": "^0.7.0",
|
||||||
|
"@fingerprintjs/botd": "^1.9.1",
|
||||||
"analytics": "^0.8.16",
|
"analytics": "^0.8.16",
|
||||||
"apexcharts": "3.49.0",
|
"apexcharts": "3.49.0",
|
||||||
"bootstrap": "^5.3.5",
|
"bootstrap": "^5.3.5",
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ jest.mock('rehype-github-alerts', () => () => <div />);
|
||||||
jest.mock('../../utils/bannerDispatcher', () => ({
|
jest.mock('../../utils/bannerDispatcher', () => ({
|
||||||
getBanner: () => null,
|
getBanner: () => null,
|
||||||
}));
|
}));
|
||||||
|
jest.mock('../../utils/detectBot', () => jest.fn(() => Promise.resolve(false)));
|
||||||
|
|
||||||
const getMockPackage = (fixtureId: string): Package => {
|
const getMockPackage = (fixtureId: string): Package => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ import {
|
||||||
Version,
|
Version,
|
||||||
} from '../../types';
|
} from '../../types';
|
||||||
import bannerDispatcher from '../../utils/bannerDispatcher';
|
import bannerDispatcher from '../../utils/bannerDispatcher';
|
||||||
|
import detectBot from '../../utils/detectBot';
|
||||||
import isFuture from '../../utils/isFuture';
|
import isFuture from '../../utils/isFuture';
|
||||||
import isPackageOfficial from '../../utils/isPackageOfficial';
|
import isPackageOfficial from '../../utils/isPackageOfficial';
|
||||||
import { prepareQueryString } from '../../utils/prepareQueryString';
|
import { prepareQueryString } from '../../utils/prepareQueryString';
|
||||||
|
|
@ -172,7 +173,11 @@ const PackageView = () => {
|
||||||
|
|
||||||
async function trackView(pkgID: string, version: string) {
|
async function trackView(pkgID: string, version: string) {
|
||||||
try {
|
try {
|
||||||
API.trackView(pkgID, version);
|
// Skip tracking for bots and crawlers
|
||||||
|
if (await detectBot()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await API.trackView(pkgID, version);
|
||||||
} catch {
|
} catch {
|
||||||
// Do not do anything
|
// Do not do anything
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
import detectBot from './detectBot';
|
||||||
|
|
||||||
|
// Mock the detectBot function for testing
|
||||||
|
const mockDetectBot = detectBot as jest.MockedFunction<typeof detectBot>;
|
||||||
|
|
||||||
|
jest.mock('./detectBot', () => jest.fn());
|
||||||
|
|
||||||
|
describe('detectBot', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should detect bots', async () => {
|
||||||
|
mockDetectBot.mockResolvedValue(true);
|
||||||
|
const result = await detectBot();
|
||||||
|
expect(result).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not detect regular browsers', async () => {
|
||||||
|
mockDetectBot.mockResolvedValue(false);
|
||||||
|
const result = await detectBot();
|
||||||
|
expect(result).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle server-side rendering', async () => {
|
||||||
|
mockDetectBot.mockResolvedValue(true);
|
||||||
|
const result = await detectBot();
|
||||||
|
expect(result).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
import { load } from '@fingerprintjs/botd';
|
||||||
|
|
||||||
|
// Bot detection utility using BotD library for identifying automated requests
|
||||||
|
// This helps prevent bot traffic from being counted in view statistics
|
||||||
|
|
||||||
|
let botdPromise: Promise<Awaited<ReturnType<typeof load>>> | null = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize BotD library (singleton pattern)
|
||||||
|
*/
|
||||||
|
const initializeBotD = async (): Promise<Awaited<ReturnType<typeof load>>> => {
|
||||||
|
if (!botdPromise) {
|
||||||
|
botdPromise = load();
|
||||||
|
}
|
||||||
|
return botdPromise;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detects if the current request is likely from a bot or crawler using BotD
|
||||||
|
* @returns Promise<boolean> indicating if the request is from a bot
|
||||||
|
*/
|
||||||
|
const detectBot = async (): Promise<boolean> => {
|
||||||
|
try {
|
||||||
|
const botd = await initializeBotD();
|
||||||
|
const result = await botd.detect();
|
||||||
|
return result?.bot || false;
|
||||||
|
} catch (error) {
|
||||||
|
return false; // In case of error, assume it's not a bot
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default detectBot;
|
||||||
|
|
@ -1353,6 +1353,13 @@
|
||||||
"@eslint/core" "^0.13.0"
|
"@eslint/core" "^0.13.0"
|
||||||
levn "^0.4.1"
|
levn "^0.4.1"
|
||||||
|
|
||||||
|
"@fingerprintjs/botd@^1.9.1":
|
||||||
|
version "1.9.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/@fingerprintjs/botd/-/botd-1.9.1.tgz#6217541bc822bef4f17b3b1931955d2ddac5d4ec"
|
||||||
|
integrity sha512-7kv3Yolsx9E56i+L1hCEcupH5yqcI5cmVktxy6B0K7rimaH5qDXwsiA5FL+fkxeUny7XQKn7p13HvK7ofDZB3g==
|
||||||
|
dependencies:
|
||||||
|
tslib "^2.4.0"
|
||||||
|
|
||||||
"@humanfs/core@^0.19.1":
|
"@humanfs/core@^0.19.1":
|
||||||
version "0.19.1"
|
version "0.19.1"
|
||||||
resolved "https://registry.yarnpkg.com/@humanfs/core/-/core-0.19.1.tgz#17c55ca7d426733fe3c561906b8173c336b40a77"
|
resolved "https://registry.yarnpkg.com/@humanfs/core/-/core-0.19.1.tgz#17c55ca7d426733fe3c561906b8173c336b40a77"
|
||||||
|
|
@ -11226,7 +11233,7 @@ tslib@^1.8.1:
|
||||||
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
|
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
|
||||||
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==
|
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==
|
||||||
|
|
||||||
tslib@^2.0.3, tslib@^2.8.1:
|
tslib@^2.0.3, tslib@^2.4.0, tslib@^2.8.1:
|
||||||
version "2.8.1"
|
version "2.8.1"
|
||||||
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.8.1.tgz#612efe4ed235d567e8aba5f2a5fab70280ade83f"
|
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.8.1.tgz#612efe4ed235d567e8aba5f2a5fab70280ade83f"
|
||||||
integrity sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==
|
integrity sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue