Reputation: 15504
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?
Upvotes: 34
Views: 6529
Reputation: 93
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.
Upvotes: 2
Reputation: 324
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
Upvotes: 5
Reputation: 18629
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();
Upvotes: 0