-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
(Example) Update with-segment-analytics to use segmentio/analytics-next and app layout #52327
Merged
leerob
merged 11 commits into
vercel:canary
from
lukebussey:update-with-segment-analytics
Aug 30, 2023
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0726675
Update example to use segmentio/analytics-next and NextJS app layout
lukebussey 11bff83
Implement useSegment hook
lukebussey df9efcb
Update to use hook
lukebussey 04f3d4d
Update to remove hook and just return analytics
lukebussey 9a9fa30
Update readme
lukebussey 9d3c9d7
Add pages router example
lukebussey 5216458
Update to pages router example to use routeChangeComplete
lukebussey 6d41939
Update to match @kylemh example
lukebussey 11ee51d
Merge branch 'canary' into update-with-segment-analytics
leerob b46dfd0
Fix lint error
lukebussey 6d80cd9
Merge branch 'canary' into update-with-segment-analytics
leerob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
examples/with-segment-analytics-pages-router/.env.local.example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// This write key is associated with https://segment.com/nextjs-example/sources/nextjs. | ||
NEXT_PUBLIC_SEGMENT_WRITE_KEY=NPsk1GimHq09s7egCUlv7D0tqtUAU5wa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# With Segment Analytics (Pages Router) | ||
|
||
This example shows how to use Next.js along with [Segment Analytics](https://segment.com) using [segmentio/analytics-next](https://github.com/segmentio/analytics-next). The custom app [component](https://github.com/vercel/next.js/blob/canary/examples/with-segment-analytics-pages-router/pages/_app_.tsx) includes a component (analytics.tsx)[(https://github.com/vercel/next.js/blob/canary/examples/with-segment-analytics-pages-router/components/analytics.tsx)] which loads Segment and also exports the `analytics` object which can be imported and used to call the [Track API](https://segment.com/docs/connections/spec/track/) on user actions (Refer to [`contact.tsx`](https://github.com/vercel/next.js/blob/canary/examples/with-segment-analytics-pages-router/pages/contact.tsx)). | ||
|
||
## Deploy your own | ||
|
||
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-segment-analytics-pages-router) | ||
|
||
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-segment-analytics-pages-router&project-name=with-segment-analytics-pages-router&repository-name=with-segment-analytics-pages-router) | ||
|
||
## 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-segment-analytics-pages-router with-segment-analytics-app | ||
``` | ||
|
||
```bash | ||
yarn create next-app --example with-segment-analytics-pages-router with-segment-analytics-app | ||
``` | ||
|
||
```bash | ||
pnpm create next-app --example with-segment-analytics-pages-router with-segment-analytics-app | ||
``` | ||
|
||
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)). |
13 changes: 13 additions & 0 deletions
13
examples/with-segment-analytics-pages-router/components/analytics.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useEffect } from 'react' | ||
import { useRouter } from 'next/router' | ||
import { analytics } from '@/lib/segment' | ||
|
||
export default function Analytics() { | ||
const { asPath } = useRouter() | ||
|
||
useEffect(() => { | ||
analytics.page() | ||
}, [asPath]) | ||
|
||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
...th-segment-analytics/components/Header.js → ...lytics-pages-router/components/header.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import React from 'react' | ||
import Link from 'next/link' | ||
|
||
export default function Header() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { AnalyticsBrowser } from '@segment/analytics-next' | ||
|
||
export const analytics = AnalyticsBrowser.load({ | ||
writeKey: process.env.NEXT_PUBLIC_SEGMENT_WRITE_KEY!, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"dev": "next", | ||
"build": "next build", | ||
"start": "next start" | ||
}, | ||
"dependencies": { | ||
"@segment/analytics-next": "1.53.0", | ||
"next": "latest", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "20.4.0", | ||
"@types/react": "18.2.14", | ||
"typescript": "5.1.6" | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
examples/with-segment-analytics-pages-router/pages/_app.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { AppProps } from 'next/app' | ||
import { Inter } from 'next/font/google' | ||
import Header from '@/components/header' | ||
import Analytics from '@/components/analytics' | ||
|
||
const inter = Inter({ subsets: ['latin'] }) | ||
|
||
export default function MyApp({ Component, pageProps }: AppProps) { | ||
return ( | ||
<main className={inter.className}> | ||
<Header /> | ||
<Component {...pageProps} /> | ||
<Analytics /> | ||
</main> | ||
) | ||
} |
14 changes: 14 additions & 0 deletions
14
examples/with-segment-analytics-pages-router/pages/about.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Head from 'next/head' | ||
|
||
export default function About() { | ||
return ( | ||
<> | ||
<Head> | ||
<title>About</title> | ||
</Head> | ||
<div> | ||
<h1>This is the About page</h1> | ||
</div> | ||
</> | ||
) | ||
} |
16 changes: 16 additions & 0 deletions
16
examples/with-segment-analytics-pages-router/pages/contact.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Head from 'next/head' | ||
import Form from '@/components/form' | ||
|
||
export default function Contact() { | ||
return ( | ||
<> | ||
<Head> | ||
<title>Contact</title> | ||
</Head> | ||
<div> | ||
<h1>This is the Contact page</h1> | ||
<Form /> | ||
</div> | ||
</> | ||
) | ||
} |
14 changes: 14 additions & 0 deletions
14
examples/with-segment-analytics-pages-router/pages/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Head from 'next/head' | ||
|
||
export default function Home() { | ||
return ( | ||
<> | ||
<Head> | ||
<title>Home</title> | ||
</Head> | ||
<div> | ||
<h1>This is the Home page</h1> | ||
</div> | ||
</> | ||
) | ||
} |
28 changes: 28 additions & 0 deletions
28
examples/with-segment-analytics-pages-router/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve", | ||
"incremental": true, | ||
"plugins": [ | ||
{ | ||
"name": "next" | ||
} | ||
], | ||
"paths": { | ||
"@/*": ["./*"] | ||
} | ||
}, | ||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], | ||
"exclude": ["node_modules"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// This write key is associated with https://segment.com/nextjs-example/sources/nextjs. | ||
NEXT_PUBLIC_SEGMENT_WRITE_KEY=NPsk1GimHq09s7egCUlv7D0tqtUAU5wa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...les/with-segment-analytics/pages/about.js → ...with-segment-analytics/app/about/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export const metadata = { | ||
title: 'About', | ||
} | ||
|
||
export default function About() { | ||
return ( | ||
<div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Form from '@/components/form' | ||
|
||
export const metadata = { | ||
title: 'Contact', | ||
} | ||
|
||
export default function Contact() { | ||
return ( | ||
<div> | ||
<h1>This is the Contact page</h1> | ||
<Form /> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Inter } from 'next/font/google' | ||
import Header from '@/components/header' | ||
import Analytics from '@/components/analytics' | ||
|
||
const inter = Inter({ subsets: ['latin'] }) | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
return ( | ||
<html> | ||
<body className={inter.className}> | ||
<Header /> | ||
{children} | ||
</body> | ||
<Analytics /> | ||
</html> | ||
) | ||
} |
4 changes: 4 additions & 0 deletions
4
...les/with-segment-analytics/pages/index.js → examples/with-segment-analytics/app/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export const metadata = { | ||
title: 'Home', | ||
} | ||
|
||
export default function Home() { | ||
return ( | ||
<div> | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use client' | ||
|
||
import { useEffect } from 'react' | ||
import { usePathname, useSearchParams } from 'next/navigation' | ||
import { analytics } from '@/lib/segment' | ||
|
||
export default function Analytics() { | ||
lukebussey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const pathname = usePathname() | ||
const searchParams = useSearchParams() | ||
|
||
useEffect(() => { | ||
analytics.page() | ||
}, [pathname, searchParams]) | ||
|
||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use client' | ||
|
||
import { useState } from 'react' | ||
import { analytics } from '@/lib/segment' | ||
|
||
export default function Form() { | ||
const [message, setMessage] = useState('') | ||
|
||
const handleSubmit = (e: React.FormEvent) => { | ||
e.preventDefault() | ||
analytics.track('Form Submitted', { | ||
message, | ||
}) | ||
setMessage('') | ||
} | ||
|
||
return ( | ||
<> | ||
<form onSubmit={handleSubmit}> | ||
<label> | ||
<span>Message:</span> | ||
<textarea | ||
onChange={(e) => setMessage(e.target.value)} | ||
value={message} | ||
/> | ||
</label> | ||
<button type="submit">submit</button> | ||
</form> | ||
|
||
<style jsx>{` | ||
label span { | ||
display: block; | ||
margin-bottom: 12px; | ||
} | ||
|
||
textarea { | ||
min-width: 300px; | ||
min-height: 120px; | ||
} | ||
|
||
button { | ||
margin-top: 12px; | ||
display: block; | ||
} | ||
`}</style> | ||
</> | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://nextjs.org/docs/pages/api-reference/functions/use-router#routerevents
This might be better to do because
asPath
might represent the actual file path instead of a changed route. If you want to keep usingasPath
, I'd conditionally callpage()
only ifrouter.isReady
is true.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you get the initial page view using router events? It only seems to fire after the initial page view.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated example. Alternatively, you can stick with
asPath
, I'd just do so conditionally afterrouter.isReady
is true.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got it working with
routeChangeComplete
thanks @kylemh