Skip to content

Instantly share code, notes, and snippets.

@abhishekpatel946
Created August 27, 2022 06:26
Show Gist options
  • Save abhishekpatel946/f003011fcfa991f29b54c66cb2ec0b6e to your computer and use it in GitHub Desktop.
Save abhishekpatel946/f003011fcfa991f29b54c66cb2ec0b6e to your computer and use it in GitHub Desktop.
A tiny open-source facebook pixel custom plugin support for analytics. https://getanalytics.io/
type Config = {
pixelId: string
enabled: boolean
}
let fb: any
let fbLoaded = false
export default function facebookPixel(config: Config) {
return {
name: 'facebook-pixel',
initialize: async ({ config }) => {
const { pixelId } = config
if (typeof window !== 'undefined') {
await import('react-facebook-pixel')
.then((module) => (fb = module.default))
.then(() => {
if (!fbLoaded) {
fb.init(pixelId, {
autoConfig: true,
debug: true,
})
fbLoaded = true
}
})
}
},
page: (): void => {
fb.pageView()
},
track: ({ payload }) => {
fb.track(payload.event, payload.properties)
},
loaded: (): boolean => {
return fbLoaded
},
}
}
@abhishekpatel946
Copy link
Author

@Mexflubber This is the working code and final code. Is it not working for you properly?
FYI I'm using getAnalytics open-source package and this the plug-in for Facebook Pixel. You just need to connect with getAnalytics using the above code as utility and it's doing their job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment