Skip to main content

I would like to set a custom attribute with a string I get from UserDefaults().

This will NOT set the custom attribute:

let cam = UserDefaults.standard.string(forKey: "cam")!
Purchases.shared.attribution.setAttributes(("cam": cam])

This will set the custom attribute:

Purchases.shared.attribution.setAttributes(s"cam": "abc"])
let cam = "abc"
Purchases.shared.attribution.setAttributes(s"cam": cam])
let cam = UserDefaults.standard.string(forKey: "cam")!
Purchases.shared.attribution.setAttributes(s"cam": "abc"])
Purchases.shared.attribution.setAttributes(s"cam": cam])

UserDefaults combined with setting the attribute through a variable will not work, except if the attribute is previously set using a literal string (last example).

What do I miss here?
Is it a bug?
Is it Swift or RC related?

Addendum to the previous (edit timed out).

It appears that attribute creation is not possible using a variable. It must be a literal.

Changing an existing attribute works fine.

The workaround would be the following:

let cam = UserDefaults.standard.string(forKey: "cam")!
Purchases.shared.attribution.setAttributes(["cam": "---"]) // dummy value
Purchases.shared.attribution.setAttributes(["cam": cam]) // correct value

To me, this looks like a bug on RC side?


Hi ​@geohei,

Once you set the attribute, can you try doing a `syncAttributesAndOfferingsIfNeeded()` afterwards and check that the attribute is sent correctly? Also, can you enable debug logs and see if there’s any error there?

 

Thanks!

 


1.
Purchases.shared.syncAttributesAndOfferingsIfNeeded { offerings, error in }
This will sync the attribute, even if it doesn't exist and if its value has a variable (!= literal string) as origin. That’s good, but not what I want. I'd like the sync when the app goes in background (as per RC docs). I consider .syncAttributesAndOfferingsIfNeeded() as a workaround, not a solution.

2.
Still, .setAttributes behaves differently whether the value of the attribute has a variable (UserDefaults in my example) or a literal as origin.

.setAttributes with variable as value of non-existing attribute "cam":
DEBUG: ℹ️ setting values for attributes: u"cam"]
.setAttributes with literal as value of non-existing attribute "cam":
DEBUG: ℹ️ setting values for attributes: t"cam"]
DEBUG: ℹ️ Attribute set locally: SubscriberAttribute] key: cam value: cam setTime: 2024-11-23 11:59:01 +0000. It will be synced to the backend when the app backgrounds/foregrounds or when a purchase is made.

It’s somewhat hard to explain. I believe you need to test it yourself. It looks strongly like a bug in RC to me.


Reply