Capítulo 331 de 456
@next/third-parties provides optimized components/utilities for loading popular third-party scripts (Google Tag Manager, Google Analytics, Google Maps Embed, YouTube Embed) with minimal performance impact, using deferred/lazy loading strategies.
@next/third-parties/google: entry point for all Google integrations, imported as named exports.GoogleTagManager: component that instantiates a GTM container; fetches inline script after hydration.sendGTMEvent: pushes events into the GTM dataLayer; requires <GoogleTagManager /> present in a parent layout/page/component.GoogleAnalytics: component that loads GA4 via gtag.js after hydration; use only if GTM is not already handling analytics.sendGAEvent: pushes GA4 events into the dataLayer.GoogleMapsEmbed: lazy-loaded (via loading attribute) iframe wrapper for Google Maps Embed API.YouTubeEmbed: fast-loading YouTube embed built on lite-youtube-embed.import { GoogleTagManager } from '@next/third-parties/google'
export default function MyApp({ Component, pageProps }) {
return (
<>
<Component {...pageProps} />
<GoogleTagManager gtmId="GTM-XYZ" />
</>
)
}
_app para todas as rotas.import { sendGTMEvent } from '@next/third-parties/google'
export function EventButton() {
return (
<button onClick={() => sendGTMEvent({ event: 'buttonClicked', value: 'xyz' })}>
Send Event
</button>
)
}
dataLayer.import { GoogleMapsEmbed } from '@next/third-parties/google'
export default function Page() {
return (
<GoogleMapsEmbed apiKey="XYZ" height={200} width="100%" mode="place" q="Brooklyn+Bridge,New+York,NY" />
)
}
| Component | Required props | Notes |
|---|---|---|
GoogleTagManager | gtmId (ou gtmScriptUrl) | dataLayer, dataLayerName, auth, preview opcionais |
GoogleAnalytics | gaId | dataLayerName, debugMode, nonce opcionais |
GoogleMapsEmbed | apiKey, mode | height/width default auto; loading default lazy |
YouTubeEmbed | videoid | width/height default auto; params é query string (controls=0&start=10) |
GoogleAnalytics junto com GoogleTagManager: duplica coleta de dados; configure Analytics dentro do próprio GTM em vez de usar os dois componentes.sendGAEvent/sendGTMEvent sem o componente correspondente montado: a função não tem efeito se <GoogleAnalytics />/<GoogleTagManager /> não estiver em um componente pai.GoogleTagManager deve ser preferido a GoogleAnalytics quando GTM já está em uso, para evitar dados duplicados.latest ou canary.next/script: mecanismo genérico de carregamento de scripts que @next/third-parties usa internamente para otimizar terceiros específicos.