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 committed Sep 14, 2021
2 parents 8ad4f78 + 3b0c5e3 commit 38312b2
Showing 1 changed file with 66 additions and 2 deletions.
68 changes: 66 additions & 2 deletions Sources/Paywall/Paywall/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,23 @@ extension Paywall {
}


/// Warning: Should prefer `track` if using Swift
/// Tracks a event with properties. Remember to check `Paywall.StandardEvent` to determine if you should use a string which maps to standard event name. Properties are optional and can be added only if needed. You'll be able to reference properties when creating rules for when paywalls show up.
/// - Parameter event: The name of your custom event
/// - Parameter params: Custom parameters you'd like to include in your event. Remember, keys begining with `$` are reserved for Superwall and will be dropped. They will however be included in `PaywallDelegate.shouldTrack(event: String, params: [String: Any])` for your own records. 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.
///
/// Example:
/// ```objective-c
/// [Paywall trackWithName:@"onboarding_skip" params:NSDictionary()];
/// ```
@objc public static func track(name: String, params: NSDictionary? = [:]) {
if let stringParameterMap = params as? [String: Any] {
track(.base(name: name, params: stringParameterMap))
} else {
Logger.superwallDebug(string: "Unable to convert event into [String:Any]")
}
}

/// 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 standard: Zero or more `SubscriberUserAttribute` enums describing standard user attributes.
/// - Parameter custom: A `[String: Any?]` map used to describe 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.
Expand Down Expand Up @@ -472,13 +489,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 38312b2

Please sign in to comment.