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.

Ampli for Go

Amplitudeデータは、Goアプリからのアナリティクスイベントの追跡をサポートしています。生成されたトラッキングライブラリはGoパッケージとしてパッケージ化されます。

クイックスタート

  1. (前提条件)Amplitudeデータでトラッキングプランを作成する

    Amplitudeデータでイベントやプロパティを計画しましょう。詳細な手順については、トラッキングプランの作成を参照してください。

  2. Amplitude SDKをインストールする

    shell
    go get github.com/amplitude/analytics-go
    
  3. Ampli CLIをインストールする

    shell
    npm install -g @amplitude/ampli
    
  4. Ampliラッパーをプロジェクトに組み込む

    shell
    ampli pull [--path ./ampli]
    
  5. Ampliラッパーを初期化する

    go
    import "<your-module-name>/ampli"
    ampli.Instance.Load(ampli.LoadOptions{
        Client: ampli.LoadClientOptions{
            Configuration: ampli.NewClientConfig(AMPLITUDE_API_KEY),
        },
    })
    
  6. ユーザーを識別し、ユーザープロパティを設定する

    go
    ampli.Instance.Identify(userID, ampli.Identify.Builder().
        UserProp("A trait associated with this user").
        Build(),
    )
    
  7. 強力に型付けされたメソッドとクラスを使用してイベントを追跡する

    go
    ampli.Instance.SongPlayed("user_id", ampli.SongPlayed.Builder().SongId("song-1").Build())
    ampli.Instance.Track("user_id", ampli.SongFavorited.Builder().SongId("song-2").Build())
    
  8. アプリケーション終了前のイベントのフラッシュ

    go
    ampli.Instance.Flush()
    
  9. CLIを使用して実装ステータスを確認する

    shell
    ampli status [--update]
    

SDKをインストールする

まだインストールしていない場合は、go getを使用してコアとなるAmplitude SDKの依存関係analytics-goをインストールしてください。

shell
go get github.com/amplitude/analytics-go

Ampli CLIをインストールする

HomebrewまたはNPMからAmpli CLIをインストールします。

brew tap amplitude/ampli
brew install ampli

Ampliラッパーをプロジェクトに組み込む

Ampli CLI pullコマンドを実行してAmplitudeデータにログインし、トラッキングプラン用の強く型付けされたAmpliラッパーをダウンロードしてください。プロジェクトのルートディレクトリからAmpli CLIコマンドを実行します。

bash
ampli pull

Ampli API

Ampliは以下のメソッドをサポートしています。

ロード

コードでAmpliを初期化します。Load()メソッドには、次の設定オプションパラメータが必要です。

Go
import  "<your-module-name>/ampli"
ampli.Instance.Load(ampli.LoadOptions{
  Client: ampli.LoadClientOptions{
    Configuration: ampli.NewClientConfig(AMPLITUDE_API_KEY),
  },
})

Identify

アプリ内のユーザーを識別し、今後のすべてのイベントをそのユーザーのIDに関連付けるか、ユーザーのプロパティを設定するには、Identify()を呼び出します。

Ampliは、イベントやイベントプロパティと同じように、ユーザープロパティにもタイプを作成します。

Identify()関数は、文字列userID、Identifyイベントインスタンス、およびオプションのamplitude.EventOptionsを受け取ります。

すべてのプロパティをampli.Identify.Builder()のメソッドのパラメータとして渡します。たとえば、トラッキングプランにroleという必須のユーザープロパティが含まれているとします。プロパティのタイプは文字列です。

Go
ampli.Instance.Identify(
    "user_id",
    ampli.Identify.Builder().Role("admin").Build(),
)

オプションの引数を使用すると、DeviceIDなどのAmplitudeフィールドを渡すことができます。

Go
ampli.Instance.Identify(
    "user_id",
    ampli.Identify.Builder().Role("admin").Build(),
    amplitude.EventOptions{
        DeviceID: "device_id",
    },
)

グループ

Amplitude Go SDKとGo Ampli Wrapperはグループをサポートしていません。 この機能をご希望の場合は、Amplitudeダッシュボード上のウィジェットから機能リクエストを送信するか、サポートチケットから送信してください。

追跡

イベントを追跡するには、そのイベントに対応する関数を呼び出します。 トラッキングプラン内のすべてのイベントには、Ampliラッパー内にそれぞれ専用の関数が生成されます。この呼び出しは次の構造になっています。

Go
ampli.Instance.EventName(userID, ampli.EventName.Builder().EventProp(true).Build())

オプションのEventOptions引数を使用すると、DeviceIDなどのAmplitudeフィールドを渡すことができます。

たとえば、次のコードでは、トラッキングプランにsongPlayedというイベントが含まれています。このイベントにはsongIdおよびsongFavoritedという2つの必須プロパティがあります。songIdのプロパティタイプは文字列で、songFavoritedはブール値です。

Go
ampli.Instance.SongPlayed("user_id", ampli.SongPlayed.Builder().
    SongId("songId").
    SongPlayed(true).
    Build(),
    amplitude.EventOptions{}
)

また、Ampli は各イベント用のビルダーも生成します。 EventName.Builder()を使用して、各イベントに対応するビルダーを取得します。

Go
ampli.Instance.SongPlayed.Builder().
    SongId("songId").
    SongPlayed(true).
    Build()

汎用Trackメソッドを使用して、イベントオブジェクトを送信します。

Go
ampli.Instance.Track("user-id", ampli.SongPlayed.Builder().
    SongId("songId").
    SongPlayed(true).
    Build(),
)

フラッシュ

Ampliラッパーはイベントをキューに入れ、設定に基づく間隔で送信します。

保留中のイベントをすぐに送信するには、Flush()を呼び出します。

go
ampli.Instance.Flush()

プラグイン

プラグインはAmplitudeの動作を拡張します。 たとえば、プラグインはイベントプロパティを変更したり(エンリッチメントタイプ)、サードパーティ製APIに送信したり(送信先タイプ)できます。

まずはプラグインを定義します。 プラグインの例を参照してください。

Ampli を初期化したあとでプラグインを追加してください。

Go
ampli.Instance.Client.Add(myDestinationPlugin)

Ampli CLI

Pull

pullコマンドはAmpliラッパーコードをプロジェクトにダウンロードします。プロジェクトのルートからpullコマンドを実行します。

bash
ampli pull

Ampliは、ワークスペースにログインしてソースを選択するよう求めます。

bash
 ampli pull
Ampli project is not initialized. No existing `ampli.json` configuration found.
? Create a new Ampli project here? Yes
? Organization: Amplitude
? Workspace: My Workspace
? Source: My Source

ステータス

statusコマンドを使用して、コードがイベントを実装していることを確認します。

bash
ampli status [--update]

出力にはステータスと欠落イベントのリストが表示されます。

bash
 ampli status
 Verifying event tracking implementation in source code
 Song Played (1 location)
 Song Stopped Called when a user stops playing a song.
Events Tracked: 1 missed, 2 total

Was this helpful?