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

Migration from v3 to v4 doesn't work? #243

Closed
vietstone-ng opened this issue Mar 26, 2025 · 6 comments
Closed

Migration from v3 to v4 doesn't work? #243

vietstone-ng opened this issue Mar 26, 2025 · 6 comments

Comments

@vietstone-ng
Copy link

Hi,

I'm migrating from v3 to v4. My migrating code is quite simple as below.
My app is currently working with v3, but not with v4 on both Android, iOS.
Not sure what's wrong with my migration.

// analytics.dart

import 'package:amplitude_flutter/amplitude.dart';
// v4
import 'package:amplitude_flutter/configuration.dart';
import 'package:amplitude_flutter/constants.dart';
import 'package:amplitude_flutter/default_tracking.dart';
import 'package:amplitude_flutter/events/base_event.dart';
import 'package:amplitude_flutter/events/identify.dart';

part 'events.dart';

class Analytics {
  Analytics._();

  static Amplitude? _amplitude;

  static bool enabled = true;

  static Future<void> init() async {
    const amplitudeApiKey = bool.hasEnvironment("AMPLITUDE_API_KEY")
        ? String.fromEnvironment("AMPLITUDE_API_KEY")
        : '';

    if (amplitudeApiKey.isEmpty) {
      print('[analytics] Amplitude API key not found');
    } else {
      print('[analytics] $amplitudeApiKey');
      print('[analytics] Amplitude API init');

      // v3
      // _amplitude = Amplitude.getInstance();
      // await _amplitude?.init(amplitudeApiKey);

      // _amplitude?.trackingSessionEvents(true);

      // v4
      _amplitude = Amplitude(Configuration(
        apiKey: amplitudeApiKey,
        defaultTracking: DefaultTrackingOptions.all(),
        logLevel: LogLevel.debug,
      ));
      await _amplitude?.isBuilt;
    }
  }

  // Use Amplitude's device ID for device tracking in the backend
  static Future<String?> getDeviceId() async {
    return _amplitude?.getDeviceId();
  }

  static Future<void> logEvent(
    AnalyticsEvent event, [
    Map<String, dynamic> params = const {},
  ]) async {
    if (!enabled || _amplitude == null) {
      return;
    }
    print('[analytics] logEvent: ${event.value}, $params');

    // v3
    // await _amplitude?.logEvent(event.value, eventProperties: params);

    // v4
    await _amplitude?.track(BaseEvent(event.value, eventProperties: params));
  }

  static Future<void> setUserProperties(
    Map<String, dynamic> properties,
  ) async {
    print('[analytics] setUserProperties: $properties');

    // v3
    // await _amplitude?.setUserProperties(properties);

    // v4
    final identify = Identify();
    for (final key in properties.keys) {
      identify.set(key, properties[key]);
    }
    await _amplitude?.identify(identify);
  }
@chungdaniel
Copy link
Contributor

Hi @vietstone-ng, great to hear that you are trying to upgrade to the v4 SDK, though it is unfortunate that the migration isn't going smoothly. Are there any logs? Could you describe a bit more about what exactly isn't working?

@vietstone-ng
Copy link
Author

Hi @chungdaniel there's no debug log from the SDK and no events received on server side.
As I remember, there's event and log in the very first run, then nothing from the second run. Both Android and iOS.

@chungdaniel
Copy link
Contributor

@vietstone-ng I tried using your sample code in the example app for the Amplitude-Flutter app and wasn't able to reproduce the issue - I was able to get events sent. One thing to note is that by default we wait 30 seconds before the event queue is flushed and sent over to Amplitude, though this was the same behaviour in v3. I'm assuming you have waited longer than 30 seconds before checking if events have sent?

Is the correct API key being passed to the SDK?

@vietstone-ng
Copy link
Author

@chungdaniel I found the problem. It's because default minIdLength is 5 in v4. I changed it to 1, and everything works.
I can close this issue as the problem solved.

Just want to know the reason behind this change?
Also this should be mentioned in the migration guide (https://amplitude.com/docs/sdks/analytics/flutter/flutter-sdk-4-0-migration-guide) and debug log.

Thank you.

@chungdaniel
Copy link
Contributor

@vietstone-ng ah, good catch! Our v3 SDK used an older version of our HTTP API, which did not have any enforcement of userId/deviceId length. Our v4 SDK uses our HTTP v2 API which enforces a minimum length of 5 as shorter ID lengths could potentially cause instrumentation issues.

I'll add this to the migration guide and an item in our backlog to add debug logging for insufficient length of IDs. Thank you!

@chungdaniel
Copy link
Contributor

The docs have been updated to explicitly mention that there is now a default minimum length for userId and deviceId on both the v3 -> v4 migration guide and for the v4 docs. Thanks for finding this!
https://amplitude.com/docs/sdks/analytics/flutter/flutter-sdk-4-0-migration-guide#setuserid
https://amplitude.com/docs/sdks/analytics/flutter/flutter-sdk-4#initialize-the-sdk

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