✨ Innovative and open-source visualization application that transforms various data formats, such as JSON, YAML, XML and CSV into interactive graphs.
feat: improve google analytics setup
AykutSaracc3fed760cd25
diff --git a/.env.development b/.env.developmentindex 80f5cf42..0cb97f13 100644--- a/.env.development+++ b/.env.development@@ -1,4 +1,5 @@NEXT_PUBLIC_BASE_URL=http://localhost:3000NEXT_PUBLIC_ALTOGIC_ENV_URL=https://jsoncrack.c5-na.altogic.comNEXT_PUBLIC_ALTOGIC_CLIENT_KEY=f1e92022789f4ccf91273a345ab2bdf8-NEXT_PUBLIC_PAYMENT_URL=https://herowand.lemonsqueezy.com/checkout/buy/ce30521f-c7cc-44f3-9435-995d3260ba22\ No newline at end of file+NEXT_PUBLIC_PAYMENT_URL=https://herowand.lemonsqueezy.com/checkout/buy/ce30521f-c7cc-44f3-9435-995d3260ba22+NEXT_PUBLIC_GA_ID=G-JKZEHMJBMH\ No newline at end of filediff --git a/.env.production b/.env.productionindex e22c2a8f..3714451d 100644--- a/.env.production+++ b/.env.production@@ -1,4 +1,5 @@NEXT_PUBLIC_BASE_URL=https://jsoncrack.comNEXT_PUBLIC_ALTOGIC_ENV_URL=https://jsoncrack.c5-na.altogic.comNEXT_PUBLIC_ALTOGIC_CLIENT_KEY=f1e92022789f4ccf91273a345ab2bdf8-NEXT_PUBLIC_PAYMENT_URL=https://herowand.lemonsqueezy.com/checkout/buy/ce30521f-c7cc-44f3-9435-995d3260ba22\ No newline at end of file+NEXT_PUBLIC_PAYMENT_URL=https://herowand.lemonsqueezy.com/checkout/buy/ce30521f-c7cc-44f3-9435-995d3260ba22+NEXT_PUBLIC_GA_ID=G-JKZEHMJBMH\ No newline at end of filediff --git a/src/components/GoogleAnalytics/index.tsx b/src/components/GoogleAnalytics/index.tsxindex 6362a31b..81058fd8 100644--- a/src/components/GoogleAnalytics/index.tsx+++ b/src/components/GoogleAnalytics/index.tsx@@ -1,5 +1,6 @@import React from "react";import Script from "next/script";+import * as gtag from "src/utils/gtag";const isDevelopment = process.env.NODE_ENV === "development";@@ -8,16 +9,22 @@ export const GoogleAnalytics: React.FC = () => {return (<>- <Script src="https://www.googletagmanager.com/gtag/js?id=G-JKZEHMJBMH" /><Script- id="google-analytics"+ strategy="afterInteractive"+ src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`}+ />+ <Script+ id="gtag-init"+ strategy="afterInteractive"dangerouslySetInnerHTML={{__html: `- window.dataLayer = window.dataLayer || [];- function gtag(){dataLayer.push(arguments);}- gtag('js', new Date());- gtag('config', 'G-JKZEHMJBMH');- `,+ window.dataLayer = window.dataLayer || [];+ function gtag(){dataLayer.push(arguments);}+ gtag('js', new Date());+ gtag('config', '${gtag.GA_TRACKING_ID}', {+ page_path: window.location.pathname,+ });+ `,}}/></>diff --git a/src/components/FlexBar/index.tsx b/src/components/HovercardAds/index.tsxsimilarity index 93%rename from src/components/FlexBar/index.tsxrename to src/components/HovercardAds/index.tsxindex 922c6f71..54917077 100644--- a/src/components/FlexBar/index.tsx+++ b/src/components/HovercardAds/index.tsx@@ -1,7 +1,7 @@import React from "react";import { useRouter } from "next/router";-export const HoverCard = () => {+export const HovercardAds = () => {const router = useRouter();React.useEffect(() => {diff --git a/src/pages/_app.tsx b/src/pages/_app.tsxindex 8e5d3cc7..7e0fa952 100644--- a/src/pages/_app.tsx+++ b/src/pages/_app.tsx@@ -1,5 +1,6 @@import React from "react";import type { AppProps } from "next/app";+import { useRouter } from "next/router";import { ThemeProvider } from "styled-components";import { MantineProvider } from "@mantine/core";import { QueryClient, QueryClientProvider } from "@tanstack/react-query";@@ -10,6 +11,7 @@ import GlobalStyle from "src/constants/globalStyle";import { lightTheme } from "src/constants/theme";import { ExternalMode } from "src/layout/DevMode";import { ModalController } from "src/layout/ModalController";+import * as gtag from "src/utils/gtag";const queryClient = new QueryClient({defaultOptions: {@@ -21,11 +23,17 @@ const queryClient = new QueryClient({});function JsonCrack({ Component, pageProps }: AppProps) {+ const router = useRouter();const [isReady, setReady] = React.useState(false);React.useEffect(() => {+ const handleRouteChange = (url: string) => gtag.pageview(url);+ router.events.on("routeChangeComplete", handleRouteChange);setReady(true);- }, []);+ return () => {+ router.events.off("routeChangeComplete", handleRouteChange);+ };+ }, [router.events]);if (isReady)return (diff --git a/src/pages/index.tsx b/src/pages/index.tsxindex 22d119ea..6c2146b6 100644--- a/src/pages/index.tsx+++ b/src/pages/index.tsx@@ -18,7 +18,7 @@ import {import { FaChevronRight } from "react-icons/fa";import { SiVisualstudiocode } from "react-icons/si";import { Typewriter } from "react-simple-typewriter";-import { HoverCard } from "src/components/FlexBar";+import { HovercardAds } from "src/components/HovercardAds";import { lightTheme } from "src/constants/theme";import { Footer } from "src/layout/Footer";import { Navbar } from "src/layout/Navbar";@@ -321,7 +321,7 @@ export const HomePage = () => {<StatsBanner /><Features /><HeroBottom />- <HoverCard />+ <HovercardAds /><Footer /></ThemeProvider>);diff --git a/src/store/useUser.ts b/src/store/useUser.tsindex e0a5a3e0..31c49d3d 100644--- a/src/store/useUser.ts+++ b/src/store/useUser.ts@@ -1,3 +1,4 @@+import * as Sentry from "@sentry/nextjs";import toast from "react-hot-toast";import { create } from "zustand";import { altogic } from "src/api/altogic";@@ -71,6 +72,7 @@ const useUser = create<UserStates & UserActions>()(set => ({altogic.auth.setSession(currentSession);const { data: premiumData } = await altogic.endpoint.get("/isPremium");+ Sentry.setUser({ id: user._id, email: user.email, username: user.name });set({ user: user as User, isAuthenticated: true, premium: premiumData.premium });} else if (new URLSearchParams(window.location.search).get("access_token")) {const { errors, user } = await altogic.auth.getAuthGrant();@@ -79,8 +81,12 @@ const useUser = create<UserStates & UserActions>()(set => ({return;}- const { data: premiumData } = await altogic.endpoint.get("/isPremium");- set({ user: user as User, isAuthenticated: true, premium: premiumData.premium });+ if (user) {+ const { data: premiumData } = await altogic.endpoint.get("/isPremium");++ Sentry.setUser({ id: user._id, email: user.email, username: user.name });+ set({ user: user as User, isAuthenticated: true, premium: premiumData.premium });+ }}},}));diff --git a/src/typings/global.d.ts b/src/typings/global.d.tsindex 78834c98..9d720c77 100644--- a/src/typings/global.d.ts+++ b/src/typings/global.d.ts@@ -1,3 +1,5 @@interface Window {_bsa: any;+ gtag: (...args: any[]) => void;+ dataLayer: Record<string, any>;}diff --git a/src/utils/gtag.ts b/src/utils/gtag.tsnew file mode 100644index 00000000..42dd54a8--- /dev/null+++ b/src/utils/gtag.ts@@ -0,0 +1,17 @@+export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_ID;++// https://developers.google.com/analytics/devguides/collection/gtagjs/pages+export const pageview = (url: string) => {+ window.gtag("config", GA_TRACKING_ID, {+ page_path: url,+ });+};++// https://developers.google.com/analytics/devguides/collection/gtagjs/events+export const event = ({ action, category, label, value }) => {+ window.gtag("event", action, {+ event_category: category,+ event_label: label,+ value: value,+ });+};diff --git a/tsconfig.json b/tsconfig.jsonindex 0bed3c28..8adba52a 100644--- a/tsconfig.json+++ b/tsconfig.json@@ -17,6 +17,6 @@"incremental": true,"noImplicitAny": false},- "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],+ "include": ["src", "next-env.d.ts",],"exclude": ["node_modules"]}