On this page

For AI agents: a documentation index is available at /docs/llms.txt. Append .md to any page URL for markdown, or send Accept: text/markdown.

SDK ミドルウェア

ミドルウェアを使用すると、すべてのイベントで一連のカスタムコードを実行することでAmplitudeを拡張できます。このパターンは柔軟性が高く、イベントの拡張、変換、フィルタリング、サードパーティの宛先へのルーティングなどをサポートするために使用できます。

メンテナンスSDKはミドルウェアをサポートしていますが、メンテナンスBrowser SDKとレガシーAmpliはサポートされません。プラグインは、最新の SDK および Ampli バージョンのミドルウェアに代わるものです。

ミドルウェアの構造

関数シグネチャ

各ミドルウェアは次のシグネチャを持つ単純な機能です:

js
/**
 * A function to run on the Event stream (each logEvent call)
 *
 * @param payload The `payload` contains the `event` to send and an optional `extra` that lets you pass custom data to your own middleware implementations.
 * @param next Function to run the next middleware in the chain, not calling next will end the middleware chain
 */
function (payload: MiddlewarePayload: next: MiddlewareNext): void;

ミドルウェアの種類:

  • MiddlewarePayload.event
    • タイプ: イベント。
    • 説明:送信するイベントデータです。イベントはプラットフォームによって異なります。
  • MiddlewarePayload.extra
    • :{ [x: string]: any }。
    • 説明: ユーザーがミドルウェアに余分なデータを渡すことを可能にする非構造化オブジェクトです。
  • MiddlewareNext
    • :(payload: MiddlewarePayload) => void。
    • 説明: 各ミドルウェアの末尾で実行され、チェーン内の次のミドルウェアを呼び出す関数です。

キュー内の次のミドルウェアを呼び出すには、next関数を使用します。ミドルウェアチェーンを継続するには、next(payload)を呼び出す必要があります。ミドルウェアが next を呼び出さなかった場合、現在のミドルウェアが完了した後にイベント処理が停止します。

ペイロードのカスタマイズ

イベントフィールドへのミドルウェアのアクセスは、プラットフォームによって異なります。 包括的なアクセスを実現するためには、AmplitudeはAmpliの最新バージョンにアップデートし、プラグイン機能を使用することをお勧めします。

Browser Ampliの場合、以下のアクセス可能なキーは次のpayloadとおりです。

  • event.event_type
    • :文字列。
    • 説明: イベント名です。
  • event.event_properties
    • :{ [key: string]: any }。
    • 説明:イベントプロパティです。
  • event.user_id
    • :文字列。
    • 説明:イベントレベルのユーザーID。
  • event.device_id
    • :文字列。
    • 説明:イベントレベルのデバイス ID。
  • event.user_properties
    • :{ [key: string]: any }。
    • 説明:イベントレベルのユーザープロパティです。
  • extra
    • :{ [x: string]: any }。
    • 説明: 独自のミドルウェア実装にカスタムデータを渡すことができる追加情報です。

他のプラットフォームの場合、ミドルウェアはイベントJSONオブジェクト全体にアクセスして変更することで包括的な調整を行うことができます。完全なスキーマについては、イベント配列キーリファレンスを参照してください。

用途

amplitude.addEventMiddleware()を使用してAmplitudeにミドルウェアを追加します。ミドルウェアは好きなだけ追加できます。 各ミドルウェアは、追加した順序で実行されます。

js
amplitude.addEventMiddleware(yourMiddleware());

ミドルウェアの例

ミドルウェアを使用して、イベントプロパティの変更、データの変換、イベントのフィルタリング、サードパーティの宛先へのルーティングなどを行います。

フィルタリングミドルウェア

ts
    amplitude.addEventMiddleware((payload, next) => {
    const {eventType} =  payload.event;
    if (shouldSendEvent(eventType)) {
    next(payload)
    } else {
    // event doesn't continue to following middleware or get sent to Amplitude
    console.log(`Filtered event: ${eventType}`);
    }
});

PII(個人を特定できる情報)の削除

js
amplitude.addEventMiddleware((payload, next) => {
    const { event } = payload;
    if (hasPii(event.event_properties)) {
    payload.event.event_properties = obfuscate(payload.event.event_properties);
    }
    next(payload);
});

イベントプロパティを強化

js
amplitude.addEventMiddleware((payload, next) => {
    const { event } = payload;
    if (needsDeviceId(event)) {
    payload.event.deviceId = getDeviceId();
    }
    next(payload)
});

Ampliv1を使用してイベントレベルのグループを送信する

この例は、Ampliv1でイベントレベルのグループを送信する方法を示しています。SDKでイベントレベルのグループを送信する場合(Ampli以外)は、動作が異なります。使用方法の詳細については、特定のSDKを確認してください。

js
ampli.addEventMiddleware((payload, next) => {
    const {event, extra} =  payload;
    if (event && extra && event.extra.groups) {
    event.groups =  event.extra.groups;
    }
    next(payload);
});
// Pass the event-level groups info through middleware extra when calling the tracking plan.
const extra = {groups: {"test_group_name": "test_group_value"}};
ampli.eventWithGroups({requiredNumber: 1.23, requiredBoolean: false}, extra);

他のサービスへのデータの転送

js
import amplitude from 'amplitude/sdk'
import adroll from 'adroll';
import segment from 'segment';
import snowplow from 'snowplow';
amplitude.addEventMiddleware((payload, next) => {
    const { event, extra } = payload;
    segment.track(event.event_type, event.event_properties, { extra.anonymousId })
    adroll.track();
    snowplow.track(event.event_type, event.event_properties, extra.snowplow.context);
    // next();
});

クライアント側検証を使用する

js
amplitude.addEventMiddleware((payload, next) => {
    if (isDevelopment && !SchemaValidator.isValid(payload.event)) {
    throw Error(`Invalid event ${event.event_type}`);
    }
    next(payload);
});

サポートされているSDK

Was this helpful?