Unverified Commit 761d72fb authored by Dominik Wilkowski's avatar Dominik Wilkowski Committed by GitHub

Adding a plausible example (#23814)

## Documentation / Examples

- [X] Make sure the linting passes

Adding a [Plausible](https://plausible.io/) example for a google analytics alternative.
parent 911bdacf
NEXT_PUBLIC_DOMAIN=example.com
# 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
.env.development.local
.env.test.local
.env.production.local
# vercel
.vercel
# Example app with Plausible
This example shows how to use [Next.js](https://github.com/vercel/next.js) along with [Plausible](https://plausible.io) via the [next-plausible](https://github.com/4lejandrito/next-plausible) package. A custom [\_app.js](https://nextjs.org/docs/advanced-features/custom-app) is used to wrap our app with the [`PlausibleProvider`](https://github.com/4lejandrito/next-plausible#plausibleprovider-props) to track page views and [custom events](https://github.com/4lejandrito/next-plausible#send-custom-events) are shown as well.
## 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/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-plausible&project-name=with-plausible&repository-name=with-plausible)
## 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) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example::
```bash
npx create-next-app --example with-plausible with-plausible-app
# or
yarn create next-app --example with-plausible with-plausible-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)).
import Link from 'next/link'
export default function Header() {
return (
<header>
<nav>
<ul>
<li>
<Link href="/">
<a>Home</a>
</Link>
</li>
<li>
<Link href="/about">
<a>About</a>
</Link>
</li>
<li>
<Link href="/contact">
<a>Contact</a>
</Link>
</li>
</ul>
</nav>
</header>
)
}
import Header from './Header'
export default function Page({ children }) {
return (
<div>
<Header />
{children}
</div>
)
}
{
"name": "with-plausible",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"next-plausible": "^1.6.0",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"license": "MIT"
}
import PlausibleProvider from 'next-plausible'
export default function MyApp({ Component, pageProps }) {
return (
<PlausibleProvider domain={process.env.NEXT_PUBLIC_DOMAIN}>
<Component {...pageProps} />
</PlausibleProvider>
)
}
import Page from '../components/Page'
export default function About() {
return (
<Page>
<h1>This is the About page</h1>
</Page>
)
}
import { useState } from 'react'
import Page from '../components/Page'
import { usePlausible } from 'next-plausible'
export default function Contact() {
const [message, setMessage] = useState('')
const plausible = usePlausible()
const handleSubmit = (e) => {
e.preventDefault()
plausible('customEventName', {
props: {
message,
},
})
// your own submit logic
setMessage('')
}
return (
<Page>
<h1>This is the Contact page</h1>
<form onSubmit={handleSubmit}>
<label>
<span>Message:</span>
<textarea
onChange={(e) => setMessage(e.target.value)}
value={message}
/>
</label>
<button type="submit">submit</button>
</form>
</Page>
)
}
import Page from '../components/Page'
export default function Home() {
return (
<Page>
<h1>This is the Home page</h1>
</Page>
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment