34

I cant find in the documentation if there is any callback functionality in the conversion tracking (https://developers.facebook.com/docs/ads-for-websites/tag-api)

In order to track an event you just need to call:

window._fbq = window._fbq || [];
window._fbq.push(['track', 'FBCONVERSIONCODE', {'value':'0.00','currency':'USD'}]);

That is very similar to google analytics conversion code, only though they allow you to call a function when the ajax call finish:

ga('send', 'pageview', {
  'page': '/my-new-page',
  'hitCallback': function() {
    alert('analytics.js done sending data');
  }
});

Is there a way to achieve the same functionality with Facebook API?

DomingoSL
  • 14,920
  • 24
  • 99
  • 173
  • 1
    Would something like the dynamic events work? https://developers.facebook.com/docs/ads-for-websites/tag-api#dynamicevents ie Once the purchase button is clicked activate the pixels and issue a callback? – Hey Darren Apr 17 '15 at 15:43
  • 12
    I'm surprised this question hasn't received more love. Did you ever figure this out? I'm not sure how the Facebook Tag API can be considered generally usable without a callback... – logidelic Oct 01 '15 at 17:09
  • @logidelic Did you ever figure this out? – tholu Apr 27 '16 at 20:37
  • I think you don't have this functionality. Perhaps you could just create custom "override" function eg. window._fbq.pushWithCallback(['track', 'FBCONVERSIONCODE', {'value':'0.00','currency':'USD'}], function(){ console.log('callback')}); – Jure Potocnik Jun 16 '17 at 11:53
  • it's 2018, does Facebook finally support a event callback like google analytics? – 23tux Oct 02 '18 at 10:12

3 Answers3

5

As of today, Facebook still does not support it. However, since I had this issue due to immediate redirect, I used the following solution:

basically I set on localStorage the variable I needed to track =>

 window.localStorage.setItem('documentTitle', document.title);

then I did the redirect, and on the targeted page I used the following to properly track fb event

if (typeof(fbq) !== 'undefined' && window.localStorage.getItem('documentTitle')) {
    fbq('track', 'Lead', {content_name: window.localStorage.getItem('documentTitle')});
    window.localStorage.removeItem('documentTitle');}

Hope this helps someone ;)

PS: this will work only if the redirected page is on the same host of the initial page, since localStorage is unique per: protocol://host:port

2

Facebook does not have a callback, but if you are facing the issue I am which is a redirect that is not allowing the request to finish, I would suggest you wrap around your redirect in a setTimeout

Example:

fbq('track', 'Purchase');
$('.loader').fadeIn();
setTimeout(function () {  
   window.location.replace("/bookings/"+booking_id);
}, 1500);

It usually takes 50-100ms to finish the request but it's safe to leave a 1500ms to finish firing the request.

0

No, Facebook doesn’t support it.

Yes, JavaScript supports it.

If the fbq call fails for some reason it will not return undefined, so simply verify a smooth execution.

function callback() {
  console.log('fn:callback');
}

if (
  typeof fbq('track', 'AddToCart', {
    content_name: 'Really Fast Running Shoes', 
    content_category: 'Apparel & Accessories > Shoes',
    content_ids: ['1234'],
    content_type: 'product',
    value: 4.99,
    currency: 'USD' 
  }
) === 'undefined') callback();
Kirk Strobeck
  • 17,984
  • 20
  • 75
  • 114
  • What's the point of downvoting an answer without a comment explaining why? It really does not help anyone. – Aurelio Jul 05 '17 at 16:38
  • 5
    The `fbq` object is going to kick off some sort of Ajax request, which is asynchronous, to Facebook with the event data, so even though the `fbq` object will `return` something other than `undefined`, the Ajax request will likely not have completed yet, so the `callback` function would not be run after the event data is actually sent to Facebook. This solution does not appear to meet the OP's need of running a callback after the "ajax call finished." – John Washam Sep 12 '17 at 17:19
  • Great point. It should be clarified that this is an optimistic approach. There is probably a better way to check. – Kirk Strobeck Sep 13 '17 at 09:44