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

Add with-facebook-pixel example #17667

Merged
merged 5 commits into from Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/with-facebook-pixel/.env.local.example
@@ -0,0 +1 @@
NEXT_PUBLIC_FACEBOOK_PIXEL_ID=
30 changes: 30 additions & 0 deletions examples/with-facebook-pixel/.gitignore
@@ -0,0 +1,30 @@
# 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
29 changes: 29 additions & 0 deletions examples/with-facebook-pixel/README.md
@@ -0,0 +1,29 @@
## 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)).
27 changes: 27 additions & 0 deletions examples/with-facebook-pixel/components/FacebookPixel.js
@@ -0,0 +1,27 @@
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
}
16 changes: 16 additions & 0 deletions examples/with-facebook-pixel/package.json
@@ -0,0 +1,16 @@
{
"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"
}
11 changes: 11 additions & 0 deletions examples/with-facebook-pixel/pages/_app.js
@@ -0,0 +1,11 @@
import FacebookPixel from '../components/FacebookPixel'

function MyApp({ Component, pageProps }) {
return (
<FacebookPixel>
<Component {...pageProps} />
</FacebookPixel>
)
}

export default MyApp
7 changes: 7 additions & 0 deletions examples/with-facebook-pixel/pages/index.js
@@ -0,0 +1,7 @@
export default function Home() {
return (
<h1>
Go to `pages/_app.ks` to see how you can add Facebook Pixel to your app
</h1>
)
}
Binary file added examples/with-facebook-pixel/public/favicon.ico
Binary file not shown.