google-analytics.js 533 B

1234567891011121314151617
  1. export default ({ app }) => {
  2. // 在客户端(浏览器)中执行
  3. if (process.client) {
  4. // 只在生产环境加载 Google Analytics 脚本
  5. const script = document.createElement('script');
  6. script.async = true;
  7. script.src = 'https://www.googletagmanager.com/gtag/js?id=G-G4G8QQY1ZX';
  8. document.head.appendChild(script);
  9. window.dataLayer = window.dataLayer || [];
  10. function gtag() {
  11. window.dataLayer.push(arguments);
  12. }
  13. gtag('js', new Date());
  14. gtag('config', 'G-G4G8QQY1ZX');
  15. }
  16. };