Files
interview-demo-code/frontend/spa/src/utils/yaMetrika.js
Nikita Kiselev 393bbb286b
Some checks failed
Telegram Mini App Shop Builder / Compute version metadata (push) Has been cancelled
Telegram Mini App Shop Builder / Run Frontend tests (push) Has been cancelled
Telegram Mini App Shop Builder / Run Backend tests (push) Has been cancelled
Telegram Mini App Shop Builder / Run PHP_CodeSniffer (push) Has been cancelled
Telegram Mini App Shop Builder / Build module. (push) Has been cancelled
Telegram Mini App Shop Builder / release (push) Has been cancelled
Squashed commit message
2026-03-11 23:00:59 +03:00

31 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {useYaMetrikaStore} from "@/stores/yaMetrikaStore.js";
function getMetrikaId() {
// Пробуем найти все элементы <script> с mc.yandex.ru
const scripts = Array.from(document.scripts);
for (const s of scripts) {
if (s.src.includes('mc.yandex.ru/metrika/tag.js')) {
const match = s.src.match(/id=(\d+)/);
if (match) return match[1];
}
}
return null;
}
export function injectYaMetrika() {
const script = document.createElement('script');
script.src = '/index.php?route=extension/tgshop/handle/ya_metrika';
// script.async = true;
document.head.appendChild(script);
console.debug('[ym] Yandex Metrika injected to the page.');
script.onload = () => {
window.YA_METRIKA_ID = getMetrikaId();
console.debug('[ym] Detected Yandex.Metrika ID:', window.YA_METRIKA_ID);
const yaMetrika = useYaMetrikaStore();
yaMetrika.initUserParams();
window.dataLayer = window.dataLayer || [];
yaMetrika.processQueue();
}
}