Skip to content

Commit

Permalink
Merge pull request #3 from superwall-me/brian/sw-218-objective-c-user…
Browse files Browse the repository at this point in the history
…-attributes

Exposes setUserAttributes which accepts an NSDictionary
  • Loading branch information
anglinb authored Sep 14, 2021
2 parents 8ad4f78 + 3b0c5e3 commit 9fa795e
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions Sources/Paywall/Paywall/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -472,13 +472,60 @@ extension Paywall {
map[.apnsToken] = s
case .createdAt(let d):
map[.createdAt] = d

}
}

track(.userAttributes(standard: map, custom: custom))
}

/// *Note* Please use `setUserAttributes` if you're using Swift.
/// Sets additional information on the user object in Superwall. Useful for analytics and conditional paywall rules you may define in the web dashboard. Remember, attributes are write-only by the SDK, and only require your public key. They should not be used as a source of truth for sensitive information.
/// - Parameter attributes: A `NSDictionary` used to describe user attributes and any custom attributes you'd like to store to the user. Remember, keys begining with `$` are reserved for Superwall and will be dropped. Values can be any JSON encodable value, URLs or Dates. Arrays and dictionaries as values are not supported at this time, and will be dropped.
///
/// We make our best effort to pick out "known" user attributes and set them to our internal names. For exampe `{"first_name": "..." }` and `{"firstName": "..."}` will both be translated into `$first_name` for use in Superwall where we require a first name.
///
/// Example:
/// ```swift
/// var userAttributes: NSDictionary = NSDictionary()
/// userAttributes.setValue(value: "Jake", forKey: "first_name");
/// Superwall.setUserAttributes(userAttributes)
/// ```
@objc public static func setUserAttributesDictionary(attributes: NSDictionary = [:]) {
var map = [StandardUserAttributeKey: Any]()
map[.applicationInstalledAt] = DeviceHelper.shared.appInstallDate
for (anyKey, value) in attributes {
if let key = anyKey as? String {
switch (key) {
case "firstName", "first_name":
map[.firstName] = value
case "id", "ID":
map[.id] = value
case "lastName", "last_name":
map[.firstName] = value
case "email":
map[.email] = value
case "phone":
map[.phone] = value
case "full_phone", "fullPhone":
map[.fullPhone] = value
case "phone_country_code", "phoneCountryCode":
map[.phoneCountryCode] = value
case "fcm_token", "fcmToken":
map[.fcmToken] = value
case "apns_token", "apnsToken", "APNS":
map[.apnsToken] = value
case "createdAt", "created_at":
map[.createdAt] = value
default:
break;
}
}
}
if let anyAttributes = attributes as? [String:Any] {
track(.userAttributes(standard: map, custom: anyAttributes))
} else {
track(.userAttributes(standard: map, custom: [:]))
}
}
}


Expand Down

0 comments on commit 9fa795e

Please sign in to comment.