MatomoTracker.tsx 753 B

123456789101112131415161718192021222324252627282930313233
  1. 'use client';
  2. import { useEffect } from 'react';
  3. interface MatomoTrackerProps {
  4. containerUrl?: string;
  5. }
  6. export default function MatomoTracker({ containerUrl }: MatomoTrackerProps) {
  7. useEffect(() => {
  8. if (!containerUrl) {
  9. console.warn('Matomo 容器 URL 未配置');
  10. return;
  11. }
  12. // 初始化 Matomo Tag Manager
  13. const _mtm = (window._mtm = window._mtm || []);
  14. _mtm.push({ 'mtm.startTime': new Date().getTime(), event: 'mtm.Start' });
  15. const d = document;
  16. const g = d.createElement('script');
  17. const s = d.getElementsByTagName('script')[0];
  18. g.async = true;
  19. g.src = containerUrl;
  20. if (s.parentNode) {
  21. s.parentNode.insertBefore(g, s);
  22. }
  23. }, [containerUrl]);
  24. return null;
  25. }