segmentio / analytics-react-native

The hassle-free way to add analytics to your React-Native app.
https://segment.com/docs/sources/mobile/react-native/
MIT License
354 stars 182 forks source link

Issue setting context ip and device id #846

Closed flo-butterfly closed 12 months ago

flo-butterfly commented 1 year ago

Hi,

We want to anonymize the ip to 0.0.0.0 as well as device.id to 0000 To do so on track, we call: Segment.track(event, { properties, context: { ip: '0.0.0.0', device: { id: '0000' } }, });

but it seems to set those values in the property context of Properties, instead of the context itself (where you can see context ip is set 2x):

{
  "anonymousId": "XXX-XXX-XXX-XXX-XXX",
  "context": {
    "app": {
      "build": "XXX",
      "name": "XXX",
      "namespace": "XXX",
      "version": "XXX"
    },
    "device": {
      "id": "XXX-XXX-XXX-XXX-XXX",
      "manufacturer": "XXX",
      "model": "XXX",
      "name": "iPhone",
      "type": "ios"
    },
    "ip": "172.XXX.XXX.XXX",
    "library": {
      "name": "@segment/analytics-react-native",
      "version": "XXX"
    },
    "locale": "en-US",
    "network": {
      "cellular": XXX,
      "wifi": XXX
    },
    "os": {
      "name": "XXX",
      "version": "XXX"
    },
    "screen": {
      "height": XXX,
      "width": XXX
    },
    "timezone": "XXX",
    "traits": {}
  },
  "integrations": {},
  "messageId": "XXX-XXX-XXX-XXX",
  "name": "XXXName",
  "originalTimestamp": "2023-06-06TZ",
  "properties": {
    "context": {
      "device": {
        "id": "0000"
      },
      "ip": "0.0.0.0"
    },
    "properties": {
      "key": "XXXKey",
      "name": "XXXName"
    }
  },
  "receivedAt": "2023-06-06TZ",
  "sentAt": "2023-06-06TZ",
  "timestamp": "2023-06-06TZ",
  "type": "XXX",
  "userId": "XXX",
  "writeKey": "XXX"
}

Steps to reproduce Segment.track("an event", { properties, context: { ip: '0.0.0.0', device: { id: '0000' } }, });

Expected behavior The context ip an device id should be set respectively to 0.0.0.0 and 0000

Actual behavior the context ip and context device id are not overwritten and those values are being added in the "Properties" instead

oscb commented 12 months ago

Setting context is not currently an option when calling track. The recommended way of doing this is by using plugins or stripping them on Cloud:

Here's a short guide on how to write a plugin

The example shows just a logger but you can transform the event however you want, for example stripping the device.id:

export class RemoveDeviceID extends Plugin {

  type = PluginType.before;

  execute(event: SegmentEvent) {
    return {
        ...event,
        context: {
            ...event.context,
            device: {
                ...event.context.device,
                id: "0000"
            }
        }
    }
  }
}

The IP is special though as it is stamped by Segment Cloud when it arrives, not collected by the library.

I recommend contacting technical support at Segment to evaluate options to strip this depending on your case (that might also work for the deviceId if you're not using any native plugins).