Amplitude Data supports tracking analytics events from Go apps. The generated tracking library is packaged as a Go package.
(Prerequisite) Create a Tracking Plan in Amplitude Data
Plan your events and properties in Amplitude Data. See detailed instructions here
1go get github.com/amplitude/analytics-go
1npm install -g @amplitude/ampli
Pull the Ampli Wrapper into your project
1ampli pull [--path ./ampli]
1import "<your-module-name>/ampli"2 3ampli.Instance.Load(ampli.LoadOptions{4 Client: ampli.LoadClientOptions{5 Configuration: ampli.NewClientConfig(AMPLITUDE_API_KEY),6 },7})
Identify users and set user properties
1ampli.Instance.Identify(userID, ampli.Identify.Builder().2 UserProp("A trait associated with this user").3 Build(),4)
Track events with strongly typed methods and classes
1ampli.Instance.SongPlayed("user_id", ampli.SongPlayed.Builder().SongId("song-1").Build())2ampli.Instance.Track("user_id", ampli.SongFavorited.Builder().SongId("song-2").Build())
Flush events before application exit
1ampli.Instance.Flush()
Verify implementation status with CLI
1ampli status [--update]
If you haven't already, install the core Amplitude SDK dependencies analytics-go
using go get
:
1go get github.com/amplitude/analytics-go
You can install the Ampli CLI from Homebrew or NPM.
1brew tap amplitude/ampli2brew install ampli
1npm install -g @amplitude/ampli
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.
1ampli pull
Ampli supports the following methods.
Initialize Ampli in your code. The Load()
method requires a configuration options parameter:
1import "<your-module-name>/ampli"2 3ampli.Instance.Load(ampli.LoadOptions{4 Client: ampli.LoadClientOptions{5 Configuration: ampli.NewClientConfig(AMPLITUDE_API_KEY),6 },7})
Arg of load() | Description |
---|---|
options |
Required. A instance of LoadOptions. Specifies configuration options for the Ampli Wrapper. |
Arg of LoadOptions |
Description |
---|---|
Instance |
Required if APIKey isn't set. Specifies an Amplitude instance. By default Ampli creates an instance for you. |
APIKey |
Required if Instance isn't set. Specifies an API Key. This option overrides the default, which is the API Key configured in your tracking plan. |
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. |
Client |
A instance of LoadClientOptions specifies configuration options for the Amplitude core SDK client. |
Arg of LoadClientOptions |
Description |
---|---|
Configuration |
Specifies the Amplitude configuration. This option overrides the default configuration. |
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 optional 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.
1ampli.Instance.Identify(2 "user_id",3 ampli.Identify.Builder().Role("admin").Build(),4)
The options argument allows you to pass Amplitude fields for this call, such as DeviceID
.
1ampli.Instance.Identify(2 "user_id",3 ampli.Identify.Builder().Role("admin").Build(),4 amplitude.EventOptions{5 DeviceID: "device_id",6 },7)
The Amplitude Go SDK and Go Ampli Wrapper don't support Groups. If you're interested in this feature, submit a feature request through the widget on the Amplitude dashboard, or through a support ticket.
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:
1ampli.Instance.EventName(userID, ampli.EventName.Builder().EventProp(true).Build())
Optional 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.
1ampli.Instance.SongPlayed("user_id", ampli.SongPlayed.Builder().2 SongId("songId").3 SongPlayed(true).4 Build(),5 amplitude.EventOptions{}6)
Ampli also generates a builder for each event. Use EventName.Builder()
to get the corresponding builder for each event.
1ampli.Instance.SongPlayed.Builder().2 SongId("songId").3 SongPlayed(true).4 Build()
Send event objects using the generic track method.
1ampli.Instance.Track("user-id", ampli.SongPlayed.Builder().2 SongId("songId").3 SongPlayed(true).4 Build(),5)
The Ampli wrapper queues events and sends them on an interval based on the configuration.
Call Flush()
to immediately send any pending events.
1ampli.Instance.Flush()
Plugins allow you to extend the Amplitude behavior, for example, modifying event properties (enrichment type) or sending to third-party APIs (destination type).
First you need to define your plugin: plugin examples.
Add your plugin after init Ampli:
1ampli.Instance.Client.Add(myDestinationPlugin)
The pull
command downloads the Ampli Wrapper code to your project. Run the pull
command from the project root.
1ampli pull
You will be prompted to log in to your workspace and select a source.
1➜ ampli pull2Ampli project is not initialized. No existing `ampli.json` configuration found.3? Create a new Ampli project here? Yes4? Organization: Amplitude5? Workspace: My Workspace6? Source: My Source
Verify that events are implemented in your code with the status command:
1ampli status [--update]
The output displays status and indicates what events are missing.
1➜ ampli status2✘ Verifying event tracking implementation in source code3 ✔ Song Played (1 location)4 ✘ Song Stopped Called when a user stops playing a song.5Events Tracked: 1 missed, 2 total
Thanks for your feedback!
June 18th, 2024
Need help? Contact Support
Visit Amplitude.com
Have a look at the Amplitude Blog
Learn more at Amplitude Academy
© 2024 Amplitude, Inc. All rights reserved. Amplitude is a registered trademark of Amplitude, Inc.