analytics.ts 780 B

123456789101112131415161718192021222324252627282930
  1. export const trackEvent = (
  2. category: string,
  3. action: string,
  4. label?: string,
  5. value?: number,
  6. ) => {
  7. try {
  8. // Uncomment the next line to track locally
  9. // console.log("Track Event", { category, action, label, value });
  10. if (typeof window === "undefined" || process.env.JEST_WORKER_ID) {
  11. return;
  12. }
  13. if (process.env.REACT_APP_GOOGLE_ANALYTICS_ID && window.gtag) {
  14. window.gtag("event", action, {
  15. event_category: category,
  16. event_label: label,
  17. value,
  18. });
  19. }
  20. // MATOMO event tracking _paq must be same as the one in index.html
  21. if (window._paq) {
  22. window._paq.push(["trackEvent", category, action, label, value]);
  23. }
  24. } catch (error) {
  25. console.error("error during analytics", error);
  26. }
  27. };