Reputation: 21
I’m sending people to a blank landing page in order to make sure the lead is tracked by our Facebook pixel. I can’t do it on the main site as we do not have control of it (so can’t add the pixel there).
I currently have the FB pixel in the body, and then under it I do my redirect in Javascript after a 1 second timeout:
<html>
<body>
<!-- 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('init', '[pixel_id]');
fbq('track', 'PageView');
fbq('track', 'Lead');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=[pixel_id]&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->
<script>
function sendOn () {
window.location.replace("[redirect_url]");
}
setTimeout(sendOn, 1000);
</script>
</body>
</html>
This seems to have missed some visits though. What’s the best way for me to make sure the pixel has fired before the redirect? Should I put the pixel tracking in the HEAD - is that enough? Or is there a better JS event I can use than just timeout?
Thanks for your help!
Upvotes: 2
Views: 3350
Reputation: 697
Putting the pixel in the <head></head>
tag will be your best bet.
The reason being is that pages load from top to bottom. If you put the pixel after all your <link></link>
and <script></script>
tags, then they have to load first, then your pixel will load. So if people leave your page before it fully loads all those items and doesn't get to the pixel then it won't track.
Facebook even recommends this: https://www.facebook.com/business/a/online-sales/conversion-tracking
EDIT: I also just noticed that your <noscript>
image pixel doesn't fire the "Lead" event. It only fires a "PageView" event, which might point to your missing tracking numbers. You may want to either modify that url to send both events (if that's possible) or add a second image tag to send the "Lead" event.
Upvotes: 2