---
title: "PostHog"
description: ""
---

PostHog は、ユーザー行動のトラッキング、プロダクト利用状況の可視化、リテンション分析などのツールを提供するアナリティクスプラットフォームです。イベントトラッキング、ユーザーフロー、フィーチャーフラグといった機能を備え、プロダクトをより深く理解して改善するために設計されています。

PostHog と Adapty を連携することで、トライアル開始・更新・解約といったサブスクリプション関連イベントをシームレスにトラッキングできます。これらのイベントを PostHog に送信することで、サブスクリプションの変化がユーザー行動に与える影響を分析したり、ペイウォールのパフォーマンスを評価したり、マネタイズ戦略に関するより深いインサイトを得たりできます — すべて既存のアナリティクスワークフロー内で完結します。

## 連携の特性 \{#integration-characteristics\}

| 連携の特性 | 説明 |
| -------------------------- | ------------------------------------------------------------ |
| スケジュール | リアルタイム。イベントが PostHog ダッシュボードにすぐ表示されない場合があります。 |
| データの方向 | Adapty のイベントは Adapty サーバーから PostHog サーバーへ送信されます。 |
| Adapty の連携ポイント | <ul><li>モバイルアプリのコード内の PostHog と Adapty SDK</li><li>Adapty サーバー</li></ul> |

## PostHog イベントの構造 \{#posthog-event-structure\}

Adapty は、[PostHog 連携ページ](https://app.adapty.io/integrations/posthog)の **Events names** セクションで設定されたイベントを PostHog に送信します。各イベントの構造は以下のとおりです：

```json showLineNumbers
{
  "distinct_id": "john.doe@example.com",
  "timestamp": "2025-01-08T11:06:12+00:00",
  "event": "subscription_started",
  "properties": {
    "$set": {
      "email": "user@example.com",
      "first_name": "John",
      "last_name": "Doe",
      "birthday": "1990-01-01",
      "gender": "male",
      "os": "iOS"
    },
    "timezone": "America/New_York",
    "ip_address": "10.168.1.1",
    "*": "{{other_event_properties}}"
  }
}
```

各パラメーターの説明：

| **パラメーター** | **型** | **説明** |
| --------------- | -------------------- | ------------------------------------------------------------ |
| **distinct_id** | String | ユーザーの一意識別子（例：`profile.posthog_distinct_user_id`、`customer_user_id`、または `profile_id`）。 |
| **timestamp** | ISO 8601 日時 | イベントの日時。 |
| **event** | String | [PostHog 設定](https://app.adapty.io/integrations/posthog)の Events names セクションで定義したイベント名。 |
| **properties** | Object | [properties.$set](posthog#propertiesset-parameters) と [イベント固有のプロパティ](messaging#event-properties)をすべて含みます。各プロパティはオプションであり、値がない場合は PostHog に送信されません。 |

### properties.$set パラメーター \{#propertiesset-parameters\}

`properties.$set` オブジェクトの各パラメーターはオプションであり、値がない場合は PostHog に送信されません。

| **パラメーター** | **型** | **説明** |
| --------------- | -------------------- | ------------------------------------------------------------ |
| **email** | String | ユーザーのメールアドレス。 |
| **first_name** | String | ユーザーの名。 |
| **last_name** | String | ユーザーの姓。 |
| **birthday** | String (Date) | ユーザーの生年月日。 |
| **gender** | String | ユーザーの性別。 |
| **os** | String | ユーザーのデバイスのOS。 |

## PostHog 連携の設定 \{#setting-up-posthog-integration\}

1. Adapty ダッシュボードで [Integrations -> PostHog](https://app.adapty.io/integrations/posthog) ページを開き、トグルを有効にします。

   
     <img src="/assets/shared/img/posthog-on.webp"
     style={{
       border: 'none', /* border width and color */
       width: '700px', /* image width */
       display: 'block', /* for alignment */
       margin: '0 auto' /* center alignment */
     }}
   />
   

2. [PostHog ダッシュボード](https://posthog.com/)にログインします。

3. **Settings -> Project** に移動します。

   
     <img src="/assets/shared/img/posthog-settings.webp"
     style={{
       border: 'none', /* border width and color */
       width: '700px', /* image width */
       display: 'block', /* for alignment */
       margin: '0 auto' /* center alignment */
     }}
   />
   

4. **Project** ウィンドウで下にスクロールして **Project ID** セクションを表示し、**Project API key** をコピーします。

5. APIキーを Adapty ダッシュボードの **Project API key** フィールドに貼り付けます。PostHog にはサーバー間連携専用のサンドボックスモードはありません。

6. **PostHog Deployment** を選択します：

   | オプション | 説明 |
   | ------ | ------------------------------------------------------------ |
   | us/eu | PostHog がホストするデフォルトのデプロイメント。 |
   | Custom | セルフホスト環境向け。**PostHog Instance URL** フィールドにインスタンスのURLを入力します。 |

7. （任意）セルフホストの PostHog デプロイメントを使用している場合は、**PostHog Instance URL** フィールドにデプロイメントのアドレスを入力します。

8. （任意）**Reporting Proceeds**、**Exclude Historical Events**、**Report User's Currency**、**Send Trial Price** などの設定を調整します。これらのオプションの詳細は [Integration settings](configuration#integration-settings) を参照してください。

9. （任意）**Events names** セクションで PostHog に送信するイベントをカスタマイズすることもできます。不要なイベントを無効にしたり、名前を変更したりできます。

10. **Save** をクリックして設定を確定します。

## SDK の設定 \{#sdk-configuration\}

PostHog からアトリビューションデータを受け取るには、以下のように `distinctId` の値を Adapty に渡してください：

---
no_index: true
---

import Callout from '../../../components/Callout.astro';

<Callout type="note">
サードパーティSDKはユーザーIDを非同期で生成します。`Adapty.activate()` の実行時にIDがまだ準備できていない場合があります。**Customer User ID** がこれらのSDKのいずれかから取得される場合は、IDなしで `Adapty.activate()` を呼び出してください。IDが取得できたら、`setIntegrationIdentifier()` を呼び出し、次にCUIDで `identify()` を呼び出してください。
</Callout>

<Tabs groupId="current-os" queryString>

<TabItem value="swift" label="Swift" default>

```swift showLineNumbers
do {
    let distinctId = PostHogSDK.shared.getDistinctId()

    try await Adapty.setIntegrationIdentifier(
        key: "posthog_distinct_user_id",
        value: distinctId
    )
} catch {
    // handle the error
}
```

</TabItem>

<TabItem value="kotlin" label="Kotlin" default>

```kotlin showLineNumbers
Adapty.setIntegrationIdentifier("posthog_distinct_user_id", PostHog.distinctId()) { error ->
    if (error != null) {
        // handle the error
    }

```

 </TabItem>

<TabItem value="java" label="Java" default>

```java showLineNumbers
Adapty.setIntegrationIdentifier("posthog_distinct_user_id", PostHog.distinctId(), error -> {
    if (error != null) {
        // handle the error
    }
});
```

</TabItem>

<TabItem value="flutter" label="Flutter" default>

```javascript showLineNumbers
try {
    final distinctId = await Posthog().getDistinctId();

    await Adapty().setIntegrationIdentifier(
        key: "posthog_distinct_user_id",
        value: distinctId,
    );
} catch (e) {
    // handle the error
}
```

</TabItem>

<TabItem value="unity" label="Unity" default>

Unity 向けの公式 PostHog SDK はありません。

</TabItem>

<TabItem value="rn" label="React Native (TS)" default>

```typescript showLineNumbers

// ...
const posthog = usePostHog();

// ...
try {
  await adapty.setIntegrationIdentifier("posthog_distinct_user_id", posthog.getDistinctId());
} catch (error) {
  // handle `AdaptyError`
}
```

</TabItem>

</Tabs>

Adapty は PostHog へのイベント送信とアトリビューションの受信を行えるようになりました。