thebergamo / react-native-fbsdk-next

MIT License
633 stars 165 forks source link

[Expo] How to initialize SDK only on specific build? #497

Closed otani-mnbf closed 2 months ago

otani-mnbf commented 2 months ago

Needs help

using Expo, I can install the SDK without any problem by following the README, and I'm trying to initialize the SDK only on production env so it collects only real-world data.

so what I did is

package.json

    "react-native-fbsdk-next": "^12.1.3",

app.json

    "plugins": [
      ...
      [
        "react-native-fbsdk-next",
        {
          "appID": "14124113217378",
          "clientToken": "fb7dddxxxe4eac1120535xxx",
          "displayName": "demo-app",
          "scheme": "fb14124113217378",
          "advertiserIDCollectionEnabled": true,
          "autoLogAppEventsEnabled": true,
          "isAutoInitEnabled": false
        }
      ]
    ]

App.jsx

...
import { isDev, isExpoGo } from '@config/env';

export default function App() {
  const getFacebookSDK = useCallback(async () => {
    return (await import('react-native-fbsdk-next')).Settings;
  }, []);

  useEffect(() => {
    const initSDK = async () => {
      if (!isDev && !isExpoGo) {
        const fbSDK = await getFacebookSDK();
        fbSDK.initializeSDK();
      }
    };

    initSDK();
  }, [getFacebookSDK]);

  return (
    <MainApp />
   )

, which doesn't seem working on Android although it does on iOS for some reason... not working I mean, on Meta Console I can't see any numbers of sdk_initialization on Android and AppInstalls count doesn't seem correct cuz it doesn't match the one on GooglePlayConsole.

so if anyone knows anything about it or how to achieve it, Please let me know here🙏 Thank you!

otani-mnbf commented 2 months ago

It got it to work. This is what I did. Cheers!

app.json

    "plugins": [
      ...
      [
        "react-native-fbsdk-next",
        {
          "appID": "14124113217378",
          "clientToken": "fb7dddxxxe4eac1120535xxx",
          "displayName": "demo-app",
          "scheme": "fb14124113217378",
          "advertiserIDCollectionEnabled": false, // must be false by default
          "autoLogAppEventsEnabled": false, // must be false by default
          "isAutoInitEnabled": true // must be true by default
        }
      ]
    ]

App.jsx

...
import { isDev, isExpoGo } from '@config/env';

export default function App() {
  const getFacebookSDK = useCallback(async () => {
    return (await import('react-native-fbsdk-next')).Settings;
  }, []);

  useEffect(() => {
    const initSDK = async () => {
      if (!isDev && !isExpoGo) {
        const fbSDK = await getFacebookSDK();
        fbSDK.initializeSDK();
        fbSDK.setAutoLogAppEventsEnabled(true); // add this
        fbSDK.setAdvertiserIDCollectionEnabled(true); // add this
      }
    };

    initSDK();
  }, [getFacebookSDK]);

  return (
    <MainApp />
   )