react-native-analytics-segment-io icon indicating copy to clipboard operation
react-native-analytics-segment-io copied to clipboard

Application installed not being tracked

Open rodrijuarez opened this issue 8 years ago • 9 comments

I'm doing the Analytics setup on the JavaScript but I'm having a problem and missing some events on iOS (Application Opened and Application Installed)

Analytics.setup('segment_write_key', { [AnalyticsConstants.enableAdvertisingTracking]: true })

What I did for fixing this is removing the setup of Analytics on Javascript side and move it to the didFinishLaunchingWithOptions like this:

  SEGAnalyticsConfiguration *configuration = [SEGAnalyticsConfiguration configurationWithWriteKey:@"myKey"];
  configuration.trackApplicationLifecycleEvents = YES; 
  configuration.recordScreenViews = YES;
  configuration.enableAdvertisingTracking = YES;
  configuration.flushAt = 1;
  configuration.trackAttributionData = YES;
  configuration.trackApplicationLifecycleEvents = YES;
  configuration.recordScreenViews = YES;
  [SEGAnalytics debug:YES];
  
  [configuration use:[SEGFirebaseIntegrationFactory instance]];
  [configuration use:[SEGAppsFlyerIntegrationFactory instance]];

  
  [SEGAnalytics setupWithConfiguration:configuration];

In this way I receive the different events but they aren't pushed by some weird way to Segment and I think the Integrations aren't setup correctly (the integrations manager instance doesn't have integrations and registeredIntegrations setted up, only factories field)

I also tried to Analytics.setup in the componentWillMount of my index.ios.js file but it's still 'too late' for catching those events.

I think this is mostly related to this subscriptions not being setup early enough for them to be called when the different events are called:

        if (application) {
            for (NSString *name in @[ UIApplicationDidEnterBackgroundNotification,
                                      UIApplicationDidFinishLaunchingNotification,
                                      UIApplicationWillEnterForegroundNotification,
                                      UIApplicationWillTerminateNotification,
                                      UIApplicationWillResignActiveNotification,
                                      UIApplicationDidBecomeActiveNotification ]) {
                [nc addObserver:self selector:@selector(handleAppStateNotification:) name:name object:application];
            }
        }

I'm still debugging this issue trying to solve it on native side, giving up the alternative of having the code on the Javascript side for iOS. Maybe I'm having some simple issue that I cannot see, hope you guys can help me out with this one, because the configuration of this library has been pretty straightforward

rodrijuarez avatar Jan 23 '18 17:01 rodrijuarez

Did you try to set the trackApplicationLifecycleEvents to true when passing the options down from RN by including [AnalyticsConstants.trackApplicationLifecycleEvents]: true ?

I can see that you enable that in the native code, but not in the JavaScript, and it's disabled by default, according to the Segment docs: https://segment.com/docs/sources/mobile/ios/quickstart/#step-3-track-actions

For more information on the setup options, check https://github.com/leoilab/react-native-analytics-segment-io#setup-function-key-options--

I will make it a bit more clear in the documentation. I would appreciate your input on this PR: #41

kevinpelgrims avatar Jan 24 '18 12:01 kevinpelgrims

@kevinpelgrims yes, my bad, for the Javascript code I just pasted an example from the docs, indeed in my app I've trackApplicationLifecycleEvents added, what I think the problem is, is that these lifecycle events that the native code is subscribed to, are triggered before the Javascript code is loaded and therefore this Analytics.setup code is executed, so what I'm basically doing for now is the setup of Segment on native code (both iOS and Android) and in JavaScript I'm using the bridge that you guys created between JS and native. Though, I'm okay with this solution of setting up native and the rest on JS given that we want to have the most accuracy possible and I think that this is something that it's not easily solvable if we do everything on JS side.

BTW, something like this was my config in JS:

     [AnalyticsConstants.enableAdvertisingTracking]: true,		
        [AnalyticsConstants.flushAt]: (__DEV__) ? 1 : 20,		                        
        [AnalyticsConstants.recordScreenViews]: true,		
        [AnalyticsConstants.trackApplicationLifecycleEvents]: true,		
        [AnalyticsConstants.trackAttributionData]: true,		
        [AnalyticsConstants.trackDeepLinks]: true,		
        [AnalyticsConstants.debug]: __DEV__

rodrijuarez avatar Jan 24 '18 13:01 rodrijuarez

Alright. I can't immediately reproduce it myself.

The example app does the Segment setup in componentDidMount and I can get the App Opened event to trigger from there.

We also use the library in a bigger app, and there we do the Segment setup before we show any screens.

Not sure how we can get to the bottom of this.

kevinpelgrims avatar Jan 24 '18 13:01 kevinpelgrims

Ignore my previous comment. I was wrong. I did some more testing, and I can confirm that Application Installed and the first (from cold start) Application Opened are not tracked. I will look into this further to try to come up with a solution. Thanks for posting a workaround!

kevinpelgrims avatar Jan 25 '18 16:01 kevinpelgrims

@kevinpelgrims I don't know if this helps you in anything, but this is probably because of the Javascript load and execution being too late. Again, I don't think this can be solved in JS side in a reliable way, but I can be making a mistake given that I don't have good knowledge of react-native or native code. If I can help in any way for this to be solved or update the documentation just tell me!

rodrijuarez avatar Jan 25 '18 18:01 rodrijuarez

Closed for iOS in #44, but I will keep this open for Android. The fix from #44 is included in release 3.1.1 (out now).

kevinpelgrims avatar Jan 31 '18 12:01 kevinpelgrims

Is anyone still experiencing this issue? This event does not seem to be firing consistently for me (even for iOS). We are using the latest release (3.1.3).

We are also using react-native-branch-deeplinking, which captures install attribution, so I'm wondering if that could be throwing it off.

0xpatrickdev avatar Oct 19 '18 20:10 0xpatrickdev

We were having this issue in 2018 when we first started using Segment for React Native. Just recently re-visited this and it's still happening in 2021, any help here would be much appreciated.

All of Segment's UIApplicationDidBecomeActiveNotification observers get added AFTER the initial launch event fires...

giaset avatar Jun 18 '21 20:06 giaset

I would recommend that you switch to the official Segment RN package instead. This unofficial package is deprecated in favor of the official one, which is supported by Segment and regularly updated.

kevinpelgrims avatar Jun 27 '21 11:06 kevinpelgrims