๐ŸŽ‰ berenickt ๋ธ”๋กœ๊ทธ์— ์˜จ ๊ฑธ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๐ŸŽ‰
Front
NextJs
Third Party Libraries

Third Party Libraries

@next/third-parties is a library that provides a collection of components and utilities that improve the performance and developer experience of loading popular third-party libraries in your Next.js application.

Good to know: @next/third-parties is a new experimental library that is still under active development. Weโ€™re currently working on adding more third-party integrations.

All third-party integrations provided by @next/third-parties have been optimized for performance and ease of use.


Getting Started

To get started, you must install the @next/third-parties library:

Terminal
1
npm install @next/third-parties

Google Third-Parties

All supported third-party libraries from Google can be imported from @next/third-parties/google.

Google Tag Manager

The GoogleTagManager component can be used to instantiate a Google Tag Manager container to your page. By default, it fetches the original inline script after hydration occurs on the page.

To load Google Tag Manager for all routes, include the component directly in your custom _app:

pages/_app.js
1
import { GoogleTagManager } from '@next/third-parties/google'
2
3
export default function MyApp({ Component, pageProps }) {
4
return (
5
<>
6
<Component {...pageProps} />
7
<GoogleTagManager gtmId="GTM-XYZ" />
8
</>
9
)
10
}

To load Google Tag Manager for a single route, include the component in your page file:

pages/index.js
1
import { GoogleTagManager } from '@next/third-parties/google'
2
3
export default function Page() {
4
return <GoogleTagManager gtmId="GTM-XYZ" />
5
}

Sending Events

The sendGTMEvent function can be used to track user interactions on your page by sending events using the dataLayer object. For this function to work, the <GoogleTagManager /> component must be included in either a parent layout, page, or component, or directly in the same file.

app/page.js
1
'use client'
2
3
import { sendGTMEvent } from '@next/third-parties/google'
4
5
export function EventButton() {
6
return (
7
<div>
8
<button onClick={() => sendGTMEvent({ event: 'buttonClicked', value: 'xyz' })}>Send Event</button>
9
</div>
10
)
11
}

Refer to the Tag Manager developer documentation to learn about the different variables and events that can be passed into the function.


Options

Options to pass to the Google Tag Manager. For a full list of options, read the Google Tag Manager docs.

NameTypeDescription
gtmIdRequiredYour GTM container id.
dataLayerOptionalData layer array to instantiate the container with. Defaults to an empty array.
dataLayerNameOptionalName of the data layer. Defaults to dataLayer.
authOptionalValue of authentication parameter (gtm_auth) for environment snippets.
previewOptionalValue of preview parameter (gtm_preview) for environment snippets.

Google Analytics

The GoogleAnalytics component can be used to include Google Analytics 4 to your page via the Google tag (gtag.js). By default, it fetches the original scripts after hydration occurs on the page.

Recommendation: If Google Tag Manager is already included in your application, you can configure Google Analytics directly using it, rather than including Google Analytics as a separate component. Refer to the documentation to learn more about the differences between Tag Manager and gtag.js.

To load Google Analytics for all routes, include the component directly in your root layout:

app/layout.tsx
1
import { GoogleAnalytics } from '@next/third-parties/google'
2
3
export default function RootLayout({
4
children,
5
}: {
6
children: React.ReactNode
7
}) {
8
return (
9
<html lang="en">
10
<body>{children}</body>
11
<GoogleAnalytics gaId="GA-XYZ" />
12
</html>
13
)
14
}

To load Google Analytics for a single route, include the component in your page file:

app/page.js
1
import { GoogleAnalytics } from '@next/third-parties/google'
2
3
export default function Page() {
4
return <GoogleAnalytics gaId="GA-XYZ" />
5
}

Sending Events

The sendGAEvent function can be used to measure user interactions on your page by sending events using the dataLayer object. For this function to work, the <GoogleAnalytics /> component must be included in either a parent layout, page, or component, or directly in the same file.

app/page.js
1
'use client'
2
3
import { sendGAEvent } from '@next/third-parties/google'
4
5
export function EventButton() {
6
return (
7
<div>
8
<button onClick={() => sendGAEvent({ event: 'buttonClicked', value: 'xyz' })}>Send Event</button>
9
</div>
10
)
11
}

Refer to the Google Analytics developer documentation to learn more about event parameters.


Options

Options to pass to the <GoogleAnalytics> component.

NameTypeDescription
gaIdRequiredYour Google tag id.
dataLayerNameOptionalName of the data layer. Defaults to dataLayer.

Google Maps Embed

The GoogleMapsEmbed component can be used to add a Google Maps Embed to your page. By default, it uses the loading attribute to lazy-load the embed below the fold.

app/page.js
1
import { GoogleMapsEmbed } from '@next/third-parties/google'
2
3
export default function Page() {
4
return (
5
<GoogleMapsEmbed
6
apiKey="XYZ" //
7
height={200}
8
width="100%"
9
mode="place"
10
q="Brooklyn+Bridge,New+York,NY"
11
/>
12
)
13
}

Options

Options to pass to the Google Maps Embed. For a full list of options, read the Google Map Embed docs.

NameTypeDescription
apiKeyRequiredYour api key.
modeRequiredMap mode
heightOptionalHeight of the embed. Defaults to auto.
widthOptionalWidth of the embed. Defaults to auto.
styleOptionalPass styles to the iframe.
allowfullscreenOptionalProperty to allow certain map parts to go full screen.
loadingOptionalDefaults to lazy. Consider changing if you know your embed will be above the fold.
qOptionalDefines map marker location. This may be required depending on the map mode.
centerOptionalDefines the center of the map view.
zoomOptionalSets initial zoom level of the map.
maptypeOptionalDefines type of map tiles to load.
languageOptionalDefines the language to use for UI elements and for the display of labels on map tiles.
regionOptionalDefines the appropriate borders and labels to display, based on geo-political sensitivities.

YouTube Embed

The YouTubeEmbed component can be used to load and display a YouTube embed. This component loads faster by using lite-youtube-embed under the hood.

app/page.js
1
import { YouTubeEmbed } from '@next/third-parties/google'
2
3
export default function Page() {
4
return <YouTubeEmbed videoid="ogfYd705cRs" height={400} params="controls=0" />
5
}

Options

NameTypeDescription
videoidRequiredYouTube video id.
widthOptionalWidth of the video container. Defaults to auto
heightOptionalHeight of the video container. Defaults to auto
playlabelOptionalA visually hidden label for the play button for accessibility.
paramsOptionalThe video player params defined here. Params are passed as a query param string. Eg: params="controls=0&start=10&end=30"
styleOptionalUsed to apply styles to the video container.