Facebook Pixel (consent mode)
Facebook Pixel offers methods to enable or disable consent mode based on whether or not the user has consented to the suitable cookie category. Currently the implementation is not built in as the suitable category may vary depending on how you use Facebook Pixel on your site.
It’s however easy to implement with a few changes to the implementation code.
How to implement the Facebook Pixel consent mode with CookieHub
1. Add the fbq(‘consent’…) line to your Facebook Pixel code above the init method as shown below:
<!-- Facebook Pixel Code --> <script> !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('consent', 'revoke'); fbq('init', 'xxxxxxxxxxxxxxxxxxx'); fbq('track', 'PageView'); </script> <!-- End Facebook Pixel Code -->
2. Add the onAllow and onDeny triggers to your CookieHub implementation code:
<script type="text/javascript"> var cpm = { onAllow: function(category) { if (category == 'marketing') { fbq('consent', 'grant'); } }, onRevoke: function(category) { if (category == 'marketing') { fbq('consent', 'revoke'); } } }; (function(h,u,b){ var d=h.getElementsByTagName("script")[0],e=h.createElement("script"); e.async=true;e.src='https://cookiehub.net/c2/xxxxxxxx.js'; e.onload=function(){u.cookiehub.load(b);} d.parentNode.insertBefore(e,d); })(document,window,cpm); </script>
In this example, Facebook Pixel is allowed when the user allows the marketing category. If you would like to use the analytics instead, simply replace marketing with analytics.