> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Experiment CenterとAuth0 Actionsの連携

> Auth0 Actionsで実験コンテキストを読み取り、割り当てられたバリエーションに応じてロジックを分岐する方法。

> [!WARNING]
>
> **Auth0 Experiment Center機能は現在、[早期アクセス](/docs/docs/troubleshoot/product-lifecycle/product-release-stages)です。**
>
> この機能は[Enterprise計画](https://auth0.com/pricing)でご利用いただけます。
>
> 参加をご希望の場合は、your Account Executiveまでお問い合わせください。

実験が有効な場合、Experiment Center はサポート対象の Action トリガーに `ExperimentContext` オブジェクトを注入します。

## サポート対象のトリガー

実験コンテキストは、次の Auth0 [Actions](/docs/docs/ja-jp/customize/actions/actions-overview) のトリガーで利用できます。

* [`post_login`](/docs/docs/ja-jp/customize/actions/explore-triggers/post-login) トリガー。
* [`pre_user_registration`](/docs/docs/ja-jp/actions/reference/pre-user-registration/pre-user-registration-event-object) トリガー。
* [`post_user_registration`](/docs/docs/ja-jp/customize/actions/explore-triggers/signup-and-login-triggers/post-user-registration-trigger) トリガー。

## event.experiment オブジェクト

サポート対象のトリガーでは、Experiment Center により、イベントオブジェクトに `experiment` フィールドが追加されます:

```typescript theme={null}
interface ExperimentContext {
  experiment_id: string;   // アクティブな実験ID
  variation_id: string;    // 割り当てられたバリエーションID
  config: {                // マージされた構成：ベースライン＋オーバーライド
    [paramName: string]: { value: unknown };
  };
  is_control: boolean;     // コントロールバリエーションの場合はtrue
}
```

実験がアクティブでない場合 (またはそのテナントで機能が有効になっていない場合) 、`event.experiment` は `null` です (`undefined` ではありません) 。

### config オブジェクトには、完全にマージされた構成が含まれます

`config` オブジェクトには、機能フラグで定義されたすべてのパラメーターが、割り当てられたバリエーションのオーバーライドとマージされた状態で含まれます。ベースライン値を参照したり、フォールバックロジックを書いたりする必要はありません。パラメーターが機能フラグに存在するなら、`config` にも存在します。

## null 安全パターン

プロパティを参照する前に、アクティブな実験かどうかを確認します:

```javascript theme={null}
exports.onExecutePostLogin = async (event, api) => {
  const ec = event.experiment;
  if (!ec) return; // アクティブな実験がない場合は何もしない

  // ここで ec.config、ec.variation_id、ec.is_control に安全にアクセスできる
};
```

このパターンは、3つのサポート対象トリガーすべてで使用してください。早期リターンにより、Actionを簡潔に保ち、実験が実行されていない期間でも正しく動作するようにできます。

## 例: post\_login — 条件付きMFAポリシー

この例では、ブール値パラメーターを読み取り、バリエーションに応じて異なるMFAポリシーを適用します。

```javascript theme={null}
// post_login Action
exports.onExecutePostLogin = async (event, api) => {
  const ec = event.experiment;
  if (!ec) return;

  const enforceMfa = ec.config?.require_mfa?.value;

  if (enforceMfa === true) {
    // トリートメントバリエーション: このユーザーにMFAを強制する
    api.multifactor.enable("any", { allowRememberBrowser: false });
  }
  // コントロールバリエーション: MFA強制の変更なし（ベースラインの動作）
};
```

`treatment variation` のユーザーでは `ec.config.require_mfa.value` は `true`、`コントロールバリエーション` のユーザーでは `false` (ベースライン) になります。フォールバックロジックは不要です。

## 例: post\_login — バリエーション に基づいてカスタムクレームを設定する

この例では、実験の assignment をユーザーの ID トークンにカスタムクレームとして付与します。分析パイプラインによっては、テナントログではなくトークンのクレームを参照するものもあります。

```javascript theme={null}
// post_login Action
exports.onExecutePostLogin = async (event, api) => {
  const ec = event.experiment;
  if (!ec) return;

  // ID トークンに実験の割り当てを追加する
  api.idToken.setCustomClaim("https://example.com/experiment", {
    experiment_id: ec.experiment_id,
    variation_id: ec.variation_id,
  });
};
```

OIDC のカスタムクレーム規約に従い、名前空間付きクレームの URL を使用してください。これにより、受け取り側 (アプリ) はテナントログを照会しなくても、トークンからそのクレームを読み取れます。

## 例: pre\_user\_registration — バリエーション別のメタデータ

この例では、登録フローの実験を使って、登録するユーザーがどのバリエーションに割り当てられたかに応じて `user_metadata` を設定します。

```javascript theme={null}
// pre_user_registration Action
exports.onExecutePreUserRegistration = async (event, api) => {
  const ec = event.experiment;
  if (!ec) return;

  const onboardingVariant = ec.config?.onboarding_variant?.value;

  if (onboardingVariant) {
    api.user.setUserMetadata("onboarding_variant", onboardingVariant);
    api.user.setUserMetadata("onboarding_experiment_id", ec.experiment_id);
  }
};
```

メタデータは登録時に書き込まれるため、セッションをまたいでもユーザーに紐づいたまま保持されます。後でこれを参照して、ユーザーがどのサインアップコホートに属しているかを把握できます。

## 例: post\_user\_registration、ダウンストリームの登録をトリガーする

この例では、新規ユーザーに割り当てられたバリエーションに応じて、登録後に webhook を送信します。

```javascript theme={null}
// post_user_registration Action
exports.onExecutePostUserRegistration = async (event, api) => {
  const ec = event.experiment;
  if (!ec) return;

  const enrollInNewProgram = ec.config?.enroll_welcome_program?.value;

  if (enrollInNewProgram === true) {
    await fetch("https://your-backend.example.com/api/enrollment", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        user_id: event.user.user_id,
        email: event.user.email,
        experiment_id: ec.experiment_id,
        variation_id: ec.variation_id,
      }),
    });
  }
};
```

トリートメント バリエーションのユーザーのみが enrollment webhook をトリガーします。control ユーザーは標準の登録後フローに従います。

## `is_control` を使用する

パラメータ `is_control` は、ユーザーが対照群に属している場合に `true` になります (ベースラインが表示され、overrides は適用されません) 。変更のないエクスペリエンスをどのユーザーが見たかを追跡したい場合や、対照群のユーザーに対する任意の処理をスキップしたい場合に使用します。

```javascript theme={null}
exports.onExecutePostLogin = async (event, api) => {
  const ec = event.experiment;
  if (!ec) return;

  if (!ec.is_control) {
    // 分析システムではトリートメントユーザーのみを追跡する
    // （コントロールユーザーはActionの外で別途追跡される）
    await logTreatmentEvent(ec.experiment_id, ec.variation_id, event.user.user_id);
  }
};
```

挙動に応じて分岐させる場合 (分析目的ではなく) は、`config` パラメータの値を直接確認してください。その方が、より明確で読みやすくなります。
