Skip to content
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
merged 11 commits into from
Aug 30, 2023
4 changes: 2 additions & 2 deletions examples/with-segment-analytics/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Example app with analytics
# With Segment Analytics

This example shows how to use Next.js along with [Segment Analytics](https://segment.com). A [custom app](https://nextjs.org/docs/advanced-features/custom-app) is used to inject the [Segment Analytics.js snippet](https://github.com/segmentio/snippet). The server and client-side call the [Page API](https://segment.com/docs/connections/spec/page/), while components call the [Track API](https://segment.com/docs/connections/spec/track/) on user actions (Refer to [`contact.js`](https://github.com/vercel/next.js/blob/canary/examples/with-segment-analytics/pages/contact.js)).
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 main app [layout](https://github.com/vercel/next.js/blob/canary/examples/with-segment-analytics/app/layout.tsx) includes a client component (analytics.tsx)[(https://github.com/vercel/next.js/blob/canary/examples/with-segment-analytics/components/analytics.tsx)] 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.js`](https://github.com/vercel/next.js/blob/canary/examples/with-segment-analytics/app/contact/page.tsx)).

## Deploy your own

Expand Down
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>
Expand Down
14 changes: 14 additions & 0 deletions examples/with-segment-analytics/app/contact/page.tsx
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>
)
}
21 changes: 21 additions & 0 deletions examples/with-segment-analytics/app/layout.tsx
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>
)
}
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>
Expand Down
17 changes: 0 additions & 17 deletions examples/with-segment-analytics/components/Page.js

This file was deleted.

27 changes: 27 additions & 0 deletions examples/with-segment-analytics/components/analytics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use client'

import { useEffect } from 'react'
import { usePathname, useSearchParams } from 'next/navigation'
import { AnalyticsBrowser } from '@segment/analytics-next'

// This write key is associated with https://segment.com/nextjs-example/sources/nextjs.
const DEFAULT_WRITE_KEY = 'NPsk1GimHq09s7egCUlv7D0tqtUAU5wa'

const analytics = AnalyticsBrowser.load({
writeKey: process.env.NEXT_PUBLIC_SEGMENT_WRITE_KEY || DEFAULT_WRITE_KEY,
})

export function useSegment() {
return analytics;
lukebussey marked this conversation as resolved.
Show resolved Hide resolved
}

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
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
'use client'
import { useState } from 'react'
import { useSegment } from '@/components/analytics'

const Contact = () => {
export default function Form() {
const analytics = useSegment();
const [message, setMessage] = useState('')

const handleSubmit = (e) => {
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault()
global.analytics.track('Form Submitted', {
analytics.track('Form Submitted', {
message,
})
setMessage('')
}

return (
<div>
<h1>This is the Contact page</h1>
<>
<form onSubmit={handleSubmit}>
<label>
<span>Message:</span>
Expand Down Expand Up @@ -41,7 +43,6 @@ const Contact = () => {
display: block;
}
`}</style>
</div>
</>
)
}
export default Contact
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() {
Expand Down
7 changes: 6 additions & 1 deletion examples/with-segment-analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
"start": "next start"
},
"dependencies": {
"@segment/snippet": "^4.0.1",
"@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"
}
}
36 changes: 0 additions & 36 deletions examples/with-segment-analytics/pages/_app.js

This file was deleted.

28 changes: 28 additions & 0 deletions examples/with-segment-analytics/tsconfig.json
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"]
}