Set up a Go source in your dashboard. You should be able to see a write key for this source:
You will also need a data plane URL. Refer to the Dashboard Overview guide for more information on the data plane URL and where to find it.
The Setup tab in the RudderStack dashboard (seen above) has the SDK installation snippet containing both the write key and the data plane URL. Copy it to integrate the Go SDK into your application.
Installing the Go SDK
To install the Go SDK in the GOPATH, run the following command:
gogetgithub.com/rudderlabs/analytics-go
Initializing the SDK
Run the following code snippet to initialize the Go SDK:
packagemainimport("github.com/rudderlabs/analytics-go/v4")funcmain(){// Instantiates a client to use send messages to the RudderStack API.
// Use your write key in the below placeholder:
client:=analytics.New(<WRITE_KEY>,<DATA_PLANE_URL>)// Enqueues a track event that will be sent asynchronously.
client.Enqueue(analytics.Track{UserId:"1hKOmRA4GRlm",Event:"Test Event",})// Flushes any queued messages and closes the client.
client.Close()}
Alternatively, you can run the following snippet:
packagemainimport("github.com/rudderlabs/analytics-go/v4")funcmain(){// Instantiates a client to use send messages to the RudderStack API.
// Enter your write key in the below placeholder:
client,_:=analytics.NewWithConfig(WRITE_KEY,analytics.Config{DataPlaneUrl:DATA_PLANE_URL,Interval:30*time.Second,BatchSize:100,Verbose:true,DisableGzip:false,// Set to true to disable Gzip compression.
})// Enqueues a track event that will be sent asynchronously.
client.Enqueue(analytics.Track{UserId:"1hKOmRA4GRlm",Event:"Test Event",})// Flushes any queued messages and closes the client.
client.Close()}
Migrating to SDK v4
To migrate to the Go SDK v4.1.0, set the data plane URL in Config (as seen in the above section) instead of passing it as an argument.
SDK initialization options
The RudderStack Go SDK provides the following initialization options:
Field
Type
Description
Interval
Time (in sec)
The SDK sends the messages when this flushing interval time has elapsed.
BatchSize
Integer
Total number of messages to be sent in a single batch. Default value is 100.
Verbose
Boolean
When set to true, the client sends more frequent and detailed messages to the logger.
RetryAfter
Time (in sec)
Determines how long the SDK should wait to retry sending the failed messages.
DisableGzip
Boolean
Disables gzip compression of the requests. By default, this is set to false. To disable gzip, set this parameter to true.
NoProxySupport
Boolean
Set this variable to true if you do not use a proxy to send the events.
Setting NoProxySupport to true will avoid RudderStack making calls to the proxy for fetching the total number of nodes in case of a multi-node setup.
Gzipping requests
The gzip compression feature is enabled in the Go SDK version 4.1.0 by default.
The Go SDK automatically gzips requests. However, you can disable this by setting the DisableGzip parameter to true while initializing the SDK:
The DisableGzip parameter requires rudder-serverversion 1.4 or higher. Otherwise, your events might fail.
Sending events
RudderStack does not store or persist the user state in any of the server-side SDKs.
Unlike the client-side SDKs that deal with only a single user at a given time, the server-side SDKs deal with multiple users simultaneously. Therefore, you must specify either the userId or anonymousIdevery time while making any API calls supported by the Go SDK.
Identify
The identify call lets you identify a visiting user and associate them to their actions. It also lets you record the traits about them like their name, email address, etc.
A sample identify call made using the Go SDK is shown below:
The page method parameters are as described below:
Field
Type
Description
userId Required, if anonymousId is absent.
String
Unique identifier for a user in your database.
anonymousId Required, if userId is absent.
String
The SDK automatically sets this identifier in cases where there is no unique identifier for the user.
name Required
String
Name of the viewed page.
properties
Object
An optional dictionary of the properties associated with the viewed page, like url or referrer.
context
Object
An optional dictionary of information that provides context about the event. It is not directly related to the API call.
integrations
Object
An optional dictionary containing the destinations to be enabled or disabled.
timestamp
Timestamp in ISO 8601 format
The timestamp of the event’s arrival.
Screen
The screen call is the mobile equivalent of the page call. It lets you record the screen views on your mobile app along with other relevant information about the screen.
The screen method parameters are as described below:
Field
Type
Description
userId Required, if anonymousId is absent.
String
Unique identifier for a user in your database.
anonymousId Required, if userId is absent.
String
The SDK automatically sets this identifier in cases where there is no unique identifier for the user.
name Required
String
Name of the viewed screen.
properties
Object
An optional dictionary of the properties associated with the viewed screen, like url or referrer.
context
Object
An optional dictionary of information that provides context about the event. It is not directly related to the API call.
integrations
Object
An optional dictionary containing the destinations to be enabled or disabled.
timestamp
Timestamp in ISO 8601 format
The timestamp of the event’s arrival.
Group
The group call lets you link an identified user with a group, such as a company, organization, or an account. It also lets you record any custom traits or properties associated with that group.
A sample group call made using the Go SDK is shown below:
The SDK automatically sets this identifier in cases where there is no unique identifier for the user.
groupId Required
String
Unique identifier of the group in your database.
traits
Object
An optional dictionary of the group’s traits like nameor email.
context
Object
An optional dictionary of information that provides context about the event. It is not directly related to the API call.
integrations
Object
An optional dictionary containing the destinations to be enabled or disabled.
timestamp
Timestamp in ISO 8601 format
The timestamp of the event’s arrival.
Alias
The alias call lets you merge different identities of a known user. It is an advanced method that lets you change the tracked user’s ID explicitly. You can use alias for managing the user’s identity in some of the downstream destinations.
RudderStack supports sending alias events only to select downstream destinations. Refer to the destination-specific documentation for more details.
This site uses cookies to improve your experience while you navigate through the website. Out of
these
cookies, the cookies that are categorized as necessary are stored on your browser as they are as
essential
for the working of basic functionalities of the website. We also use third-party cookies that
help
us
analyze and understand how you use this website. These cookies will be stored in your browser
only
with
your
consent. You also have the option to opt-out of these cookies. But opting out of some of these
cookies
may
have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This
category only includes cookies that ensures basic functionalities and security
features of the website. These cookies do not store any personal information.
This site uses cookies to improve your experience. If you want to
learn more about cookies and why we use them, visit our cookie
policy. We'll assume you're ok with this, but you can opt-out if you wish Cookie Settings.