Skip to content

Events missing and can't add AppsFlyer integration #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
awaisjamil-mp opened this issue Oct 22, 2024 · 1 comment
Closed

Events missing and can't add AppsFlyer integration #110

awaisjamil-mp opened this issue Oct 22, 2024 · 1 comment

Comments

@awaisjamil-mp
Copy link

I am facing two issues

1: I am not able to add appsFlyerId to integration map on segment event

I have workarround for this find the code below

`import 'dart:async';

import 'package:analytics_service/analytics.dart';
import 'package:mp_logger/mp_logger.dart';
import 'package:segment_analytics/analytics.dart';
import 'package:segment_analytics/client.dart';
import 'package:segment_analytics/event.dart';
import 'package:segment_analytics/plugin.dart';
import 'package:segment_analytics/state.dart';

class SegmentAnalytics implements AnalyticsService {
final _log = 'SegmentAnalytics';
late Analytics _analytics;
late bool enabled;
late String firebaseAppInstanceId;
Map<String, dynamic>? integrations;

@OverRide
Future initialize({
required String writeKey,
required String appInstanceId,
required bool isEnabled,
}) async {
Logger.info(
_log,
'Initializing Segment Analytics with Write Key: $writeKey',
);
enabled = isEnabled;
firebaseAppInstanceId = appInstanceId;

if (!isEnabled) {
  Logger.info(_log, 'Segment Analytics is disabled.');
  return;
}

try {
  _analytics = createClient(
    Configuration(
      writeKey,
      trackApplicationLifecycleEvents: true,
      maxBatchSize: 15,
      collectDeviceId: true,
      errorHandler: (error) {
        Logger.error(_log, 'Error occurred: $error');
      },
    ),
  );

  return await _analytics.init().then(
        (_) => Logger.info(
            _log, 'Segment Analytics initialized successfully.'),
      );
} catch (e) {
  Logger.error(_log, 'Error initializing Segment Analytics: $e');
}

}

@OverRide
Future identify({
required String userId,
UserTraits? userTraits,
bool shouldReset = true,
}) async {
if (!enabled) {
Logger.info(_log, 'identify() skipped, Segment Analytics is disabled.');
return Future.value();
}

Logger.info(_log, 'Identifying user with ID: $userId');

try {
  if (userTraits != null) {
    userTraits.custom['firebaseAppInstanceId'] = firebaseAppInstanceId;
  }

  return _analytics.identify(userId: userId, userTraits: userTraits).then(
        (_) => Logger.info(_log, 'User identified successfully: $userId'),
      );
} catch (e) {
  Logger.error(_log, 'Error identifying user $userId: $e');
  return Future.value();
}

}

@OverRide
Future logEvent({
required String event,
Map<String, dynamic>? parameters,
}) {
Logger.info(_log, 'Logging event => $event');

if (!enabled) {
  Logger.info(_log, 'logEvent() skipped, Segment Analytics is disabled.');
  return Future.value();
}

try {
  if (parameters != null && parameters.isNotEmpty) {
    parameters['firebaseAppInstanceId'] = firebaseAppInstanceId;
  } else {
    parameters = {'firebaseAppInstanceId': firebaseAppInstanceId};
  }

  return _analytics.track(event, properties: parameters).then(
        (_) => Logger.info(_log, 'Event tracked successfully => $event'),
      );
} catch (e) {
  Logger.error(_log, 'Error logging event $event: $e');
  return Future.value();
}

}

@OverRide
Future screen({
required String name,
Map<String, dynamic>? parameters,
}) {
Logger.info(_log, 'Tracking screen => $name');

if (!enabled) {
  Logger.info(_log, 'screen() skipped, Segment Analytics is disabled.');
  return Future.value();
}

try {
  if (parameters != null && parameters.isNotEmpty) {
    parameters['firebaseAppInstanceId'] = firebaseAppInstanceId;
  } else {
    parameters = {'firebaseAppInstanceId': firebaseAppInstanceId};
  }

  return _analytics.screen(name, properties: parameters).then(
        (_) => Logger.info(_log, 'Screen tracked successfully => $name'),
      );
} catch (e) {
  Logger.error(_log, 'Error tracking screen $name: $e');
  return Future.value();
}

}

@OverRide
Future alias({required String alias}) {
Logger.info(_log, 'Creating alias for user => $alias');

if (!enabled) {
  Logger.info(_log, 'alias() skipped, Segment Analytics is disabled.');
  return Future.value();
}

try {
  return _analytics.alias(alias).then(
        (_) =>
            Logger.info(_log, 'Alias created successfully for => $alias'),
      );
} catch (e) {
  Logger.error(_log, 'Error creating alias for $alias: $e');
  return Future.value();
}

}

@OverRide
Future flush() {
Logger.info(_log, 'Flushing events');

if (!enabled) {
  Logger.info(_log, 'flush() skipped, Segment Analytics is disabled.');
  return Future.value();
}

try {
  return _analytics.flush().then(
        (_) => Logger.info(_log, 'Events flushed successfully.'),
      );
} catch (e) {
  Logger.error(_log, 'Error flushing events: $e');
  return Future.value();
}

}

@OverRide
Future group({
required String groupId,
GroupTraits? groupTraits,
}) {
Logger.info(_log, 'Grouping user into group => $groupId');

if (!enabled) {
  Logger.info(_log, 'group() skipped, Segment Analytics is disabled.');
  return Future.value();
}

try {
  return _analytics.group(groupId, groupTraits: groupTraits).then(
        (_) => Logger.info(_log, 'User grouped into => $groupId'),
      );
} catch (e) {
  Logger.error(_log, 'Error grouping user into $groupId: $e');
  return Future.value();
}

}

@OverRide
Future reset() async {
Logger.info(_log, 'Resetting analytics data');

if (!enabled) {
  Logger.info(_log, 'reset() skipped, Segment Analytics is disabled.');
  return;
}

try {
  Logger.info(_log, 'Analytics data reset successfully.');
  return await _analytics.reset();
} catch (e) {
  Logger.error(_log, 'Error resetting analytics data: $e');
}

}

@OverRide
void setUpIntegrationsMap(String appsFlyerId) {
Logger.info(
_log, 'Setting up integrations map with AppsFlyer ID: $appsFlyerId');

integrations = {
  "AppsFlyer": {
    "appsFlyerId": appsFlyerId,
  }
};

if (!enabled) {
  Logger.info(_log,
      'setUpIntegrationsMap() skipped, Segment Analytics is disabled.');
  return;
}

try {
  _analytics.addPlugin(
    AppsFlyerPlugin(
      'AppsFlyer',
      appsFlyerId: appsFlyerId,
      integrations: integrations ?? {},
    ),
    settings: {
      'appsFlyerId': appsFlyerId, // Pass your AppsFlyer ID in the settings
    },
  );

  Logger.info(_log, 'Integrations map set successfully.');
} catch (e) {
  Logger.error(_log, 'Error setting up integrations map: $e');
}

}
}

class AppsFlyerPlugin extends DestinationPlugin {
final String appsFlyerId;
final Map<String, dynamic> integrations;

AppsFlyerPlugin(
super.key, {
required this.appsFlyerId,
required this.integrations,
});

@OverRide
Future<RawEvent?> execute(RawEvent event) async {
// Your custom logic for AppsFlyer
event.integrations = integrations;
return super.execute(event);
}
}`

it adds the intgeration into the events. is this the right approach??

2: many times we have experienced an issue of missed events some events get missed on segment we are unable to find anything

could you please help.

I have attached whole code

@neelkanth-kaushik
Copy link
Contributor

HI @awaisjamil-mp ,

Here is a checklist for troubleshooting missing events issue -

  1. Make sure that you’re calling a Segment API method once the library is successfully installed and initialized.
  2. Make sure your application isn’t shutting down before the local queue events are pushed to Segment. You can manually call flush events to ensure the queue is fully processed before shutdown.
  3. Identifier is not present: Segment’s tracking API requires that each payload has a userId and/or anonymousId. If you send events without either the userId or anonymousId, Segment’s tracking API responds with an no_user_anon_id error. Check the event payload and client instrumentation for more details.
  4. Track event is missing name: All Track events to Segment must have a name in string format.
  5. Event dropped during deduplication: Segment automatically adds a messageId field to all payloads and uses this value to deduplicate events. If you’re manually setting a messageId value, ensure that each event has a unique value.
  6. Incorrect credentials: Double check your credentials for your downstream destination(s).
  7. Destination incompatibility: Make sure that the destination you are troubleshooting can accept server-side API calls. You can see compatibility information on the Destination comparison by category page and in the documentation for your specific destination.
  8. Destination-specific requirements: Check out the destination’s documentation to see if there are other requirements for using the method and destination that you’re trying to get working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants