chore(app): update Google Analytics ID in layout component

This commit is contained in:
Fatih Kadir Akın
2025-12-12 04:13:56 +03:00
parent dee7882a49
commit e73ebc3344
3 changed files with 39 additions and 11 deletions
+2
View File
@@ -24,3 +24,5 @@ NEXTAUTH_SECRET="your-super-secret-key-change-in-production"
# AI Search (optional - enable aiSearch in prompts.config.ts)
# OPENAI_API_KEY=your_openai_api_key
GOOGLE_ANALYTICS_ID="G-XXXXXXXXX"
+19
View File
@@ -1,4 +1,5 @@
import type { Metadata } from "next";
import Script from "next/script";
import { Inter, Noto_Sans_Arabic } from "next/font/google";
import { getMessages, getLocale } from "next-intl/server";
import { Providers } from "@/components/providers";
@@ -92,6 +93,24 @@ export default async function RootLayout({
return (
<html lang={locale} dir={isRtl ? "rtl" : "ltr"} suppressHydrationWarning className={themeClasses} style={themeStyles}>
<head>
{process.env.GOOGLE_ANALYTICS_ID && (
<>
<Script
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.GOOGLE_ANALYTICS_ID}`}
strategy="afterInteractive"
/>
<Script id="google-analytics" strategy="afterInteractive">
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${process.env.GOOGLE_ANALYTICS_ID}');
`}
</Script>
</>
)}
</head>
<body className={`${fontClasses} antialiased`}>
<Providers locale={locale} messages={messages} theme={config.theme} branding={config.branding}>
<div className="relative min-h-screen flex flex-col">
+17 -10
View File
@@ -124,19 +124,26 @@ async function buildAuthConfig() {
},
callbacks: {
async jwt({ token, user, trigger }: { token: any; user?: any; trigger?: string }) {
// On sign in, add user data to token
if (user) {
token.id = user.id;
token.role = (user as User).role;
token.username = (user as User).username;
token.locale = (user as User).locale;
// On sign in, look up the actual database user by email to ensure correct ID
if (user && user.email) {
const dbUser = await db.user.findUnique({
where: { email: user.email },
select: { id: true, role: true, username: true, locale: true },
});
if (dbUser) {
token.id = dbUser.id;
token.role = dbUser.role;
token.username = dbUser.username;
token.locale = dbUser.locale;
}
}
// Always verify user exists in database
if (token.id) {
// On subsequent requests, verify user exists and refresh data
if (token.id && !user) {
const dbUser = await db.user.findUnique({
where: { id: token.id as string },
select: { role: true, username: true, locale: true },
select: { id: true, role: true, username: true, locale: true },
});
// User no longer exists - invalidate token
@@ -144,7 +151,7 @@ async function buildAuthConfig() {
return null;
}
// Update token with latest user data
// Update token with latest user data on explicit update or if data missing
if (trigger === "update" || !token.username) {
token.role = dbUser.role;
token.username = dbUser.username;