Skip to content
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
2 changes: 2 additions & 0 deletions src/types/window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ interface Window {
systemSettings?: import('../services/settings').SettingsObject;

api: import('../services/api/index').YdbEmbeddedAPI;

[key: `yaCounter${number}`]: any;
}
1 change: 1 addition & 0 deletions src/uiFactory/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface UIFactory<H extends string = CommonIssueType> {
countHealthcheckIssuesByType: (issueTrees: IssuesTree[]) => Record<H, number>;
};
hasAccess?: boolean;
yaMetricaMap?: Record<string, number>;
}

export type HandleCreateDB = (params: {clusterName: string}) => Promise<boolean>;
Expand Down
75 changes: 75 additions & 0 deletions src/utils/yaMetrica.ts
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;
}
}
}

Copy link

Copilot AI Aug 12, 2025

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.

Suggested change
/**
* Retrieves a YaMetrica counter instance by name.
*
* @param {string} name - The name of the YaMetrica counter to retrieve.
* @returns {Counter} The YaMetrica counter instance if found, otherwise a FakeMetrica instance.
* If the counter is not defined, FakeMetrica will log a warning once when its methods are called.
*/

Copilot uses AI. Check for mistakes.
/**
* 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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Metrica ID Check Fails on Zero

The getMetrica function's truthiness check on yaMetricaId (e.g., yaMetricaId ? ...) incorrectly treats 0 as falsy. Since 0 is a valid Yandex Metrica counter ID, this causes the function to return a FakeMetrica instance instead of the real window['yaCounter0']. The condition should use yaMetricaId !== undefined or yaMetricaId != null to correctly identify valid IDs.

Fix in Cursor Fix in Web


return metricaInstance ?? new FakeMetrica(name);
}
Loading