Skip to content

Commit

Permalink
examples: Adds with-google-tag-manager example that uses `@next/thi…
Browse files Browse the repository at this point in the history
…rd-parties` (vercel#57364)
  • Loading branch information
housseindjirdeh authored Aug 15, 2024
1 parent 327bd94 commit f87cf5c
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/with-google-tag-manager/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID=
36 changes: 36 additions & 0 deletions examples/with-google-tag-manager/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
35 changes: 35 additions & 0 deletions examples/with-google-tag-manager/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Example app using Google Tag Manager

This example shows how to include Google Tag Manager in a Next.js application using `@next/third-parties`. The GTM container is instantiated in `layout.js` and the `sendGTMEvent` function is fired in the `EventButton` client component.

## Deploy your own

Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-google-tag-manager&project-name=with-google-tag-manager&repository-name=with-google-tag-manager)

## How to use

Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:

```bash
npx create-next-app --example with-google-tag-manager with-google-tag-manager-app
```

```bash
yarn create next-app --example with-google-tag-manager with-google-tag-manager-app
```

```bash
pnpm create next-app --example with-google-tag-manager with-google-tag-manager-app
```

Next, copy the `.env.local.example` file in this directory to `.env.local` (which will be ignored by Git):

```bash
cp .env.local.example .env.local
```

Set the `NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID` variable in `.env.local` to match your Google Tag Manager ID.

Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
15 changes: 15 additions & 0 deletions examples/with-google-tag-manager/app/components/EventButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use client'

import { sendGTMEvent } from '@next/third-parties/google'

export function EventButton() {
return (
<div>
<button
onClick={() => sendGTMEvent({ event: 'buttonClicked', value: 'xyz' })}
>
Send Event
</button>
</div>
)
}
12 changes: 12 additions & 0 deletions examples/with-google-tag-manager/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { GoogleTagManager } from '@next/third-parties/google'

const GTM_ID = process.env.NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID

export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
<GoogleTagManager gtmId={GTM_ID} />
</html>
)
}
9 changes: 9 additions & 0 deletions examples/with-google-tag-manager/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { EventButton } from './components/EventButton'

export default function Page() {
return (
<div>
<EventButton />
</div>
)
}
14 changes: 14 additions & 0 deletions examples/with-google-tag-manager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"@next/third-parties": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}

0 comments on commit f87cf5c

Please sign in to comment.