用于 React Native 的 Facebook 重定向像素代码

Facebook Retargeting Pixel code for React Native

您好,我正在尝试为我的 React Native 应用程序添加 Facebook 重定向像素,我希望社区可以帮助我澄清一些事情。首先,要跟踪产品的事件 PageViewViewContent,我将以下脚本添加到产品页面。

<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','//connect.facebook.net/en_US/fbevents.js');

fbq('init', 'xxxxxxxxxxxxxxxx');
fbq('track', 'PageView');
fbq('track', 'ViewContent', {

        content_name: 'Truyện Cổ Grimm (Bìa Cứng)',
        content_category: 'Truyện đọc',
        content_ids: ['8935212329736'],
        content_type: 'product',
        value: 153750,
        currency: 'VND'

});
</script>

我想知道如何使用 React Native 实现同样的效果。我看到 Facebook 有 FB SDK for react native (https://developers.facebook.com/docs/react-native),我想问一下,这个 SDK 是不是我可以实现的,类似于我上面的 web。如果我查看产品,我会这样做吗?

AppEventsLogger.logEvent('ViewContent');

如果可以,我可以指定其他参数吗,比如这样?

AppEventsLogger.logEvent('ViewContent', {

    content_name: 'Truyện Cổ Grimm (Bìa Cứng)',
    content_category: 'Truyện đọc',
    content_ids: ['8935212329736'],
    content_type: 'product',
    value: 153750,
    currency: 'VND'
});

哪位有这方面的成功经验,请帮帮我。非常感谢

您猜对了要与 react-native-fbsdk 一起使用的事件。如果您查看此 SDK 的源代码 here,您会发现可以通过多种方式使用 logEvent 方法。

你猜的方法:

AppEventsLogger.logEvent('ViewContent', {
    content_name: 'Truyện Cổ Grimm (Bìa Cứng)',
    content_category: 'Truyện đọc',
    content_ids: ['8935212329736'],
    content_type: 'product',
    value: 153750,
    currency: 'VND'
});

将在 FB Analytics 上记录事件,然后您可以根据这些参数自定义您的报告。在我使用它的过程中,我遇到了一个问题,即它不支持数组。所以上面的代码将更改如下:

AppEventsLogger.logEvent('ViewContent', {
    content_name: 'Truyện Cổ Grimm (Bìa Cứng)',
    content_category: 'Truyện đọc',
    content_ids: '8935212329736',
    content_type: 'product',
    value: 153750,
    currency: 'VND'
});

有了这个你就可以开始了。我希望这能解决您的问题。

注:

React Native Bridge 目前不支持数组。不支持数组的地方可以查看this

2021 年 9 月更新

Facebook 现已停止对 react-native-fbsdk 的官方支持,但有一个 strong community fork 正在积极维护。