-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add Yandex Metrica message processor and corresponding test #192
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
19b6b58
feat: add Yandex Metrica message processor and corresponding test
Dobrunia 856a29f
webvisor
Dobrunia 69061d2
feat: enhance Yandex Metrica message processor to support multiple co…
Dobrunia 2484bef
refactor: rename Yandex Metrica addon and update references in Catche…
Dobrunia a208e13
update readme
Dobrunia 3ca01d7
chore
Dobrunia d09a15e
chore
Dobrunia a4352f6
chore
Dobrunia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
packages/browser/src/addons/yandex-metrica-addon-message-processor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import type { MessageProcessor, ProcessingPayload } from '@hawk.so/core'; | ||
|
|
||
| /** | ||
| * Addon key used to attach Yandex Metrica identifiers. | ||
| */ | ||
| export const YANDEX_METRICA_ADDON_KEY = 'yandexMetrica'; | ||
|
|
||
| const MAX_YANDEX_METRICA_COUNTERS = 10; | ||
|
|
||
| interface YandexMetricaFunction { | ||
|
Dobrunia marked this conversation as resolved.
Outdated
|
||
| ( | ||
| counterId: number, | ||
| method: 'getClientID', | ||
| callback: (clientId: unknown) => void | ||
| ): void; | ||
| a?: ArrayLike<ArrayLike<unknown>>; | ||
| } | ||
|
|
||
| type WindowWithYandexMetrica = Window & { | ||
| ym?: YandexMetricaFunction; | ||
| }; | ||
|
|
||
| interface YandexMetricaIdentifiers { | ||
| counterId: number; | ||
| clientId: string; | ||
| } | ||
|
|
||
| /** | ||
| * Reads up to ten Yandex Metrica counter IDs, requests their ClientIDs once | ||
| * during initialization, and attaches available identifiers to subsequent events. | ||
| * | ||
| * Important: `window.ym.a[index][0]` relies on the Metrica initialization queue | ||
| * and is not a public API contract. This is acceptable for the MVP, but the SDK | ||
| * should accept counter IDs explicitly in the future. | ||
| * | ||
| * @see https://yandex.ru/support/metrica/ru/objects/get-client-id | ||
| */ | ||
| export class YandexMetricaAddonMessageProcessor implements MessageProcessor<'errors/javascript'> { | ||
| /** | ||
| * Cached Yandex Metrica identifiers keyed by their one-based queue position. | ||
|
Dobrunia marked this conversation as resolved.
Outdated
|
||
| */ | ||
| private identifiers: Record<number, YandexMetricaIdentifiers> = {}; | ||
|
|
||
| /** | ||
| * Reads up to ten initialized counters and requests their ClientIDs. | ||
| */ | ||
| constructor() { | ||
|
Dobrunia marked this conversation as resolved.
Outdated
|
||
| const ym = (window as WindowWithYandexMetrica).ym; | ||
|
|
||
| if (typeof ym !== 'function') { | ||
| return; | ||
| } | ||
|
|
||
| for (let queueIndex = 0; queueIndex < MAX_YANDEX_METRICA_COUNTERS; queueIndex++) { | ||
| const queueEntry = ym.a?.[queueIndex]; | ||
| const rawCounterId = queueEntry?.[0]; | ||
| const counterId = typeof rawCounterId === 'number' || typeof rawCounterId === 'string' | ||
| ? Number(rawCounterId) | ||
| : NaN; | ||
| const options = queueEntry?.[2] as { webvisor?: unknown } | undefined; | ||
| const isWebvisorEnabled = options?.webvisor === true; | ||
|
|
||
| if (!Number.isSafeInteger(counterId) || counterId <= 0 || !isWebvisorEnabled) { | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| ym(counterId, 'getClientID', (clientId) => { | ||
| if (typeof clientId === 'string' && clientId.length > 0) { | ||
| this.identifiers[queueIndex + 1] = { | ||
| counterId, | ||
| clientId, | ||
| }; | ||
| } | ||
| }); | ||
| } catch { | ||
| /** | ||
| * Yandex Metrica integration must not affect error reporting. | ||
| */ | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Attaches cached Yandex Metrica identifiers when they are available. | ||
| * | ||
| * @param payload - event message payload to enrich | ||
| * @returns {ProcessingPayload<'errors/javascript'>} enriched or original payload | ||
| */ | ||
| public apply( | ||
| payload: ProcessingPayload<'errors/javascript'> | ||
| ): ProcessingPayload<'errors/javascript'> { | ||
| if (Object.keys(this.identifiers).length > 0) { | ||
| (payload.addons as Record<string, unknown>)[YANDEX_METRICA_ADDON_KEY] = { | ||
| ...this.identifiers, | ||
| }; | ||
| } | ||
|
|
||
| return payload; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
packages/browser/tests/addons/yandex-metrica-message-processor.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
| import { | ||
| YANDEX_METRICA_ADDON_KEY, | ||
| YandexMetricaAddonMessageProcessor | ||
| } from '../../src/addons/yandex-metrica-addon-message-processor'; | ||
|
Dobrunia marked this conversation as resolved.
Outdated
|
||
| import { makePayload } from './message-processor.helpers'; | ||
|
|
||
| type YandexMetricaMock = ReturnType<typeof vi.fn> & { | ||
| a?: ArrayLike<ArrayLike<unknown>>; | ||
| }; | ||
|
|
||
| function setYandexMetrica(ym?: YandexMetricaMock): void { | ||
| Object.defineProperty(window, 'ym', { | ||
| configurable: true, | ||
| value: ym, | ||
| }); | ||
| } | ||
|
|
||
| describe('YandexMetricaAddonMessageProcessor', () => { | ||
| afterEach(() => { | ||
| setYandexMetrica(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('should attach counterId and ClientID for multiple Yandex Metrica counters', () => { | ||
| const ym = vi.fn((counterId, _method, callback) => callback(`client-${counterId}`)) as YandexMetricaMock; | ||
|
|
||
| ym.a = [ | ||
| [456, 'init', { webvisor: true }], | ||
| [789, 'init', { webvisor: true }], | ||
| ]; | ||
| setYandexMetrica(ym); | ||
|
|
||
| const result = new YandexMetricaAddonMessageProcessor().apply(makePayload()); | ||
|
|
||
| expect(ym).toHaveBeenCalledWith(456, 'getClientID', expect.any(Function)); | ||
| expect(ym).toHaveBeenCalledWith(789, 'getClientID', expect.any(Function)); | ||
| expect(result.addons).toHaveProperty(YANDEX_METRICA_ADDON_KEY, { | ||
| ['1']: { | ||
| counterId: 456, | ||
| clientId: 'client-456', | ||
| }, | ||
| ['2']: { | ||
| counterId: 789, | ||
| clientId: 'client-789', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should leave payload unchanged when Yandex Metrica is not installed', () => { | ||
| const payload = makePayload(); | ||
| const result = new YandexMetricaAddonMessageProcessor().apply(payload); | ||
|
|
||
| expect(result).toBe(payload); | ||
| expect(result.addons).toEqual({}); | ||
| }); | ||
|
|
||
| it('should leave payload unchanged when counter ID is unavailable', () => { | ||
| const ym = vi.fn() as YandexMetricaMock; | ||
|
|
||
| setYandexMetrica(ym); | ||
|
|
||
| const payload = makePayload(); | ||
| const result = new YandexMetricaAddonMessageProcessor().apply(payload); | ||
|
|
||
| expect(ym).not.toHaveBeenCalled(); | ||
| expect(result.addons).toEqual({}); | ||
| }); | ||
|
|
||
| it('should leave payload unchanged when webvisor is disabled', () => { | ||
| const ym = vi.fn() as YandexMetricaMock; | ||
|
|
||
| ym.a = [[456, 'init', { webvisor: false }]]; | ||
| setYandexMetrica(ym); | ||
|
|
||
| const payload = makePayload(); | ||
| const result = new YandexMetricaAddonMessageProcessor().apply(payload); | ||
|
|
||
| expect(ym).not.toHaveBeenCalled(); | ||
| expect(result.addons).toEqual({}); | ||
| }); | ||
|
|
||
| it('should leave payload unchanged when webvisor option is missing', () => { | ||
| const ym = vi.fn() as YandexMetricaMock; | ||
|
|
||
| ym.a = [[456, 'init', {}]]; | ||
| setYandexMetrica(ym); | ||
|
|
||
| const payload = makePayload(); | ||
| const result = new YandexMetricaAddonMessageProcessor().apply(payload); | ||
|
|
||
| expect(ym).not.toHaveBeenCalled(); | ||
| expect(result.addons).toEqual({}); | ||
| }); | ||
|
|
||
| it('should preserve the queue position when an earlier counter is invalid', () => { | ||
| const ym = vi.fn((_counterId, _method, callback) => callback('client-id')) as YandexMetricaMock; | ||
|
|
||
| ym.a = [ | ||
| [456, 'init', { webvisor: false }], | ||
| [789, 'init', { webvisor: true }], | ||
| ]; | ||
| setYandexMetrica(ym); | ||
|
|
||
| const result = new YandexMetricaAddonMessageProcessor().apply(makePayload()); | ||
|
|
||
| expect(result.addons).toHaveProperty(YANDEX_METRICA_ADDON_KEY, { | ||
| ['2']: { | ||
| counterId: 789, | ||
| clientId: 'client-id', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should process no more than ten Yandex Metrica counters', () => { | ||
| const ym = vi.fn((counterId, _method, callback) => callback(`client-${counterId}`)) as YandexMetricaMock; | ||
|
|
||
| ym.a = Array.from({ length: 11 }, (_, index) => [ | ||
| 100 + index, | ||
| 'init', | ||
| { webvisor: true }, | ||
| ]); | ||
| setYandexMetrica(ym); | ||
|
|
||
| const result = new YandexMetricaAddonMessageProcessor().apply(makePayload()); | ||
| const identifiers = result.addons[YANDEX_METRICA_ADDON_KEY] as Record<string, unknown>; | ||
|
|
||
| expect(ym).toHaveBeenCalledTimes(10); | ||
| expect(identifiers).toHaveProperty('10', { | ||
| counterId: 109, | ||
| clientId: 'client-109', | ||
| }); | ||
| expect(identifiers).not.toHaveProperty('11'); | ||
| }); | ||
|
|
||
| it('should attach identifiers only after getClientID resolves', () => { | ||
| let resolveClientId: ((clientId: unknown) => void) | undefined; | ||
| const ym = vi.fn((_counterId, _method, callback) => { | ||
| resolveClientId = callback; | ||
| }) as YandexMetricaMock; | ||
|
|
||
| ym.a = [[456, 'init', { webvisor: true }]]; | ||
| setYandexMetrica(ym); | ||
|
|
||
| const processor = new YandexMetricaAddonMessageProcessor(); | ||
|
|
||
| expect(processor.apply(makePayload()).addons).toEqual({}); | ||
|
|
||
| resolveClientId?.('client-id'); | ||
|
|
||
| expect(processor.apply(makePayload()).addons).toHaveProperty(YANDEX_METRICA_ADDON_KEY, { | ||
| ['1']: { | ||
| counterId: 456, | ||
| clientId: 'client-id', | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.