|
| 1 | +--- |
| 2 | +title: Facebook's Pixel |
| 3 | +description: Take advantage of the [context](/context) and [custom events](/context-page) to create a component that dynamically sends Pixel events |
| 4 | +--- |
| 5 | + |
| 6 | +According to [developers.facebook.com](https://developers.facebook.com/docs/facebook-pixel/): |
| 7 | + |
| 8 | +"The Facebook pixel is a snippet of JavaScript code that allows you to track visitor activity on your website." |
| 9 | + |
| 10 | +You can take advantage of the [context](/context) and [custom events](/context-page) to create a component that dynamically sends Pixel events. |
| 11 | + |
| 12 | +Facebook's Pixel can only be called after [hydrate](/full-stack-lifecycle) to ensure it is running in the client. |
| 13 | + |
| 14 | +```jsx |
| 15 | +import Nullstack from 'nullstack'; |
| 16 | + |
| 17 | +class FacebookPixel extends Nullstack { |
| 18 | + |
| 19 | + async hydrate({id}) { |
| 20 | + ! function(f, b, e, v, n, t, s) { |
| 21 | + if (f.fbq) return; |
| 22 | + n = f.fbq = function() { |
| 23 | + n.callMethod ? |
| 24 | + n.callMethod.apply(n, arguments) : n.queue.push(arguments) |
| 25 | + }; |
| 26 | + if (!f._fbq) f._fbq = n; |
| 27 | + n.push = n; |
| 28 | + n.loaded = !0; |
| 29 | + n.version = '2.0'; |
| 30 | + n.queue = []; |
| 31 | + t = b.createElement(e); |
| 32 | + t.async = !0; |
| 33 | + t.src = v; |
| 34 | + s = b.getElementsByTagName(e)[0]; |
| 35 | + s.parentNode.insertBefore(t, s) |
| 36 | + }(window, document, 'script', |
| 37 | + 'https://connect.facebook.net/en_US/fbevents.js'); |
| 38 | + fbq('init', id); |
| 39 | + fbq('track', 'PageView'); |
| 40 | + window.addEventListener('nullstack.page.title', () => { |
| 41 | + fbq('init', id); |
| 42 | + fbq('track', 'PageView'); |
| 43 | + }) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +export default FacebookPixel; |
| 48 | +``` |
| 49 | + |
| 50 | +```jsx |
| 51 | +import Nullstack from 'nullstack'; |
| 52 | +import FacebookPixel from './FacebookPixel'; |
| 53 | + |
| 54 | +class Application extends Nullstack { |
| 55 | + |
| 56 | + // ... |
| 57 | + |
| 58 | + render() { |
| 59 | + return ( |
| 60 | + <main> |
| 61 | + <FacebookPixel id="REPLACE_WITH_YOUR_FACEBOOK_PIXEL_ID" /> |
| 62 | + </main> |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +} |
| 68 | + |
| 69 | +export default Application; |
| 70 | +``` |
| 71 | + |
| 72 | +## Using a Wrapper |
| 73 | + |
| 74 | +Alternatively, you can install [nullstack-facebook-pixel](https://github.com/Mortaro/nullstack-facebook-pixel) as a dependency: |
| 75 | + |
| 76 | +```sh |
| 77 | +npm install nullstack-facebook-pixel |
| 78 | +``` |
| 79 | + |
| 80 | +```jsx |
| 81 | +import Nullstack from 'nullstack'; |
| 82 | +import FacebookPixel from 'nullstack-facebook-pixel'; |
| 83 | + |
| 84 | +class Application extends Nullstack { |
| 85 | + |
| 86 | + // ... |
| 87 | + |
| 88 | + render() { |
| 89 | + return ( |
| 90 | + <main> |
| 91 | + <FacebookPixel id="REPLACE_WITH_YOUR_FACEBOOK_PIXEL_ID" /> |
| 92 | + </main> |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | +export default Application; |
| 100 | +``` |
| 101 | + |
| 102 | +## Next step |
| 103 | + |
| 104 | +> 🎉 *Congratulations*. You are done with the documentation! |
| 105 | +
|
| 106 | +⚔ If you want to see this more examples please [open an issue on github](https://github.com/nullstack/nullstack/issues). |
0 commit comments