이 페이지에서

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(),
)

options 인수를 사용하여 이 호출에 대한 Amplitude 필드를 전달하십시오(예: DeviceID).

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

그룹

Amplitude Go SDK 및 Go Ampli 래퍼는 그룹을 지원하지 않습니다. 이 기능을 원하시면 Amplitude 대시보드의 위젯을 통해 기능 요청을 제출하시거나 지원 티켓을 통해 요청을 제출하십시오.

추적

이벤트를 추적하려면 이벤트에 해당하는 함수를 호출하십시오. 추적 계획의 모든 이벤트는 Ampli 래퍼에서 고유한 함수를 갖습니다. 호출은 다음과 같은 구조를 갖습니다.

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

선택적 EventOptions 인수를 사용하면 DeviceID와 같은 Amplitude 필드를 전달할 수 있습니다.

예를 들어 다음 코드 조각에서 추적 계획에는 songPlayed이라는 이벤트가 포함되어 있습니다. 이벤트에는 두 가지 필수 속성이 있습니다. songId 및 songFavorited. 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

이 내용이 도움이 되었나요?