> For the complete documentation index, see [llms.txt](https://gitbook.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.com/docs/documentation/ja-gitbook-documentation/publish/adaptive-content/enabling-adaptive-content/cookies.md). Agents can also search and read this documentation through its [MCP server](https://gitbook.com/docs/~gitbook/mcp).

# Cookie

公開 Cookie または署名付き Cookie を通じて、訪問者データをドキュメントに渡します

{% hint style="info" %}
ガイドで [詳しい手順](https://gitbook.com/docs/guides/docs-personalization-and-authentication/setting-up-adaptive-content.md) を確認し、Cookie を使ってアダプティブコンテンツを設定しましょう。
{% endhint %}

{% hint style="warning" %}
cookies を使ってアダプティブコンテンツを利用するには、アプリケーションにコードを追加する必要があります。

この方法は、サイトが [カスタムドメイン](https://gitbook.com/docs/documentation/ja-gitbook-documentation/publish/custom-domain.md).
{% endhint %}

訪問者のブラウザ cookie を通じて、訪問者データをドキュメントに渡すことができます。以下は各方法の概要です。

<table data-full-width="false"><thead><tr><th width="335.125">方法</th><th width="266.6015625">ユースケース</th><th width="206.58984375">設定のしやすさ</th><th width="202">セキュリティ</th><th>形式</th></tr></thead><tbody><tr><td>署名付き Cookie <code>gitbook-visitor-token</code></td><td>API テスト用認証情報、顧客識別</td><td>署名とカスタムドメインが必要</td><td><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span> プロパティはバックエンドでのみ定義できます</td><td>JWT</td></tr><tr><td>公開 Cookie <code>gitbook-visitor-public</code></td><td>機能フラグ、ロール</td><td>設定が簡単</td><td><span data-gb-custom-inline data-tag="emoji" data-code="274c">❌</span> 訪問者はプロパティを上書きできます</td><td>JSONだ</td></tr></tbody></table>

### 公開 cookie

公開 Cookie から GitBook にデータを渡すには、公開用の `gitbook-visitor-public` Cookie を設定して、アプリケーションからデータを送信する必要があります。

以下はシンプルな JavaScript の例です:

```javascript
import Cookies from 'js-cookie';

const cookieData = {
  isLoggedIn: true,
  isBetaUser: false,
};

Cookies.set('gitbook-visitor-public', JSON.stringify(cookieData), {
  secure: true,
  domain: '*.acme.org',
})
```

{% hint style="warning" %}
公開 Cookie 経由で渡すデータは、 [署名なし](https://gitbook.com/docs/documentation/ja-gitbook-documentation/publish/adaptive-content/enabling-adaptive-content.md#set-an-unsigned-claim) オブジェクトを使って、ビジタースキーマで定義する必要があります。
{% endhint %}

### 署名済みcookie

より安全に GitBook にデータを渡すには、アプリケーションから [JSON Web Token](https://jwt.io/introduction) として送信し、次の名前の Cookie に格納する必要があります。 `gitbook-visitor-token` この Cookie はドメインに紐付けます。

これを設定するには、アプリケーションのログインフローを次の手順に合わせて調整する必要があります:

{% stepper %}
{% step %}
**ユーザーがアプリケーションにログインしたときに JWT を生成する**

ユーザーが製品にログインするたびに、認証済みユーザー情報の選択した属性を含む JWT を生成します。
{% endstep %}

{% step %}
**サイトの visitor signing key を使用して JWT に署名する**

次に、サイトの **ビジター署名キー**を使って JWT に署名します。このキーは、アダプティブコンテンツを有効にした後、サイトのオーディエンス設定で確認できます。
{% endstep %}

{% step %}
**JWT をワイルドカードのセッション cookie に保存する**

最後に、ユーザー情報を含む署名済みJWTをワイルドカードセッションCookieに保存する必要があります **プロダクトのドメイン配下に**.

たとえば、アプリケーションが `app.acme.org` ドメインで配信されている場合、Cookie は `.acme.org` ワイルドカードドメイン配下に作成する必要があります。
{% endstep %}
{% endstepper %}

以下はシンプルな TypeScript の例です:

```typescript
import * as jose from 'jose';

import { Request, Response } from 'express';

import { getUserInfo } from '../services/user-info-service';
import { getFeatureFlags } from '../services/feature-flags-service';

const GITBOOK_VISITOR_SIGNING_KEY = process.env.GITBOOK_VISITOR_SIGNING_KEY;
const GITBOOK_VISITOR_COOKIE_NAME = 'gitbook-visitor-token';


export async function handleAppLoginRequest(req: Request, res: Response) {
   // ログインリクエストを処理するためのビジネスロジック
   // たとえば、資格情報を確認してユーザーを認証する
   //
   // 例:
   // const loggedInUser = await authenticateUser(req.body.username, req.body.password);

   // ユーザーを認証した後、GitBook に渡したいユーザー情報を
   // データベースまたはユーザーサービスから取得します。
   const userInfo = await getUserInfo(loggedInUser.id);
      
   // ユーザー情報を使って JWT のペイロードを構築する
   const gitbookVisitorClaims = {
       firstName: userInfo.firstName,
       lastName: userInfo.lastName,
       isBetaUser: userInfo.isBetaUser
       products: userInfo.products.map((product) => product.name),
       featureFlags: await getFeatureFlags({userId: loggedInUser.id})
   }
   
   // クレームを使って署名済み JWT を生成する
   const gitbookVisitorJWT = await new jose.SignJWT(gitbookVisitorClaims)
     .setProtectedHeader({ alg: 'HS256' })
     .setIssuedAt()
     .setExpirationTime('2h') // 任意の 2 時間の有効期限
     .sign(GITBOOK_VISITOR_SIGNING_KEY);
     
  // エンコードされた JWT を含む `gitbook-visitor-token` cookie を
  // ログインハンドラーのレスポンスに含める
  res.cookie(GITBOOK_VISITOR_COOKIE_NAME, gitbookVisitorJWT, {
     httpOnly: true,
     secure: process.env.NODE_ENV === 'production',
     maxAge: 2 * 60 * 60 * 1000, // 任意の 2 時間の有効期限
     domain: '.acme.org' //
  });
  
  // ユーザーをアプリにリダイレクトする処理を含む、ログインハンドラーの残りのロジック
  res.redirect('/'); // リダイレクトの例
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gitbook.com/docs/documentation/ja-gitbook-documentation/publish/adaptive-content/enabling-adaptive-content/cookies.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
