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 1 commit
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
23 changes: 23 additions & 0 deletions examples/with-facebook-pixel/README.md
@@ -0,0 +1,23 @@
## Example app using Facebook Pixel

This example shows how to use Next.js along with Facebook Pixel. A custom `_app.js` is used to track route changes and send pageviews to facebook pixel. This example uses [react-facebook-pixel](https://www.npmjs.com/package/react-facebook-pixel).

You will need to set your `NEXT_PUBLIC_FACEBOOK_PIXEL_ID` environment variable for it to send events to your facebook app. When not specified, tracking is disabled.
ashconnell marked this conversation as resolved.
Show resolved Hide resolved

## Deploy your own

Deploy the example using [Vercel](https://vercel.com/now):

[![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
```

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 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'
lfades marked this conversation as resolved.
Show resolved Hide resolved

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

export default MyApp
3 changes: 3 additions & 0 deletions examples/with-facebook-pixel/pages/index.js
@@ -0,0 +1,3 @@
export default function Home() {
return <h1>with-facebook-pixel</h1>
}
Binary file added examples/with-facebook-pixel/public/favicon.ico
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/with-facebook-pixel/public/vercel.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.