Skip to content
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

Events missing and can't add AppsFlyer integration #110

Open
awaisjamil-mp opened this issue Oct 22, 2024 · 0 comments
Open

Events missing and can't add AppsFlyer integration #110

awaisjamil-mp opened this issue Oct 22, 2024 · 0 comments

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

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

1 participant