Skip to content

Commit

Permalink
improve web2web, add ability to restore by email (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
ren6 authored Nov 15, 2024
1 parent 5e27d38 commit 4b0cae3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ApphudSDK.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'ApphudSDK'
s.version = '3.5.5'
s.version = '3.5.6'
s.summary = 'Build and Measure In-App Subscriptions on iOS.'
s.description = 'Apphud covers every aspect when it comes to In-App Subscriptions from integration to analytics on iOS and Android.'
s.homepage = 'https://github.com/apphud/ApphudSDK'
Expand Down
43 changes: 29 additions & 14 deletions Sources/Internal/ApphudInternal+Attribution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,30 +220,45 @@ extension ApphudInternal {

@MainActor
internal func tryWebAttribution(attributionData: [AnyHashable: Any], completion: @escaping (Bool, ApphudUser?) -> Void) {
let userId = attributionData["aph_user_id"] ?? attributionData["apphud_user_id"]
if let userId = userId as? String, !userId.isEmpty {

if (currentUser?.userId == userId) {
apphudLog("Already web2web user, skipping")
completion(true, currentUser)
return
}

apphudLog("Found a match from web click, updating User ID to \(userId)", forceDisplay: true)
self.updateUser(fields: ["user_id": userId, "from_web2web": true]) { (result, _, data, _, _, _, attempts) in

let userId = (attributionData["aph_user_id"] ?? attributionData["apphud_user_id"]) as? String ?? ""
let email = (attributionData["email"] ?? attributionData["apphud_user_email"]) as? String ?? ""

if userId.isEmpty && email.isEmpty {
completion(false, currentUser)
return
}

if (email.isEmpty && currentUser?.userId == userId) {
apphudLog("Already web2web user, skipping")
completion(true, currentUser)
return
}

var params: [String: Any] = ["from_web2web": true]
if !userId.isEmpty {
params["user_id"] = userId
}
if !email.isEmpty {
params["email"] = email
}

apphudLog("Found a match from web click, updating User ID to \(userId)", forceDisplay: true)
self.performWhenUserRegistered {
self.updateUser(fields: params) { (result, _, data, _, _, _, attempts) in
if result {
Task {
await self.parseUser(data: data)
let changes = await self.parseUser(data: data)

Task { @MainActor in
self.notifyAboutUpdates(changes)
completion(true, self.currentUser)
}
}
} else {
completion(false, self.currentUser)
}
}
} else {
completion(false, currentUser)
}
}
}
10 changes: 6 additions & 4 deletions Sources/Public/Apphud.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Foundation
import UserNotifications
import SwiftUI

internal let apphud_sdk_version = "3.5.5"
internal let apphud_sdk_version = "3.5.6"

// MARK: - Initialization

Expand Down Expand Up @@ -791,10 +791,12 @@ final public class Apphud: NSObject {

/**
Web-to-Web flow only. Attempts to attribute the user with the provided attribution data.
If the `data` parameter contains either `aph_user_id` or `apphud_user_id`, the SDK will submit this information to the Apphud server.
The server will return a premium web user if found; otherwise, the callback will return `false`.
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.
The server will return a restored web user if found; otherwise, the callback will return `false`.
__Important:__ If the callback returns `true`, it doesn't mean the user has premium access, you should still call `Apphud.hasPremiumAccess()`.
Additionally, the delegate methods `apphudSubscriptionsUpdated` and `apphudDidChangeUserID` will be called.
Additionally, the delegate methods `apphudSubscriptionsUpdated` and `apphudDidChangeUserID` may be called.
The callback returns `true` if the user is successfully attributed via the web and includes the updated `ApphudUser` object.
After this callback, you can check the `Apphud.hasPremiumAccess()` method, which should return `true` if the user has premium access.
Expand Down

0 comments on commit 4b0cae3

Please sign in to comment.