segmentio/analytics-ios

Remove individual identify traits using Swift

nbeadman opened this issue · 5 comments

I am trying to switch from CocoaPods to Swift Package Manager. This has caused some code that I was using to remove an outdated trait to no longer compile:

        if var segmentTraits = SEGState.sharedInstance().userInfo.traits,
           segmentTraits["outdated_trait"] != nil {
            segmentTraits["outdated_trait"] = nil
            SEGState.sharedInstance().userInfo.traits = segmentTraits
        }

because SEGState is no longer in the Public headers for the SDK.

Looking at issue #952 I see that this is intentional.

The best I have come up with to clear the outdated trait is:

        Analytics.shared().identify(nil, traits: ["outdated_trait": NSNull()])

but that results in the outdated_trait still being sent with every API call as shown in the debugger:

{
  "anonymousId": "XXXXX",
  "context": {
    "app": {
      "build": "XXXXX",
      "name": "",
      "namespace": "XXXXX",
      "version": "XXXXX"
    },
    "device": {
      "id": "XXXXX",
      "manufacturer": "Apple",
      "model": "x86_64",
      "name": "iPhone",
      "type": "ios"
    },
    "ip": "XXXXX",
    "library": {
      "name": "analytics-ios",
      "version": "4.1.5"
    },
    "locale": "en-US",
    "network": {
      "cellular": false,
      "wifi": true
    },
    "os": {
      "name": "iOS",
      "version": "14.5"
    },
    "protocols": {
      "sourceId": "XXXXX"
    },
    "screen": {
      "height": 812,
      "width": 375
    },
    "timezone": "America/Los_Angeles",
    "traits": {
      "anonymousId": "XXXXX",
      "outdated_trait": null,
    }
  },
  "integrations": {},
  "messageId": "XXXXX",
  "originalTimestamp": "2021-09-11T18:17:06.630Z",
  "receivedAt": "2021-09-11T18:17:15.680Z",
  "sentAt": "2021-09-11T18:17:15.252Z",
  "timestamp": "2021-09-11T18:17:07.058Z",
  "traits": {
    "anonymousId": "XXXXX",
  },
  "type": "identify",
  "userId": "XXXXX",
  "writeKey": "XXXXX"
}

It is possible to clear a single trait without going nuclear and calling Analytics.shared().reset()?

I can't merge this change unfortunately. To do something like this, you would write a source middleware to filter it out.

You might also check out our new analytics-swift library. It makes doing stuff like this quite a bit easier and will be entering beta on Tuesday.

hey @bsneed Thanks for clarifying.
Can you please explain how this would be easy to accomplish in analytics-swift? Can you please share some documentation?

Thanks in advance

In analytics-swift, it'd be as simple as doing this ...

// add an enrichment closure to remove the outdated trait
        analytics.add { event in
            guard var identify = event as? IdentifyEvent else { return event }
            identify.traits = try? identify.traits?.remove(key: "outdated_event")
            return identify
        }

Analytics-Swift info is here: https://github.com/segmentio/analytics-swift

Thank you so much.

My pleasure!