(AppsFlyer / ReactNative) How can I get attribution parameter from onAppOpenAttribution?

1.2k Views Asked by At

This might be a dumb question, but currently I really need a help. Can someone please help me out? I'm implementing AppsFlyer on my ReactNative Project (Android) What I want to do is console.log attribution parameter. But, there are no console.logging happening. Could someone please read my snippet and how can I access to attribution parameter, please? or, is there any proper way to console.log attribution parameter or save it to variable?

App.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);
      };
  })
2

There are 2 best solutions below

0
On

To my understanding, the onAppOpenAttribution event only triggers when you already have the app installed and click on a deep link. Try to use onInstallConversionData instead and see what happens, since it triggers once the SDK is initialized. I'd also remove the "useEffect" section entirely just to test. I hope this helps.

0
On

nevermind, I added appsFlyer.onInstallConversionData then it worked...

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