Skip to content
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

Добавить js метод, для получения id счётчика #7

Open
kernusr opened this issue Apr 7, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@kernusr
Copy link
Contributor

kernusr commented Apr 7, 2021

Код, котрый дождётся метрику и вернёт номер счётчика

/** Ожидание загрузки счетчика Яндекс.Метрики
 * @param {?(number|string)} ymCounterNum - номер счетчика, если известен
 * @param {function} callback - получает аргументами объект и номер счетчика
 * @param {number} interval - интервал проверки готовности счетчика
 */
function waitForYm(ymCounterNum, callback, interval) {
  if (!callback) return;
  if (!ymCounterNum) {
    let metrikaObj  = (window.Ya && (window.Ya.Metrika || window.Ya.Metrika2)) || null;
    ymCounterNum = (metrikaObj && metrikaObj.counters && (metrikaObj.counters() || [0])[0].id) || 0;
  }
  let ymCounterObj = window['yaCounter' + ymCounterNum] || null;
  if (ymCounterObj) return (callback(ymCounterObj, ymCounterNum), undefined);
  setTimeout(function () { waitForYm(ymCounterNum, callback, interval); }, interval || 250);
}
@kernusr kernusr added the enhancement New feature or request label Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant