-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add metrics infrustructure to ui #2698
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import {uiFactory} from '../uiFactory/uiFactory'; | ||
|
|
||
| /** | ||
| * Interface for a counter that provides methods for tracking metrics. | ||
| * | ||
| * @method hit - Tracks a hit event with optional arguments. https://yandex.ru/support/metrica/ru/objects/hit | ||
| * @method params - Sets parameters for the counter with optional arguments. https://yandex.ru/support/metrica/ru/objects/params-method | ||
| * @method userParams - Sets user-specific parameters for the counter with optional arguments. https://yandex.ru/support/metrica/ru/objects/user-params | ||
| * @method reachGoal - Tracks a goal achievement event with optional arguments. https://yandex.ru/support/metrica/ru/objects/reachgoal | ||
| */ | ||
| export interface Counter { | ||
| hit: (...args: unknown[]) => void; | ||
| params: (...args: unknown[]) => void; | ||
| userParams: (...args: unknown[]) => void; | ||
| reachGoal: (...args: unknown[]) => void; | ||
| } | ||
|
|
||
| const yaMetricaMap = uiFactory.yaMetricaMap; | ||
|
|
||
| /** | ||
| * A fake implementation of a counter metric for Yandex.Metrica. | ||
| * This class is used when the actual Yandex.Metrica counter is not defined, | ||
| * and it provides a warning message the first time any of its methods are called. | ||
| * | ||
| * @property name - The name of the counter. | ||
| * @property warnShown - Flag to indicate if the warning has been shown. | ||
| */ | ||
| class FakeMetrica implements Counter { | ||
| name: string; | ||
|
|
||
| private warnShown = false; | ||
|
|
||
| constructor(name: string) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| hit() { | ||
| this.warnOnce(); | ||
| } | ||
|
|
||
| params() { | ||
| this.warnOnce(); | ||
| } | ||
|
|
||
| userParams() { | ||
| this.warnOnce(); | ||
| } | ||
|
|
||
| reachGoal() { | ||
| this.warnOnce(); | ||
| } | ||
|
|
||
| private warnOnce() { | ||
| if (!this.warnShown) { | ||
| console.warn(`Yandex.Metrica counter "${this.name}" is not defined\n`); | ||
| this.warnShown = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves a Yandex Metrica instance by name from the global window object. | ||
| * If no instance is found for the given name, returns a FakeMetrica instance instead. | ||
| * | ||
| * @param name The name of the metrica to retrieve | ||
| * @returns The Yandex Metrica instance if found, otherwise a FakeMetrica instance | ||
| */ | ||
| export function getMetrica(name: string) { | ||
| const yaMetricaId = yaMetricaMap?.[name]; | ||
| const metricaInstance = yaMetricaId | ||
| ? (window[`yaCounter${yaMetricaId}`] as Counter) | ||
| : undefined; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Metrica ID Check Fails on ZeroThe |
||
|
|
||
| return metricaInstance ?? new FakeMetrica(name); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The getMetrica function lacks documentation. Consider adding JSDoc to explain its purpose, parameters, and return value, especially the fallback behavior to FakeMetrica.