Skip to content

Go Ampli Wrapper (Beta)

Overview

The Ampli Wrapper is a generated, strongly typed API for tracking Analytics events based on your Tracking Plan in Amplitude Data. The tracking library exposes a function for every event in your team’s tracking plan. The function’s arguments correspond to the event’s properties.

Ampli can benefit your app by providing autocompletion for events & properties defined in Data and enforce your event schemas in code to prevent bad instrumentation.

Amplitude Data supports tracking analytics events from Go apps. The generated tracking library is packaged as a Go package.

Go Ampli Resources (Beta)

Examples · Releases

Ampli Wrapper versus the Amplitude SDK

We recommend using the Ampli wrapper for all the benefits mentioned above. However, if you want to send events without creating a tracking plan in Amplitude Data, you can learn more about the underlying Amplitude SDK in our SDK Quickstart Guide. Click here for more documentation on the Amplitude Go SDK.

Quick Start

  1. (Prerequisite) Create a Tracking Plan in Amplitude Data

    Plan your events and properties in Amplitude Data. See detailed instructions here

  2. Install the Amplitude SDK

    go get https://github.com/amplitude/analytics-go
    
  3. Install the Ampli CLI

    npm install -g @amplitude/ampli
    
  4. Pull the Ampli Wrapper into your project

    ampli pull [--path ./ampli]
    
  5. Initialize the Ampli Wrapper

    import "<your-module-name>/ampli"
    
    ampli.Instance.Load(ampli.LoadOptions{
        Environment: ampli.EnvironmentProduction,
    })
    
  6. Identify users and set user properties

    ampli.Instance.Identify(userID, ampli.Identify.Builder().
        UserProp("A trait associated with this user").Build()
    )
    
  7. Track events with strongly typed methods and classes

    ampli.Instance.SongPlayed("user_id", SongPlayed().Builder().SongId("song-1").Build())
    ampli.Instance.Track("user_id", SongFavorited().Builder().SongId("song-2").Build())
    
  8. Flush events before application exit

    ampli.Instance.Flush()
    
  9. Verify implementation status with CLI

    ampli status [--update]
    

Installation

Install the Amplitude SDK

If you haven't already, install the core Amplitude SDK dependencies analytics-go using go get:

go get https://github.com/amplitude/analytics-go

Install the Ampli CLI

You can install the Ampli CLI from Homebrew or NPM.

brew tap amplitude/ampli
brew install ampli
npm install -g @amplitude/ampli

Pull the Ampli Wrapper into your project

Run the Ampli CLI pull command to log in to Amplitude Data and download the strongly typed Ampli Wrapper for your tracking plan. Ampli CLI commands are usually run from the project root directory.

ampli pull

API

Load

Initialize Ampli in your code. The Load() method requires a configuration options parameter:

import  "<your-module-name>/ampli"

ampli.Instance.Load(ampli.LoadOptions{
    Environment: ampli.EnvironmentProduction,
})
Arg of load()
Description
options Required. A instance of LoadOptions. Specifies configuration options for the Ampli Wrapper.
Arg of LoadOptions
Description
Environment Required. String. Specifies the environment the Ampli Wrapper is running in. For example, EnvironmentProduction or EnvironmentDevelopment. Create, rename, and manage environments in Amplitude Data.

Environment determines which API token is used when sending events.

If a Client.ApiKey or Client.Instance is provided, Environment is ignored, and can be omitted.
Disabled Specifies whether the Ampli Wrapper does any work. When true, all calls to the Ampli Wrapper are no-ops. Useful in local or development environments.
Environment Specifies the environment the Ampli Wrapper runs in: either EnvironmentDevelopment or EnvironmentProduction. Environment determines which Access Token is used to load the underlying analytics provider libraries.
Client A instance of LoadClientOptions specifies configuration options for the Amplitude core SDK client.
Arg of LoadClientOptions
Description
Instance Specifies an Amplitude instance. By default Ampli creates an instance for you.
APIKey Specifies an API Key. This option overrides the default, which is the API Key configured in your tracking plan.
Configuration Specifies the Amplitude configuration. This option overrides the default configuration.

Identify

Call Identify() to identify a user in your app and associate all future events with their identity, or to set their properties.

Just as Ampli creates types for events and their properties, it creates types for user properties.

The Identify() function accepts a string userID, an Identify event instance, and amplitude.EventOptions.

All properties are passed in as parameters of methods to ampli.Identify.Builder(). For example your tracking plan only contains a required user property called role. The property's type is a string.

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

The options argument allows you to pass Amplitude fields for this call, such as DeviceID.

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

Group Identify

Feature availability

This feature is available in accounts with a Growth or Enterprise plan with the Accounts add-on.

Call GroupIdentify() to identify a group in your app and set/update group properties.

Just as Ampli creates types for events and their properties, it creates types for group properties.

The GroupIdentify() function accepts a string groupType, a string groupName, an Group event instance, and EventOptions.

For example your tracking plan contains a group sport:football has a required property called totalMember. The property's type is a int.

ampli.Instance.GroupIdentify(
    "sport",
    "football",
    ampli.NewGroup(23),
    amplitude.EventOptions{},
)

The same with NewIdentify(), if your tracking plan contains an optional property called totalMember.

ampli.Instance.GroupIdentify(
    "sport",
    "football",
    ampli.NewGroup().OptionalTotalMember(23),
    amplitude.EventOptions{},
)

Set group

Call SetGroup() to associate a user with their group (for example, their department or company). The SetGroup() function accept userID groupType, groupName and EventOptions.

ampli.Instance.Client.SetGroup("user-id", "sport", []string{"football"}, amplitude.EventOptions{})

Multiple group names can be set at once.

ampli.Instance.Client.SetGroup("user-id", "sport", []string{"football", "basketball"}, amplitude.EventOptions{})

Track

To track an event, call the event's corresponding function. Every event in your tracking plan gets its own function in the Ampli Wrapper. The call is structured like this:

ampli.Instance.EventName(userID, ampli.EventName.Builder().eventProp(true).Build(), amplitude.EventOptions{})

EventOptions argument allows you to pass Amplitude fields, like DeviceID.

For example, in the following code snippet, your tracking plan contains an event called songPlayed. The event is defined with two required properties: songId and songFavorited. The property type for songId is string, and songFavorited is a boolean.

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

Ampli also generates a class for each event. Use NewEventName() to get the corresponding class for each event.

SongPlayed().Builder()
    .SongId("songId")
    .SongPlayed(true)
    .Build()

Send event objects using the generic track method.

ampli.Instance.Track("user-id", SongPlayed().Builder()
    .SongId("songId")
    .SongPlayed(true)
    .Build(),
    amplitude.EventOptions{},
)

Flush

The Ampli wrapper queues events and sends them on an interval based on the configuration.

Call Flush() to immediately send any pending events.

The Flush() method returns a promise that can be used to ensure all pending events have been sent before continuing. This can be useful to call prior to application exit.

ampli.Instance.Flush()

Plugin

Plugins allow you to extend the Amplitude behavior, for example, modifying event properties (enrichment type) or sending to a third-party APIs (destination type).

First you need to define your plugin: plugin examples.

Add your plugin after init Ampli:

ampli.Instance.Client.Add(myDestinationPlugin)

Ampli CLI

Pull

The pull command downloads the Ampli Wrapper code to your project. Run the pull command from the project root.

ampli pull

You will be prompted to log in to your workspace and select a source.

 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

Learn more about ampli pull.

Status

Verify that events are implemented in your code with the status command:

ampli status [--update]

The output displays status and indicates what events are missing.

➜ 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

Learn more about ampli status.


Still have questions? Ask them in the Community.