refactor(analytics): streamline Google Analytics integration and remove proxy routes

- Replaced the previous Google Analytics proxy routes with a direct integration in the layout component for improved performance and simplicity.
- Updated the Next.js configuration to include new rewrite rules for Google Analytics scripts and collection endpoints.
- Removed outdated proxy logic to enhance code clarity and maintainability.
This commit is contained in:
Evandro Fonseca Junior committed 2026-01-20 21:20:03 -03:00
1 parent 54d99fbffd
commit 525ef140fe
5 files changed
+40 -195

No files matched your search

+2 -79
View File
@@ -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<RootLayoutProps>) {
data-session-replay="true"
data-track-errors="true"
/>
{/* Google Analytics - Interceptor para bypass de adblocker */}
<Script
id="google-analytics-proxy-interceptor"
strategy="beforeInteractive"
dangerouslySetInnerHTML={{
__html: `
(function() {
// Interceptar fetch - método mais robusto
const originalFetch = window.fetch;
window.fetch = function(url, options) {
if (typeof url === 'string') {
// Interceptar google-analytics.com/g/collect
if (url.includes('google-analytics.com/g/collect')) {
const urlObj = new URL(url);
url = '/api/metrics' + urlObj.search;
}
// Interceptar outros endpoints do google-analytics.com
else if (url.includes('google-analytics.com')) {
url = url.replace(/https?:\\/\\/[^\\/]*google-analytics\\.com/g, '/api/metrics');
}
}
return originalFetch.call(this, url, options);
};
// Interceptar XMLHttpRequest
const originalOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url, ...rest) {
if (typeof url === 'string') {
if (url.includes('google-analytics.com/g/collect')) {
const urlObj = new URL(url);
url = '/api/metrics' + urlObj.search;
}
else if (url.includes('google-analytics.com')) {
url = url.replace(/https?:\\/\\/[^\\/]*google-analytics\\.com/g, '/api/metrics');
}
}
return originalOpen.call(this, method, url, ...rest);
};
// Interceptar sendBeacon (método usado pelo GA4)
const originalSendBeacon = navigator.sendBeacon;
navigator.sendBeacon = function(url, data) {
if (typeof url === 'string') {
if (url.includes('google-analytics.com/g/collect')) {
const urlObj = new URL(url);
return originalSendBeacon.call(this, '/api/metrics' + urlObj.search, data);
}
else if (url.includes('google-analytics.com')) {
url = url.replace(/https?:\\/\\/[^\\/]*google-analytics\\.com/g, '/api/metrics');
}
}
return originalSendBeacon.call(this, url, data);
};
})();
`,
}}
/>
{/* Google Analytics - Script principal via proxy */}
<Script
src="/api/gtm/G-QTT9QT0T3S"
strategy="afterInteractive"
async
/>
{/* Google Analytics - Inicialização */}
<Script
id="google-analytics-init"
strategy="afterInteractive"
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-QTT9QT0T3S', {
// Forçar uso de fetch em vez de imagem beacon
transport_type: 'fetch'
});
`,
}}
/>
<GoogleAnalytics />
</body>
</html>
);