Skip to main content

Facebook Pixel

If your Facebook Pixel is not managed with Google Tag Manager or DataGrail's ScriptControl, you can do so with a callback function. With this implementation, you can leave the script running, but control the pixel opt-out via the Facebook Pixel GDPR support and a callback.

Here's some sample code:

<!-- 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');

// NOTE: do not call fbq('init', '{pixel-id-goes-here}')
// outside of the `init_fb_prefs` function.

window.dgEvent = window.dgEvent || [];
function fb_prefs(preferences) {
if (window.DG_BANNER_API.categoryEnabled('marketing')) {
fbq('consent', 'grant');
} else {
fbq('consent', 'revoke');
}
}
function init_fb_prefs(preferences) {
if (window.DG_BANNER_API.categoryEnabled('marketing')) {
fbq('consent', 'grant');
fbq('init', '{pixel-id-goes-here}');
// optionally
fbq('track', 'PageView');
} else {
fbq('consent', 'revoke');
fbq('init', '{pixel-id-goes-here}');
}
}

window.dgEvent.push({
event: "preference_callback",
params: fb_prefs
});
window.dgEvent.push({
event: "initial_preference_callback",
params: init_fb_prefs
});
</script>

We have provided two functions in this example, one that controls the pixel initialization and consent setting on every page load (init_fb_prefs) or one that controls the pixel consent setting when a consent preference is received (fb_prefs).

 

Need help?
If you have any questions, please reach out to your dedicated Account Manager or contact us at support@datagrail.io.

Disclaimer: The information contained in this message does not constitute as legal advice. We would advise seeking professional counsel before acting on or interpreting any material.