1

这可能是一个愚蠢的问题,但目前我真的需要帮助。有人可以帮我吗?我在我的 ReactNative 项目(Android)上实现 AppsFlyer 我想要做的是 console.log 属性参数。但是,没有 console.logging 发生。有人可以阅读我的代码段吗?我该如何访问归因参数?或者,是否有任何适当的方法来console.log属性参数或将其保存到变量?

应用程序.tsx

​import appsFlyer from 'react-native-appsflyer';

var testFunc = appsFlyer.onAppOpenAttribution(
    (data) => {
        console.log(data);
    }
);

appsFlyer.initSdk(
    {
        devKey: '***************************',
        isDebug: false,
    },
    (result) => {
        console.log(result);
    },
    (error) => {
        console.error(error);
    },
);

const Home: React.FC<Props> = props => {
    const [appState, setAppState] = useState(AppState.currentState);

    // ! when I press device's home button (appstate changes to background),
   // ! console.log in testFunc is not working...
  
    useEffect(() => {
        function handleAppStateChange(nextAppState) {
            if (appState.match(/active|foreground/) && nextAppState === 'background') {
                if (testFunc) {
                    testFunc();
                    testFunc = null;
                }
            }
          setAppState(nextAppState);
       }

        AppState.addEventListener('change', handleAppStateChange);

        return () => {
        AppState.removeEventListener('change', handleAppStateChange);
      };
  })
4

2 回答 2

0

据我了解,该onAppOpenAttribution事件仅在您已安装应用程序并单击深层链接时触发。尝试onInstallConversionData改用,看看会发生什么,因为它会在 SDK 初始化后触发。我也会完全删除“useEffect”部分来进行测试。我希望这有帮助。

于 2020-07-08T02:26:44.057 回答
0

没关系,我添加了 appsFlyer.onInstallConversionData 然后它工作了......

  import appsFlyer from 'react-native-appsflyer';
            
  var onInstallConversionDataCanceller = appsFlyer.onInstallConversionData((res) => {
    if (JSON.parse(res.data.is_first_launch) == true) {
      if (res.data.af_status === 'Non-organic') {
        var media_source = res.data.media_source;
        var campaign = res.data.campaign;
        console.log('This is first launch and a Non-Organic install. Media source: ' + media_source + ' Campaign: ' + campaign);
      } else if (res.data.af_status === 'Organic') {
        console.log('This is first launch and a Organic Install');
      }
    } else {
      console.log('This is not first launch');
    }
  });
        
  var onAppOpenAttributionCanceller = appsFlyer.onAppOpenAttribution((res) => {
    console.log(res)
  });
            
  appsFlyer.initSdk(
    {
      devKey: '***************************',
      isDebug: false,
    },
    (result) => {
      console.log(result);
    },
    (error) => {
      console.error(error);
    },
  );
            
  const Home: React.FC<Props> = props => {
    const [appState, setAppState] = useState(AppState.currentState);
      useEffect(() => {
        function handleAppStateChange(nextAppState) {
          if (appState.match(/active|foreground/) && nextAppState === 'background') {
            if (onInstallConversionDataCanceller) {
              onInstallConversionDataCanceller();
              onInstallConversionDataCanceller = null;
            }

            if (onAppOpenAttributionCanceller) {
              onAppOpenAttributionCanceller();
              onAppOpenAttributionCanceller = null;
            }
          }
            
          AppState.addEventListener('change', handleAppStateChange);
            
          return () => {
            AppState.removeEventListener('change', handleAppStateChange);
          };
        })
于 2020-07-08T02:34:05.320 回答