Facebook conversion event callback

☆樱花仙子☆ 提交于 2019-12-06 16:39:06

问题


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?


回答1:


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();



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/29487133/facebook-conversion-event-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!