Skip to content

Commit 4b0cae3

Browse files
authored
improve web2web, add ability to restore by email (#124)
1 parent 5e27d38 commit 4b0cae3

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

ApphudSDK.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'ApphudSDK'
3-
s.version = '3.5.5'
3+
s.version = '3.5.6'
44
s.summary = 'Build and Measure In-App Subscriptions on iOS.'
55
s.description = 'Apphud covers every aspect when it comes to In-App Subscriptions from integration to analytics on iOS and Android.'
66
s.homepage = 'https://github.com/apphud/ApphudSDK'

Sources/Internal/ApphudInternal+Attribution.swift

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -220,30 +220,45 @@ extension ApphudInternal {
220220

221221
@MainActor
222222
internal func tryWebAttribution(attributionData: [AnyHashable: Any], completion: @escaping (Bool, ApphudUser?) -> Void) {
223-
let userId = attributionData["aph_user_id"] ?? attributionData["apphud_user_id"]
224-
if let userId = userId as? String, !userId.isEmpty {
225-
226-
if (currentUser?.userId == userId) {
227-
apphudLog("Already web2web user, skipping")
228-
completion(true, currentUser)
229-
return
230-
}
231-
232-
apphudLog("Found a match from web click, updating User ID to \(userId)", forceDisplay: true)
233-
self.updateUser(fields: ["user_id": userId, "from_web2web": true]) { (result, _, data, _, _, _, attempts) in
223+
224+
let userId = (attributionData["aph_user_id"] ?? attributionData["apphud_user_id"]) as? String ?? ""
225+
let email = (attributionData["email"] ?? attributionData["apphud_user_email"]) as? String ?? ""
226+
227+
if userId.isEmpty && email.isEmpty {
228+
completion(false, currentUser)
229+
return
230+
}
231+
232+
if (email.isEmpty && currentUser?.userId == userId) {
233+
apphudLog("Already web2web user, skipping")
234+
completion(true, currentUser)
235+
return
236+
}
237+
238+
var params: [String: Any] = ["from_web2web": true]
239+
if !userId.isEmpty {
240+
params["user_id"] = userId
241+
}
242+
if !email.isEmpty {
243+
params["email"] = email
244+
}
245+
246+
apphudLog("Found a match from web click, updating User ID to \(userId)", forceDisplay: true)
247+
self.performWhenUserRegistered {
248+
self.updateUser(fields: params) { (result, _, data, _, _, _, attempts) in
234249
if result {
235250
Task {
236-
await self.parseUser(data: data)
251+
let changes = await self.parseUser(data: data)
252+
237253
Task { @MainActor in
254+
self.notifyAboutUpdates(changes)
238255
completion(true, self.currentUser)
239256
}
240257
}
241258
} else {
242259
completion(false, self.currentUser)
243260
}
244261
}
245-
} else {
246-
completion(false, currentUser)
247262
}
248263
}
249264
}

Sources/Public/Apphud.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Foundation
1414
import UserNotifications
1515
import SwiftUI
1616

17-
internal let apphud_sdk_version = "3.5.5"
17+
internal let apphud_sdk_version = "3.5.6"
1818

1919
// MARK: - Initialization
2020

@@ -791,10 +791,12 @@ final public class Apphud: NSObject {
791791

792792
/**
793793
Web-to-Web flow only. Attempts to attribute the user with the provided attribution data.
794-
If the `data` parameter contains either `aph_user_id` or `apphud_user_id`, the SDK will submit this information to the Apphud server.
795-
The server will return a premium web user if found; otherwise, the callback will return `false`.
794+
If the `data` parameter contains either `aph_user_id`, `apphud_user_id`, `email` or `apphud_user_email`, the SDK will submit this information to the Apphud server.
795+
The server will return a restored web user if found; otherwise, the callback will return `false`.
796+
797+
__Important:__ If the callback returns `true`, it doesn't mean the user has premium access, you should still call `Apphud.hasPremiumAccess()`.
796798

797-
Additionally, the delegate methods `apphudSubscriptionsUpdated` and `apphudDidChangeUserID` will be called.
799+
Additionally, the delegate methods `apphudSubscriptionsUpdated` and `apphudDidChangeUserID` may be called.
798800

799801
The callback returns `true` if the user is successfully attributed via the web and includes the updated `ApphudUser` object.
800802
After this callback, you can check the `Apphud.hasPremiumAccess()` method, which should return `true` if the user has premium access.

0 commit comments

Comments
 (0)