Skip to content

Fix issue with missing subscriber attributes if set after login but before login callback#2313

Merged
tonidero merged 2 commits into
mainfrom
toniricodiez/sdk-2817-fix-issue-with-missing-subscriber
Feb 23, 2023
Merged

Fix issue with missing subscriber attributes if set after login but before login callback#2313
tonidero merged 2 commits into
mainfrom
toniricodiez/sdk-2817-fix-issue-with-missing-subscriber

Conversation

@tonidero

@tonidero tonidero commented Feb 22, 2023

Copy link
Copy Markdown
Contributor

Description

Deals with SDK-2817
This is the iOS counterpart to RevenueCat/purchases-android#809

There was an issue where subscriber attributes could be delayed to be synced to the backend. This happened when setting a subscriber attribute AFTER the logIn call but BEFORE the logIn operation was completed.

This issue happens because we start syncing subscriber attributes immediately when calling logIn, but if we set any after that but before the login is complete, they will be assigned to the old user id. We would sync those the next time the app is backgrounded/foregrounded but might be missed if there is a purchase before that happens. When there is a purchase, we only sync the current user's unsynced attributes, but not attributes of other users.

This PR copies all unsynced attributes from the old user id to the new one upon a successful login. This way, the unsynced attributes will be sent if the user tries to purchase.

Note that we only copy these attributes when the old user id was anonymous. This is because there is a possible edge case of copying attributes from a user to a different user if the sync request fails and remains unsynced when changing between user ids.

@tonidero tonidero added the pr:fix A bug fix label Feb 22, 2023
@tonidero tonidero marked this pull request as ready for review February 22, 2023 18:51
@tonidero tonidero requested a review from a team February 22, 2023 18:51

@NachoSoto NachoSoto left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍🏻

Just a few small things

case adservices_token_post_succeeded
case adservices_token_unavailable_in_simulator
case latest_attribution_sent_user_defaults_invalid(networkKey: String)
case copying_attributes_from_to_user(oldAppUserID: String, newAppUserID: String)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The swift way of naming this would be:

Suggested change
case copying_attributes_from_to_user(oldAppUserID: String, newAppUserID: String)
case copying_attributes(from oldAppUserID: String, to newAppUserID: String)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm when I try that, I get Enum case cannot have keyword arguments. Looking around, I don't think it's supported on enums? Going to rename it to copying_attributes(oldAppUserID: String, newAppUserID: String) though, since that's probably clearer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes sorry, that works

Comment thread Sources/Identity/IdentityManager.swift Outdated
userDefaults.setValue(groupedSubscriberAttributes, forKey: .subscriberAttributes)
}

static func deleteAllAttributes(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactor extracting these 2.

Comment thread Tests/UnitTests/Caching/DeviceCacheTests.swift
Comment thread Tests/UnitTests/Caching/DeviceCacheTests.swift Outdated
Comment thread Tests/UnitTests/Identity/IdentityManagerTests.swift Outdated
@tonidero

Copy link
Copy Markdown
Contributor Author

Ah backend tests are failing due to verification needing an update. Will hold this until then.

@NachoSoto

NachoSoto commented Feb 23, 2023

Copy link
Copy Markdown
Contributor

Yeah they deployed that a few days ago with no working. They're already failing in main so feel free to merge.

Hopefully I can finish #2309 today to fix them.

@tonidero tonidero merged commit fdd79cd into main Feb 23, 2023
@tonidero tonidero deleted the toniricodiez/sdk-2817-fix-issue-with-missing-subscriber branch February 23, 2023 14:53
@NachoSoto NachoSoto mentioned this pull request Mar 14, 2023
NachoSoto pushed a commit that referenced this pull request Mar 16, 2023
…efore login callback (#2313)

### Description
Deals with
[SDK-2817](https://linear.app/revenuecat/issue/SDK-2817/fix-issue-with-missing-subscriber-attributes-on-purchases-[ios])
This is the iOS counterpart to
RevenueCat/purchases-android#809

There was an issue where subscriber attributes could be delayed to be
synced to the backend. This happened when setting a subscriber attribute
AFTER the `logIn` call but BEFORE the `logIn` operation was completed.

This issue happens because we start syncing subscriber attributes
immediately when calling `logIn`, but if we set any after that but
before the login is complete, they will be assigned to the old user id.
We would sync those the next time the app is backgrounded/foregrounded
but might be missed if there is a purchase before that happens. When
there is a purchase, we only sync the current user's unsynced
attributes, but not attributes of other users.

This PR copies all unsynced attributes from the old user id to the new
one upon a successful login. This way, the unsynced attributes will be
sent if the user tries to purchase.

Note that we only copy these attributes when the old user id was
anonymous. This is because there is a possible edge case of copying
attributes from a user to a different user if the sync request fails and
remains unsynced when changing between user ids.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants