Skip to content

Commit

Permalink
feat(third-parties): add gtmScriptUrl option for GTM server-side tagg…
Browse files Browse the repository at this point in the history
…ing (#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: #63767
Closes: #68531
Closes: #67161
Closes: #61145
Closes: #58262

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
kutsan and ijjk committed Sep 24, 2024
1 parent aecc50e commit 6f64af6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ Refer to the Tag Manager [developer
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.

#### 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 to pass to the Google Tag Manager. For a full list of options, read the [Google Tag Manager
Expand All @@ -171,6 +176,7 @@ docs](https://developers.google.com/tag-platform/tag-manager/datalayer).
| Name | Type | Description |
| --------------- | -------- | ------------------------------------------------------------------------ |
| `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. |
| `dataLayerName` | Optional | Name of the data layer. Defaults to `dataLayer`. |
| `auth` | Optional | Value of authentication parameter (`gtm_auth`) for environment snippets. |
Expand Down
4 changes: 2 additions & 2 deletions packages/third-parties/src/google/gtm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let currDataLayerName = 'dataLayer'
export function GoogleTagManager(props: GTMParams) {
const {
gtmId,
gtmScriptUrl = 'https://www.googletagmanager.com/gtm.js',
dataLayerName = 'dataLayer',
auth,
preview,
Expand Down Expand Up @@ -53,8 +54,7 @@ export function GoogleTagManager(props: GTMParams) {
<Script
id="_next-gtm"
data-ntpc="GTM"
src={`https://www.googletagmanager.com/gtm.js?id=${gtmId}${gtmLayer}${gtmAuth}${gtmPreview}`}
nonce={nonce}
src={`${gtmScriptUrl}?id=${gtmId}${gtmLayer}${gtmAuth}${gtmPreview}`}
/>
</>
)
Expand Down
1 change: 1 addition & 0 deletions packages/third-parties/src/types/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type JSONValue =

export type GTMParams = {
gtmId: string
gtmScriptUrl?: string
dataLayer?: { [key: string]: JSONValue }
dataLayerName?: string
auth?: string
Expand Down

0 comments on commit 6f64af6

Please sign in to comment.