diff --git a/app/api/gtm/[id]/route.ts b/app/api/gtm/[id]/route.ts deleted file mode 100644 index cf7852c..0000000 --- a/app/api/gtm/[id]/route.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { NextRequest, NextResponse } from 'next/server'; - -export async function GET( - request: NextRequest, - { params }: { params: Promise<{ id: string }> } -) { - const { id } = await params; - - try { - const response = await fetch( - `https://www.googletagmanager.com/gtag/js?id=${id}`, - { - headers: { - 'User-Agent': request.headers.get('user-agent') || '', - }, - } - ); - - if (!response.ok) { - return new NextResponse('Script not found', { status: 404 }); - } - - const scriptContent = await response.text(); - - // Modificar URLs internas do script para usar o proxy - const modifiedScript = scriptContent - .replace( - /https?:\/\/[^\/]*google-analytics\.com\/g\/collect/g, - '/api/metrics' - ) - .replace( - /https?:\/\/[^\/]*google-analytics\.com/g, - '/api/metrics' - ); - - return new NextResponse(modifiedScript, { - status: 200, - headers: { - 'Content-Type': 'application/javascript', - 'Cache-Control': 'public, max-age=3600', - }, - }); - } catch (error) { - console.error('GTM proxy error:', error); - return new NextResponse('Proxy error', { status: 500 }); - } -} diff --git a/app/api/metrics/route.ts b/app/api/metrics/route.ts deleted file mode 100644 index 22d7981..0000000 --- a/app/api/metrics/route.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { NextRequest, NextResponse } from 'next/server'; - -export async function GET(request: NextRequest) { - const searchParams = request.nextUrl.searchParams; - const url = new URL('https://www.google-analytics.com/g/collect'); - - // Copiar todos os parâmetros da query string - searchParams.forEach((value, key) => { - url.searchParams.append(key, value); - }); - - try { - const response = await fetch(url.toString(), { - method: 'GET', - headers: { - 'User-Agent': request.headers.get('user-agent') || '', - 'Accept-Language': request.headers.get('accept-language') || '', - }, - }); - - return new NextResponse(null, { - status: response.status, - headers: { - 'Cache-Control': 'no-cache, no-store, must-revalidate', - }, - }); - } catch (error) { - console.error('Google Analytics proxy error:', error); - return new NextResponse(null, { status: 200 }); // Retornar 200 para não quebrar o tracking - } -} - -export async function POST(request: NextRequest) { - const searchParams = request.nextUrl.searchParams; - const url = new URL('https://www.google-analytics.com/g/collect'); - - // Copiar todos os parâmetros da query string - searchParams.forEach((value, key) => { - url.searchParams.append(key, value); - }); - - try { - const body = await request.text(); - - const response = await fetch(url.toString(), { - method: 'POST', - headers: { - 'User-Agent': request.headers.get('user-agent') || '', - 'Accept-Language': request.headers.get('accept-language') || '', - 'Content-Type': 'text/plain', - }, - body: body || undefined, - }); - - return new NextResponse(null, { - status: response.status, - headers: { - 'Cache-Control': 'no-cache, no-store, must-revalidate', - }, - }); - } catch (error) { - console.error('Google Analytics proxy error:', error); - return new NextResponse(null, { status: 200 }); - } -} diff --git a/app/components/GoogleAnalytics.tsx b/app/components/GoogleAnalytics.tsx new file mode 100644 index 0000000..c4f09af --- /dev/null +++ b/app/components/GoogleAnalytics.tsx @@ -0,0 +1,30 @@ +'use client'; + +import Script from 'next/script'; + +const GA_MEASUREMENT_ID = 'G-QTT9QT0T3S'; + +export default function GoogleAnalytics() { + return ( + <> + + + ); +} diff --git a/app/layout.tsx b/app/layout.tsx index c609db4..c221798 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -3,6 +3,7 @@ import { Geist, Geist_Mono } from "next/font/google"; import Script from "next/script"; // Importação necessária para corrigir o erro de namespace e tipagem do children import { ReactNode } from "react"; +import GoogleAnalytics from "./components/GoogleAnalytics"; import "./globals.css"; const geistSans = Geist({ @@ -194,85 +195,7 @@ export default function RootLayout({ children }: Readonly) { data-session-replay="true" data-track-errors="true" /> - {/* Google Analytics - Interceptor para bypass de adblocker */} -