特定のユーザーやユーザーグループに対してプレミアム機能を手動でアンロックしたい場合、Adapty API を使って行うことができます。プロモーションキャンペーン、投資家へのアクセス付与、カスタマーサポートの特別対応などに役立ちます。
このガイドでは、ユーザーを識別してプログラムからアクセスレベルを付与する方法を説明します。
利用シナリオの例
Google Play プロモコード: Google Play のプロモコードを使用した購入は、orderId なしで届く場合があります。Adapty の買い切り購入(サブスクリプション以外)のバリデーションには orderId が必要なため、こうしたコード引き換えは自動的にバリデーション・付与されません。以下の手順で手動でアクセスを付与してください。Server-Side API は orderId に依存しません。
ステップ 1. ユーザーを識別する
Adapty はプラットフォームやデバイスをまたいでユーザーを識別するために customer_user_id を使用します。これにより、アプリの再インストールやデバイス切り替え後もユーザーのアクセスを継続させることができます。
この ID は一度だけ作成します。ユーザーがアプリから初めてサインアップする際に、SDK アクティベーション時に customer user ID を渡すか、サインアップ前に SDK をアクティベートした場合は identify メソッドを使用します。
SDK アクティベーション後に新規ユーザーを識別する場合、SDK はまず匿名プロファイルを作成します(SDK は必ずプロファイルが必要です)。その後 identify を customer user ID で呼び出すと、新しいプロファイルが作成されます。
この動作は正常であり、アナリティクスの精度には影響しません。詳細はこちらをご覧ください。
do {
try await Adapty.identify("YOUR_USER_ID") // Unique for each user
} catch {
// handle the error
}
// User IDs must be unique for each user
Adapty.identify("YOUR_USER_ID") { error in
if let error {
// handle the error
}
}
Adapty.identify("YOUR_USER_ID") { error -> // Unique for each user
if (error == null) {
// successful identify
}
}
// User IDs must be unique for each user
Adapty.identify("YOUR_USER_ID", error -> {
if (error == null) {
// successful identify
}
});
try {
await adapty.identify("YOUR_USER_ID"); // Unique for each user
// successfully identified
} catch (error) {
// handle the error
}
try {
await Adapty().identify(customerUserId); // Unique for each user
} on AdaptyError catch (adaptyError) {
// handle the error
} catch (e) {
}
Adapty.Identify("YOUR_USER_ID", (error) => { // Unique for each user
if(error == null) {
// successful identify
}
});
Adapty.identify("YOUR_USER_ID") // Unique for each user
.onSuccess {
// successful identify
}
.onError { error ->
// handle the error
}
try {
await adapty.identify({ customerUserId: "YOUR_USER_ID" });
// successfully identified
} catch (error) {
// handle the error
}
ステップ 2. API でアクセスレベルを付与する
ユーザーが customer_user_id で識別されたら、サーバーサイド API を使ってアクセスレベルを付与できます。この API 呼び出しにより、ユーザーは実際に購入することなく有料機能にアクセスできるようになります。
メソッドの完全なリファレンスはこちらをご覧ください。
Adapty ダッシュボードでカスタム属性(例:Beta tester や Investor)を追加することで、ユーザーアクセスを管理できます。
アプリ起動時にユーザープロファイルのこの属性を確認してアクセスを自動的に付与します。
アクセスを更新するには、ダッシュボードで属性を変更するだけです。
curl --request POST \
--url https://api.adapty.io/api/v2/server-side-api/purchase/profile/grant/access-level/ \
--header 'Accept: application/json' \
--header 'Authorization: Api-Key YOUR_SECRET_API_KEY' \
--header 'Content-Type: application/json' \
--header 'adapty-customer-user-id: CUSTOMER_USER_ID' \
--data '{
"access_level_id": "YOUR_ACCESS_LEVEL"
}'
ステップ 3. アプリ内でアクセスを確認する
API でアクセスを付与すると、ユーザーのプロファイルは自動的に更新されます。プロファイルを取得してサブスクリプションの状態を確認し、プレミアム機能をアンロックしてください。
do {
let profile = try await Adapty.getProfile()
if profile.accessLevels["YOUR_ACCESS_LEVEL_ID"]?.isActive ?? false {
// grant access to premium features
}
} catch {
// handle the error
}
Adapty.getProfile { result in
if let profile = try? result.get() {
// check the access
if profile.accessLevels["YOUR_ACCESS_LEVEL_ID"]?.isActive ?? false {
// grant access to premium features
}
}
}
Adapty.getProfile { result ->
when (result) {
is AdaptyResult.Success -> {
val profile = result.value
// check the access
if (profile.accessLevels["YOUR_ACCESS_LEVEL_ID"]?.isActive == true) {
// grant access to premium features
}
}
is AdaptyResult.Error -> {
val error = result.error
// handle the error
}
}
}
Adapty.getProfile(result -> {
if (result instanceof AdaptyResult.Success) {
AdaptyProfile profile = ((AdaptyResult.Success<AdaptyProfile>) result).getValue();
// check the access
if (profile.getAccessLevels().get("YOUR_ACCESS_LEVEL_ID") != null && profile.getAccessLevels().get("YOUR_ACCESS_LEVEL_ID").getIsActive()) {
// grant access to premium features
}
} else if (result instanceof AdaptyResult.Error) {
AdaptyError error = ((AdaptyResult.Error) result).getError();
// handle the error
}
});
try {
const profile = await adapty.getProfile();
// check the access
if (profile.accessLevels["YOUR_ACCESS_LEVEL_ID"]?.isActive) {
// grant access to premium features
}
} catch (error) {
// handle the error
}
try {
final profile = await Adapty().getProfile();
// check the access
if (profile.accessLevels["YOUR_ACCESS_LEVEL_ID"]?.isActive ?? false) {
// grant access to premium features
}
} on AdaptyError catch (adaptyError) {
// handle the error
} catch (e) {
}
Adapty.GetProfile((profile, error) => {
if (error != null) {
// handle the error
return;
}
// check the access
if (profile.AccessLevels["YOUR_ACCESS_LEVEL_ID"]?.IsActive ?? false) {
// grant access to premium features
}
});
Adapty.getProfile()
.onSuccess { profile ->
// check the access
if (profile.accessLevels["YOUR_ACCESS_LEVEL_ID"]?.isActive == true) {
// grant access to premium features
}
}
.onError { error ->
// handle the error
}
try {
const profile = await adapty.getProfile();
// check the access
if (profile.accessLevels["YOUR_ACCESS_LEVEL_ID"]?.isActive) {
// grant access to premium features
}
} catch (error) {
// handle the error
}