Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
52 changes: 52 additions & 0 deletions src/utils/yaMetrica.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {uiFactory} from '../uiFactory/uiFactory';

export interface Counter {
hit: (...args: unknown[]) => void;
params: (...args: unknown[]) => void;
userParams: (...args: unknown[]) => void;
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 Counter interface and its methods lack documentation. Consider adding JSDoc comments to explain the purpose of each method and their parameters.

Suggested change
export interface Counter {
hit: (...args: unknown[]) => void;
params: (...args: unknown[]) => void;
userParams: (...args: unknown[]) => void;
/**
* Represents a Yandex.Metrica counter interface for tracking analytics events.
*/
export interface Counter {
/**
* Sends a page hit event to the counter.
* @param args - Arguments for the hit event (varies by implementation).
*/
hit: (...args: unknown[]) => void;
/**
* Sets custom parameters for the counter.
* @param args - Arguments for setting parameters (varies by implementation).
*/
params: (...args: unknown[]) => void;
/**
* Sets user-specific parameters for the counter.
* @param args - Arguments for setting user parameters (varies by implementation).
*/
userParams: (...args: unknown[]) => void;
/**
* Sends a goal achievement event to the counter.
* @param args - Arguments for the goal event (varies by implementation).
*/

Copilot uses AI. Check for mistakes.
reachGoal: (...args: unknown[]) => void;
}

const yaMetricaMap = uiFactory.yaMetricaMap;

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(`YaMetrica counter "${this.name}" is not defined\n`);
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 term 'YaMetrica' should be 'YandexMetrica' or 'Yandex.Metrica' for consistency with the official product name.

Suggested change
console.warn(`YaMetrica counter "${this.name}" is not defined\n`);
console.warn(`Yandex.Metrica counter "${this.name}" is not defined\n`);

Copilot uses AI. Check for mistakes.
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.
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