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
},
}
}
@harrisyn
Copy link

continuing the conversation here

@harrisyn
Copy link

continuing the conversation here

@abhishekpatel946
Copy link
Author

abhishekpatel946 commented Sep 28, 2023

Sure @harrisyn

@harrisyn
Copy link

harrisyn commented Sep 29, 2023

I will make a loom to show you what my implementation looks like, otherwise if you are available for call, let me know

@abhishekpatel946
Copy link
Author

Sure @harrisyn

@Mexflubber
Copy link

Hi @abhishekpatel946 and @harrisyn,

Can you share the final code for the FacebookPixel plugin ?

Thanks,
Aldo

@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