Skip to content

Feature/add url to google tag manager #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: canary
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -116,6 +116,16 @@ export default function Page() {
```

</PagesOnly>
#### Using a custom server URL

If you have a server-side tag manager deployment, you can customize the URL by passing in a `url` parameter. The component uses `https://www.googletagamanager.com` by default.

```jsx
import { GoogleTagManager } from '@next/third-parties/google'

<GoogleTagManager gtmId="GTM-XYZ" url="https://tag-manager-server.xyz" />
}
```

#### Sending Events

6 changes: 3 additions & 3 deletions packages/third-parties/src/google/gtm.tsx
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import type { GTMParams } from '../types/google'
let currDataLayerName: string | undefined = undefined

export function GoogleTagManager(props: GTMParams) {
const { gtmId, dataLayerName = 'dataLayer', auth, preview, dataLayer } = props
const { gtmId, dataLayerName = 'dataLayer', auth, preview, dataLayer, url } = props

if (currDataLayerName === undefined) {
currDataLayerName = dataLayerName
@@ -17,7 +17,7 @@ export function GoogleTagManager(props: GTMParams) {
const gtmLayer = dataLayerName !== 'dataLayer' ? `&l=${dataLayerName}` : ''
const gtmAuth = auth ? `&gtm_auth=${auth}` : ''
const gtmPreview = preview ? `&gtm_preview=${preview}&gtm_cookies_win=x` : ''

const gtmUrl = url ? url : 'https://www.googletagmanager.com'
useEffect(() => {
// performance.mark is being used as a feature use signal. While it is traditionally used for performance
// benchmarking it is low overhead and thus considered safe to use in production and it is a widely available
@@ -47,7 +47,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}`}
src={`${gtmUrl}/gtm.js?id=${gtmId}${gtmLayer}${gtmAuth}${gtmPreview}`}
/>
</>
)
1 change: 1 addition & 0 deletions packages/third-parties/src/types/google.ts
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ export type GTMParams = {
dataLayerName?: string
auth?: string
preview?: string
url?: string
}

export type GAParams = {