未验证 提交 fd4eb558 编写于 作者: A Ash Connell 提交者: GitHub

Add with-facebook-pixel example (#17667)

上级 68c62b33
NEXT_PUBLIC_FACEBOOK_PIXEL_ID=
\ No newline at end of file
# 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
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local
## Example app using Facebook Pixel
This example shows how to use Next.js along with Facebook Pixel. A [custom `App`](https://nextjs.org/docs/advanced-features/custom-app) is used to track route changes and send page views to Facebook Pixel. This example uses [react-facebook-pixel](https://www.npmjs.com/package/react-facebook-pixel).
## Deploy your own
Deploy the example using [Vercel](https://vercel.com):
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/vercel/next.js/tree/canary/examples/with-facebook-pixel)
## 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-facebook-pixel with-facebook-pixel-app
# or
yarn create next-app --example with-facebook-pixel with-facebook-pixel-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_FACEBOOK_PIXEL_ID` variable in `.env.local` to match your facebook app's pixel ID. If not specified, tracking will be disabled.
Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
import { useEffect } from 'react'
import { useRouter } from 'next/router'
const pixelId = process.env.NEXT_PUBLIC_FACEBOOK_PIXEL_ID
export default function FacebookPixel({ children }) {
const router = useRouter()
useEffect(() => {
if (!pixelId) return
let fb
function onRouteChange() {
fb.pageView()
}
import('react-facebook-pixel')
.then((module) => (fb = module.default))
.then(() => {
fb.init(pixelId, {
autoConfig: true,
debug: true,
})
fb.pageView()
})
router.events.on('routeChangeComplete', onRouteChange)
return () => router.events.off('routeChangeComplete', onRouteChange)
}, [router.events])
return children
}
{
"name": "with-facebook-pixel",
"version": "0.1.0",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-facebook-pixel": "^1.0.3"
},
"license": "MIT"
}
import FacebookPixel from '../components/FacebookPixel'
function MyApp({ Component, pageProps }) {
return (
<FacebookPixel>
<Component {...pageProps} />
</FacebookPixel>
)
}
export default MyApp
export default function Home() {
return (
<h1>
Go to `pages/_app.ks` to see how you can add Facebook Pixel to your app
</h1>
)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册