-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for facebook pixel (#67)
- Loading branch information
Showing
20 changed files
with
389 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@wayofdev/eslint-config-custom': patch | ||
'@wayofdev/google-tag-manager': patch | ||
'web': patch | ||
--- | ||
|
||
feat: add support for facebook pixel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,12 +93,12 @@ | |
"postcss": "^8.4.21", | ||
"prettier": "^2.8.3", | ||
"rimraf": "^4.1.2", | ||
"secretlint": "^6.0.2", | ||
"secretlint": "^6.1.0", | ||
"shell-quote": "^1.8.0", | ||
"sort-package-json": "^2.3.0", | ||
"sort-package-json": "^2.4.0", | ||
"stylelint": "^14.16.1", | ||
"stylelint-a11y": "^1.2.3", | ||
"turbo": "^1.7.0", | ||
"turbo": "^1.7.2", | ||
"typescript": "^4.9.5" | ||
}, | ||
"packageManager": "[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Specific eslint rules for this app/package, extends the base rules | ||
* @see https://github.com/belgattitude/nextjs-monorepo-example/blob/main/docs/about-linters.md | ||
* @see https://github.com/wayofdev/next-starter-tpl/blob/master/docs/about-linters.md | ||
*/ | ||
|
||
// Workaround for https://github.com/eslint/eslint/issues/3458 (re-export of @rushstack/eslint-patch) | ||
require('@wayofdev/eslint-config-custom/patch/modern-module-resolution') | ||
|
||
const { getDefaultIgnorePatterns } = require('@wayofdev/eslint-config-custom/helpers') | ||
|
||
module.exports = { | ||
root: true, | ||
parserOptions: { | ||
tsconfigRootDir: __dirname, | ||
project: 'tsconfig.json', | ||
}, | ||
ignorePatterns: [...getDefaultIgnorePatterns()], | ||
extends: [ | ||
'@wayofdev/eslint-config-custom/typescript', | ||
'@wayofdev/eslint-config-custom/regexp', | ||
'@wayofdev/eslint-config-custom/sonar', | ||
'@wayofdev/eslint-config-custom/jest', | ||
'@wayofdev/eslint-config-custom/rtl', | ||
'@wayofdev/eslint-config-custom/react', | ||
// Apply prettier and disable incompatible rules | ||
'@wayofdev/eslint-config-custom/prettier', | ||
], | ||
rules: { | ||
// optional overrides per project | ||
}, | ||
overrides: [ | ||
// optional overrides per project file match | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
# Will override root .ncurc.yml | ||
# @link https://github.com/raineorshine/npm-check-updates | ||
|
||
reject: [] | ||
# Exclude package dependencies (not peer) if you want to keep a lower | ||
# range when publishing. For example: | ||
# '@emotion/react', | ||
# '@emotion/styled', | ||
# 'react', | ||
# 'react-dom', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "@wayofdev/facebook-pixel", | ||
"version": "1.0.0", | ||
"private": true, | ||
"homepage": "https://github.com/wayofdev/next-starter-tpl", | ||
"license": "MIT", | ||
"author": { | ||
"name": "Andrij Orlenko", | ||
"email": "[email protected]", | ||
"url": "https://github.com/lotyp" | ||
}, | ||
"sideEffects": false, | ||
"scripts": { | ||
"clean": "rimraf dist .turbo", | ||
"lint": "eslint . --ext .ts,.tsx,.js,.jsx,.cjs,.mjs --cache --cache-location ../../.cache/eslint/facebook-pixel.eslintcache", | ||
"lint:fix": "eslint . --ext .ts,.tsx,.js,.jsx,.cjs,.mjs --fix", | ||
"lint:types": "tsc --project tsconfig.json --noEmit" | ||
}, | ||
"devDependencies": { | ||
"@types/facebook-pixel": "^0.0.24", | ||
"@types/react": "^18.0.27", | ||
"@types/react-dom": "^18.0.10" | ||
}, | ||
"peerDependencies": { | ||
"next": "^13.1.6", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/facebook-pixel/src/components/FacebookPixelNoScript.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { facebookPixelId } from '../lib/fpixel' | ||
|
||
export function FacebookPixelNoScript() { | ||
return ( | ||
<> | ||
<noscript> | ||
<img | ||
height="1" | ||
width="1" | ||
style={{ display: 'none' }} | ||
src={`https://www.facebook.com/tr?id=${facebookPixelId}&ev=PageView&noscript=1`} | ||
alt="" | ||
/> | ||
</noscript> | ||
</> | ||
) | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/facebook-pixel/src/components/FacebookPixelScript.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useRouter } from 'next/router' | ||
import Script from 'next/script' | ||
import { useEffect } from 'react' | ||
import { onRouteChangeComplete, facebookPixelId } from '../lib/fpixel' | ||
|
||
export function FacebookPixelScript() { | ||
const router = useRouter() | ||
|
||
useEffect(() => { | ||
// This pageview only triggers the first time (it's important for Pixel to have real information) | ||
onRouteChangeComplete() | ||
|
||
router.events.on('routeChangeComplete', onRouteChangeComplete) | ||
return () => { | ||
router.events.off('routeChangeComplete', onRouteChangeComplete) | ||
} | ||
}, [router.events]) | ||
|
||
if (!facebookPixelId) return null | ||
|
||
return ( | ||
<> | ||
{/* Global Site Code Pixel - Facebook Pixel */} | ||
<Script | ||
id="fb-pixel" | ||
strategy="afterInteractive" | ||
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
!function(f,b,e,v,n,t,s) | ||
{if(f.fbq)return;n=f.fbq=function(){n.callMethod? | ||
n.callMethod.apply(n,arguments):n.queue.push(arguments)}; | ||
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; | ||
n.queue=[];t=b.createElement(e);t.async=!0; | ||
t.src=v;s=b.getElementsByTagName(e)[0]; | ||
s.parentNode.insertBefore(t,s)}(window, document,'script', | ||
'https://connect.facebook.net/en_US/fbevents.js'); | ||
fbq('init', ${facebookPixelId}); | ||
`, | ||
}} | ||
/> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './FacebookPixelScript' | ||
export * from './FacebookPixelNoScript' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './components' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const facebookPixelId = process.env.NEXT_PUBLIC_FACEBOOK_PIXEL_ID | ||
|
||
declare global { | ||
interface Window { | ||
fbq: facebook.Pixel.Event | ||
} | ||
} | ||
|
||
// https://developers.facebook.com/docs/facebook-pixel/advanced/ | ||
export const event = (name: string, options = {}) => { | ||
window.fbq('track', name, options) | ||
} | ||
|
||
export const onRouteChangeComplete = () => { | ||
window.fbq('track', 'PageView') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"extends": "../../tsconfig.base.json", | ||
"exclude": ["**/node_modules", "**/.*/", "dist", "build"], | ||
"include": ["./src/**/*"], | ||
"compilerOptions": { | ||
"baseUrl": "./src", | ||
"target": "esnext", | ||
"outDir": "dist", | ||
"allowJs": true, | ||
"jsx": "react-jsx", | ||
"noEmit": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
c169c73
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-starter-tpl-web – ./apps/web
next-starter-tpl-web.vercel.app
next-starter-tpl-web-wayofdev.vercel.app
next-starter-tpl.wayof.dev
next-starter-tpl-web-git-master-wayofdev.vercel.app
c169c73
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-starter-tpl-docs – ./apps/docs
next-starter-tpl-docs-git-master-wayofdev.vercel.app
next-starter-tpl-docs.vercel.app
next-starter-tpl-docs-wayofdev.vercel.app
next-starter-tpl-docs.wayof.dev