/ Tutorials

Facebook Pixel Tracking Across Multiple Domains

A typical situation for a SaaS website is to have your public web on www.companyname.com and have your actual app on app.companyname.com.

This approach makes sense for many best practices on the web but does not work so well with Facebook Pixel tracking.

The iframe approach

A handy way and possibly the only way to get around the multiple domains or subdomains issue is to inject an iframe into the subdomain.

To track a signup event on app.companyname.com, you first need to create a new page containing the Facebook Pixel tracking code on www.companyname.com/facebook-pixel-signup.

The www.companyname.com/facebook-pixel-signup page will be very minimal as users never see this page.

<!DOCTYPE html>
    <meta name="robots" content="noindex">
    <!-- Facebook Pixel Code -->
      s.parentNode.insertBefore(t,s)}(window, document,'script',
      fbq('init', 'xxxxxxxxxxxxxxx');
      fbq('track', 'CompleteRegistration');
      <img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=xxxxxxxxxxxxxxx&ev=PageView&noscript=1">
    <!-- End Facebook Pixel Code -->

Note that we also added the robots meta tag to prevent search engines from indexing this page.

Next, add this line to your app.companyname.com/thanks-for-signing-up page.

<iframe src="https://www.companyname.com/facebook-pixel-signup" style="width:0;height:0;border:none;"></iframe>

That's it 🎊! Use this approach to track users across multiple domains for your website.



By default, your web server or balancer might not allow for iframes from other domains or subdomains. Fix that with the Content-Security-Policy response header.

Content-Security-Policy: frame-ancestors '*.companyname.com'
Send realtime data to your users
We take care of complex realtime infrastructure problems so you can focus on what you enjoy - building awesome apps
Build realtime features now