koa-health / mixpanel_analytics

A dart wrapper on the mixpanel REST API to be used in Flutter applications.
https://pub.dev/packages/mixpanel_analytics
MIT License
19 stars 26 forks source link

IP flag should be added to the query string instead of payload #6

Closed isdaniarf closed 4 years ago

isdaniarf commented 4 years ago

Hi,

First I want to say thank you for creating this library; it saved me a lot of time!

Referring to Mixpanel's geolocation and user profile updates, ip=(1|0) flag should be in the URL query string, not inside property field in the request payload. The latter should be filled with the actual IP address.

My use case is to include geolocation data (e.g. city and country) in each event, not just user profile. I've tried to call below method with ip: '1' but the location data don't appear in Mixpanel event. So I tried sending events with Mixpanel Java API with useIpAddress flag set to true, and in that case the location data is shown in Mixpanel. It turned out that the ip parameter is appended to the end of the query string.

Snippet from src/mixpanel_analytics.dart:

Future<bool> track({
    @required String event,
    @required Map<String, dynamic> properties,
    DateTime time,
    String ip,
    String insertId,
  }) async {
    if (event == null) {
      throw ArgumentError.notNull('event');
    }
    if (properties == null) {
      throw ArgumentError.notNull('properties');
    }

    var trackEvent = _createTrackEvent(
        event, properties, time ?? DateTime.now(), ip, insertId);

    if (isBatchMode) {
      _trackEvents.add(trackEvent);
      return _saveQueuedEventsToLocalStorage();
    }

    var base64Event = _base64Encoder(trackEvent);
    return _sendTrackEvent(base64Event);
  }

I think the above code misinterpreted the documentation:

Screen Shot 2020-08-14 at 18 03 21

Could it be modified so that ip flag is placed as a query parameter? e.g.:

Future<bool> _sendBatch(String batch, String op) async {
    var url = '$baseApi/$op/?verbose=${_verbose ? 1 : 0}&ip=${_ip ? 1 : 0}';
    ...
}