Unverified Commit 6f64af6e authored by Kutsan Kaplan's avatar Kutsan Kaplan Committed by GitHub

feat(third-parties): add gtmScriptUrl option for GTM server-side tagging (#65907)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

A new gtmScriptUrl option has been added to the third-parties GTM
package. This allows you to replace the default
https://www.googletagmanager.com/gtm.js URL with a custom gtm.js URL,
such as "https://gtm.example.com/gtm.js".

Closes: https://github.com/vercel/next.js/pull/63767
Closes: https://github.com/vercel/next.js/pull/68531
Closes: https://github.com/vercel/next.js/pull/67161
Closes: https://github.com/vercel/next.js/pull/61145
Closes: https://github.com/vercel/next.js/pull/58262

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent aecc50ec
...@@ -163,6 +163,11 @@ Refer to the Tag Manager [developer ...@@ -163,6 +163,11 @@ Refer to the Tag Manager [developer
documentation](https://developers.google.com/tag-platform/tag-manager/datalayer) to learn about the documentation](https://developers.google.com/tag-platform/tag-manager/datalayer) to learn about the
different variables and events that can be passed into the function. different variables and events that can be passed into the function.
#### Server-side Tagging
If you're using a server-side tag manager and serving `gtm.js` scripts from your tagging server you can
use `gtmScriptUrl` option to specify the URL of the script.
#### Options #### Options
Options to pass to the Google Tag Manager. For a full list of options, read the [Google Tag Manager Options to pass to the Google Tag Manager. For a full list of options, read the [Google Tag Manager
...@@ -171,6 +176,7 @@ docs](https://developers.google.com/tag-platform/tag-manager/datalayer). ...@@ -171,6 +176,7 @@ docs](https://developers.google.com/tag-platform/tag-manager/datalayer).
| Name | Type | Description | | Name | Type | Description |
| --------------- | -------- | ------------------------------------------------------------------------ | | --------------- | -------- | ------------------------------------------------------------------------ |
| `gtmId` | Required | Your GTM container ID. Usually starts with `GTM-`. | | `gtmId` | Required | Your GTM container ID. Usually starts with `GTM-`. |
| `gtmScriptUrl` | Optional | GTM script URL. Defaults to `https://www.googletagmanager.com/gtm.js`. |
| `dataLayer` | Optional | Data layer object to instantiate the container with. | | `dataLayer` | Optional | Data layer object to instantiate the container with. |
| `dataLayerName` | Optional | Name of the data layer. Defaults to `dataLayer`. | | `dataLayerName` | Optional | Name of the data layer. Defaults to `dataLayer`. |
| `auth` | Optional | Value of authentication parameter (`gtm_auth`) for environment snippets. | | `auth` | Optional | Value of authentication parameter (`gtm_auth`) for environment snippets. |
......
...@@ -10,6 +10,7 @@ let currDataLayerName = 'dataLayer' ...@@ -10,6 +10,7 @@ let currDataLayerName = 'dataLayer'
export function GoogleTagManager(props: GTMParams) { export function GoogleTagManager(props: GTMParams) {
const { const {
gtmId, gtmId,
gtmScriptUrl = 'https://www.googletagmanager.com/gtm.js',
dataLayerName = 'dataLayer', dataLayerName = 'dataLayer',
auth, auth,
preview, preview,
...@@ -53,8 +54,7 @@ export function GoogleTagManager(props: GTMParams) { ...@@ -53,8 +54,7 @@ export function GoogleTagManager(props: GTMParams) {
<Script <Script
id="_next-gtm" id="_next-gtm"
data-ntpc="GTM" data-ntpc="GTM"
src={`https://www.googletagmanager.com/gtm.js?id=${gtmId}${gtmLayer}${gtmAuth}${gtmPreview}`} src={`${gtmScriptUrl}?id=${gtmId}${gtmLayer}${gtmAuth}${gtmPreview}`}
nonce={nonce}
/> />
</> </>
) )
......
...@@ -14,6 +14,7 @@ type JSONValue = ...@@ -14,6 +14,7 @@ type JSONValue =
export type GTMParams = { export type GTMParams = {
gtmId: string gtmId: string
gtmScriptUrl?: string
dataLayer?: { [key: string]: JSONValue } dataLayer?: { [key: string]: JSONValue }
dataLayerName?: string dataLayerName?: string
auth?: string auth?: string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment