modified: serv_nginx/bbvue/src/main.js
add yandex and google metrix
This commit is contained in:
@@ -7,28 +7,91 @@ import App from './App.vue'
|
||||
import router from './router'
|
||||
import WriteLogo from './components/writeLogo.vue'
|
||||
|
||||
|
||||
// Яндекс.Метрика
|
||||
import { createYmaps } from 'vue-yandex-metrica'
|
||||
|
||||
// Google Analytics
|
||||
import { createGtag } from 'vue-gtag-next'
|
||||
|
||||
const app = createApp(App)
|
||||
|
||||
// Функция для загрузки Яндекс.Метрики
|
||||
function loadYandexMetrika() {
|
||||
return new Promise((resolve) => {
|
||||
if (window.ym) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
// Яндекс.Метрика
|
||||
app.use(createYmaps, {
|
||||
id: 104775055,
|
||||
router: router,
|
||||
})
|
||||
// Создаем скрипт Яндекс.Метрики
|
||||
const script = document.createElement('script');
|
||||
script.type = 'text/javascript';
|
||||
script.innerHTML = `
|
||||
(function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
|
||||
m[i].l=1*new Date();
|
||||
k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})
|
||||
(window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym");
|
||||
|
||||
ym(104775055, "init", {
|
||||
clickmap:true,
|
||||
trackLinks:true,
|
||||
accurateTrackBounce:true,
|
||||
webvisor:true
|
||||
});
|
||||
`;
|
||||
document.head.appendChild(script);
|
||||
|
||||
// Даем время на загрузку
|
||||
setTimeout(resolve, 500);
|
||||
});
|
||||
}
|
||||
|
||||
// Google Analytics
|
||||
app.use(createGtag, {
|
||||
property: {
|
||||
id: "G-r4bMCbf4zEwF7CJQu2XDn_9_G86ZL248xgWHavTY8iY"
|
||||
}
|
||||
})
|
||||
|
||||
// Функция для загрузки Google Analytics
|
||||
function loadGoogleAnalytics() {
|
||||
return new Promise((resolve) => {
|
||||
if (window.gtag) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
// Первый скрипт GA
|
||||
const script1 = document.createElement('script');
|
||||
script1.async = true;
|
||||
script1.src = 'https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX';
|
||||
document.head.appendChild(script1);
|
||||
|
||||
// Второй скрипт GA
|
||||
const script2 = document.createElement('script');
|
||||
script2.innerHTML = `
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag(){dataLayer.push(arguments);}
|
||||
gtag('js', new Date());
|
||||
gtag('config', 'G-r4bMCbf4zEwF7CJQu2XDn_9_G86ZL248xgWHavTY8iY');
|
||||
`;
|
||||
document.head.appendChild(script2);
|
||||
|
||||
setTimeout(resolve, 500);
|
||||
});
|
||||
}
|
||||
// Загружаем метрики и монтируем приложение
|
||||
Promise.all([loadYandexMetrika(), loadGoogleAnalytics()]).then(() => {
|
||||
// Отслеживание переходов по маршрутам
|
||||
router.afterEach((to) => {
|
||||
// Яндекс.Метрика
|
||||
if (window.ym && typeof window.ym === 'function') {
|
||||
setTimeout(() => {
|
||||
window.ym(104775055, 'hit', to.fullPath);
|
||||
}, 100);
|
||||
}
|
||||
|
||||
// Google Analytics
|
||||
if (window.gtag && typeof window.gtag === 'function') {
|
||||
setTimeout(() => {
|
||||
window.gtag('config', 'G-r4bMCbf4zEwF7CJQu2XDn_9_G86ZL248xgWHavTY8iY', {
|
||||
page_path: to.fullPath
|
||||
});
|
||||
}, 100);
|
||||
}
|
||||
});
|
||||
|
||||
app.use(router);
|
||||
app.mount('#app');
|
||||
});
|
||||
|
||||
// Регистрируем компоненты глобально
|
||||
app.component('WriteLogo', WriteLogo)
|
||||
|
||||
Reference in New Issue
Block a user