segmentio / analytics_flutter

The hassle-free way to add Segment analytics to your Flutter app.
MIT License
30 stars 40 forks source link

Appsflyer help #99

Closed GeorgePagMezzi closed 1 month ago

GeorgePagMezzi commented 3 months ago

Hello, I just switched over from deprecated Segment package to this one and would like to also include Appsflyer support in order to track attribution data through Segment in my ios app. In my Segment dashboard I have added the 'Appsflyer' destination and included the Apple APP ID, Appsflyer Dev Key andhave enabled 'Track Attribution Data' option. Code on the FE looks like this:

final clientAnalytics = createClient(
  Configuration(
    "KEY",
    /// This is set to false to avoid swarming the console messages. If Segment debug is needed set it to true.
    debug: false,
    trackApplicationLifecycleEvents: true,
    trackDeeplinks: true,
  ),
);
clientAnalytics.addPlugin(AppsFlyerDestination());

Sadly the attribution does not work on Segment side with this setup (even though deprecated package was somewhat consistent with attribution). Is there anything more I should do? E.G. create an appsflyerDestination object and manually input the settings?

final AppsFlyerDestination appsFlyerDestination = AppsFlyerDestination();
appsFlyerDestination.appsFlyerSettings = AppsFlyerSettings(
        appleAppID, appsFlyerDevKey, httpFallback, rokuAppID, trackAttributionData, type, versionSettings);
clientAnalytics.addPlugin(appsFlyerDestination);

When should I run the identify method of AppsFlyerDestination class? I also notice that in your docs it says: 'When you send this flag the plugin_appsflyer SDK ignores attribution. What does it mean when you say 'send' (do you mean 'set')? is the plugin_appsflyer sdk the same as segment_analytics_plugin_appsflyer? image

Also if I Restart the app, I get below error. I have declared the clientAnalytics variable globally.

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value

0 AppsFlyerDestination.identify (package:segment_analytics_plugin_appsflyer/plugin_appsflyer.dart:61:16)

plugin_appsflyer.dart:61

1 EventPlugin.execute (package:segment_analytics/plugin.dart:52:16)

plugin.dart:52

2 DestinationPlugin.process (package:segment_analytics/plugin.dart:163:17)

plugin.dart:163

Thank you in advance and remain at your disposal to provide further info.

edsonjab commented 2 months ago

Hi @GeorgePagMezzi thank you for your report, we start looking into this.

edsonjab commented 2 months ago

Hi @GeorgePagMezzi sorry to answer late, this is an example to use the AppsFlyerDestination plugin: image

GeorgePagMezzi commented 2 months ago

Thank you @edsonjab , so appsflyer plugin does not sync from Segment cloud some time after adding it as a plugin to the Analytics client, based on the settings we have set up on Segment destination dashboard? I looked into it a bit more these days and it appears that after doing

clientAnalytics.addPlugin(appsFlyerDestination);
    Function? removeListener;
    removeListener = clientAnalytics.state.integrations.addListener((integrationMap) {
      if (integrationMap['AppsFlyer'] != null) {
        appsFlyerDestination.identify(
          IdentifyEvent(
            userId: FirebaseAuth.instance.currentUser?.uid,
          ),
        );
        if (removeListener != null) {
          removeListener();
        }
      }
    });

This integrationMap['AppsFlyer'] object is filled by some values which appear in sync with what is on Segment dashboard, but the above code can still sometimes fail when identify is called due to the exception

Unhandled Exception: Null check operator used on a null value

0 AppsFlyerDestination.identify (package:segment_analytics_plugin_appsflyer/plugin_appsflyer.dart:61:16)

if I

await Future.delayed(seconds: 5) //e.g. and run identify again
   appsFlyerDestination.identify(
          IdentifyEvent(
            userId: FirebaseAuth.instance.currentUser?.uid,
          ),
        );

Then the error goes away, which makes me thing Segment makes some api calls when we add appsflyer as a pluging, but we have no way to await the initialization of appsflyer. I will try your response, but it would be great if we had a way to await the initialization of the destination.

GeorgePagMezzi commented 2 months ago

Also it would be great if that Map<String,dynamic> settings object in your screenshot was an object, to avoid misstyping the keys.

GeorgePagMezzi commented 2 months ago

@edsonjab When implementing solution from screenshot I get below error:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value

0 AppsFlyerDestination.update (package:segment_analytics_plugin_appsflyer/plugin_appsflyer.dart:27:35)

plugin_appsflyer.dart:27

and the app hangs upon startup when update is called.

edsonjab commented 2 months ago

This happened once the state inside the configuration is not initialized properly, for that reason is important add plugin AppsFlyerDestination after Analytics was created with all configurations:

image

In that line is failing your code.

GeorgePagMezzi commented 1 month ago

I will be closing this issue, as we switched package since package still seems a bit behind the deprecated one. Thank you for trying to solve this! A better documentation or code examples would be great for these kind of issues. Thanks again!