User identification
This section covers subscription transfer and event handling during user authentication.
Overview
Accurately identifying users within your application is essential for delivering personalized experiences and managing user-specific data. The Purchasely SDK provides robust tools for user identification.
Anonymous users
Handling anonymous purchasing
The Purchasely SDK automatically generates and assigns an anonymous_user_id to each user, maintaining consistency as long as the app remains installed on the device.
Your app can retrieve the anonymous_user_id by calling the following method of the SDK :
Purchasely.anonymousUserIdPurchasely.anonymousUserIdPurchasely.getAnonymousUserId();Purchasely.anonymousUserId;Purchasely.getAnonymousUserId((anonymousId) => {
console.log("Purchasely anonymous Id: " + anonymousId);
});private PurchaselyRuntime.Purchasely _purchasely;
_purchasely.GetAnonymousUserId();Logged-in users
To login a user, just provides your user id. Purchasely will save this user id for all sessions moving forward until you call Purchasely.userLogout() or the user uninstall the application.
- If the user is already logged-in when the SDK starts, you can provide the user_id directly in the
Purchasely.start()method.
import Purchasely
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Purchasely.start(
withAPIKey: "<<X-API-KEY>>",
appUserId: "XYZ-123-ABC-456" // user ID
logLevel: .debug
) {(success, error) in
print(success)
}
return true
}import android.app.Application
import io.purchasely.ext.Purchasely
import io.purchasely.google.GoogleStore
class YourApplication: Application() {
override fun onCreate() {
super.onCreate()
Purchasely.Builder(applicationContext)
.apiKey("<<X-API-KEY>>")
.userId("XYZ-123-ABC-456") // user ID
.build()
.start { isConfigured, error ->
if(isConfigured) {
// Purchasely setup is complete
)
}
}
}
import Purchasely from 'react-native-purchasely';
// Everything is optional except apiKey and storeKit1
// Example with default values
try {
const configured = await Purchasely.start({
apiKey: '<<X-API-KEY>>',
logLevel: LogLevels.ERROR, // set to debug in development mode to see logs
userId: "XYZ-123-ABC-456" // user ID
});
} catch (e) {
console.log("Purchasely SDK not configured properly");
}// Everything is optional except apiKey and storeKit1
// Example with default values
bool configured = await Purchasely.start(
apiKey: '<<X-API-KEY>>',
logLevel: PLYLogLevel.error, // set to debug in development mode to see logs
userId: "XYZ-123-ABC-456" // user ID
);
if (!configured) {
print('Purchasely SDK not configured');
return;
}/***
* @params String apiKey
* @params StringArray stores : may be Google, Amazon and Huawei
* @params String userId
* @params Purchasley.LogLevel logLevel
* @params Purchasely.RunningMode runningMode
**/
Purchasely.startWithAPIKey(
'<<X-API-KEY>>',
['Google'],
"XYZ-123-ABC-456", // user ID
Purchasely.LogLevel.DEBUG
);- When the user logs-in, you can call the following method whenever you want and as much as you want, no network connection is required, the user id is saved directly if the SDK detect it has changed.
Purchasely.userLogin(with: "123456789")Purchasely.userLogin("123456789")Purchasely.userLogin('123456789');Purchasely.userLogin('123456789');Purchasely.userLogin("123456789", (shouldRefresh) => {
if (shouldRefresh) {
// You should call your backend to refresh user entitlements
}
});Purchasely.UserLogin("123456789", (shouldRefresh) => {
if (shouldRefresh) {
// You should call your backend to refresh user entitlements
}
});Sign out users
To sign out user with Purchasely, you just need to call Purchasely.userLogout(). The user ID registered will be removed and Purchasely will use the auto generated anonymous_user_id to assign a variant for your A/B tests and to link a purchase.
All built-in attributes and customer user attributes will also be cleared by calling this method, you can prevent Purchasely from clearing your customer user attributes by adding false as argument of the method.
Purchasely.userLogout()
// To prevent Purchasely from removing all custom user attributes
Purchasely.userLogout(false)Purchasely.userLogout()
// To prevent Purchasely from removing all custom user attributes
Purchasely.userLogout(clearUserAttributes = false)Purchasely.userLogout();Purchasely.userLogout();Purchasely.userLogout();Purchasely.UserLogout();Updated about 4 hours ago