11import { uiFactory } from '../uiFactory/uiFactory' ;
22
3+ /**
4+ * Interface for a counter that provides methods for tracking metrics.
5+ *
6+ * @method hit - Tracks a hit event with optional arguments. https://yandex.ru/support/metrica/ru/objects/hit
7+ * @method params - Sets parameters for the counter with optional arguments. https://yandex.ru/support/metrica/ru/objects/params-method
8+ * @method userParams - Sets user-specific parameters for the counter with optional arguments. https://yandex.ru/support/metrica/ru/objects/user-params
9+ * @method reachGoal - Tracks a goal achievement event with optional arguments. https://yandex.ru/support/metrica/ru/objects/reachgoal
10+ */
311export interface Counter {
412 hit : ( ...args : unknown [ ] ) => void ;
513 params : ( ...args : unknown [ ] ) => void ;
@@ -9,6 +17,14 @@ export interface Counter {
917
1018const yaMetricaMap = uiFactory . yaMetricaMap ;
1119
20+ /**
21+ * A fake implementation of a counter metric for Yandex.Metrica.
22+ * This class is used when the actual Yandex.Metrica counter is not defined,
23+ * and it provides a warning message the first time any of its methods are called.
24+ *
25+ * @property name - The name of the counter.
26+ * @property warnShown - Flag to indicate if the warning has been shown.
27+ */
1228class FakeMetrica implements Counter {
1329 name : string ;
1430
@@ -36,12 +52,19 @@ class FakeMetrica implements Counter {
3652
3753 private warnOnce ( ) {
3854 if ( ! this . warnShown ) {
39- console . warn ( `YaMetrica counter "${ this . name } " is not defined\n` ) ;
55+ console . warn ( `Yandex.Metrica counter "${ this . name } " is not defined\n` ) ;
4056 this . warnShown = true ;
4157 }
4258 }
4359}
4460
61+ /**
62+ * Retrieves a Yandex Metrica instance by name from the global window object.
63+ * If no instance is found for the given name, returns a FakeMetrica instance instead.
64+ *
65+ * @param name The name of the metrica to retrieve
66+ * @returns The Yandex Metrica instance if found, otherwise a FakeMetrica instance
67+ */
4568export function getMetrica ( name : string ) {
4669 const yaMetricaId = yaMetricaMap ?. [ name ] ;
4770 const metricaInstance = yaMetricaId
0 commit comments