Skip to main content

Initialization

Import the library‚Äč

Import the Golang Tracker library like so:

import "gopkg.in/snowplow/snowplow-golang-tracker.v2/tracker"

OR

import "github.com/snowplow/snowplow-golang-tracker/v2/tracker" # When using modules

You will need to refer to the package as tracker. If you wish to use something shorter (or if tracker is already taken):

import sp "gopkg.in/snowplow/snowplow-golang-tracker.v2/tracker"

OR

import sp "github.com/snowplow/snowplow-golang-tracker/v2/tracker" # When using modules

The package can now be referred to as sp rather than tracker.

That's it - you are now ready to initialize a tracker instance.

Creating a tracker‚Äč

The simplest tracker initialization only requires you to provide the URI of the collector to which the tracker will log events:

import sp "github.com/snowplow/snowplow-golang-tracker/v2/tracker"

emitter := sp.InitEmitter(sp.RequireCollectorUri("com.acme"))
tracker := sp.InitTracker(sp.RequireEmitter(emitter))

There are other optional builder functions:

Function NameDescriptionRequired?Default
RequireEmitterThe emitter to which events are sentYesnil
OptionSubjectThe user being trackedNonil
OptionNamespaceThe name of the tracker instanceNo``
OptionAppIdThe application IDNo``
OptionPlatformThe platform the Tracker is running onNosrv
OptionBase64EncodeWhether to enable base 64 encodingNotrue

A more complete example:

subject := sp.InitSubject()
emitter := sp.InitEmitter(sp.RequireCollectorUri("com.acme"))
tracker := sp.InitTracker(
sp.RequireEmitter(emitter),
sp.OptionSubject(subject),
sp.OptionNamespace("namespace"),
sp.OptionAppId("app-id"),
sp.OptionPlatform("mob"),
sp.OptionBase64Encode(false),
)

RequireEmitter‚Äč

Accepts an argument of an Emitter instance pointer; if the object is nil will panic. See Emitters for more on emitter configuration.

OptionSubject‚Äč

The user which the Tracker will track. Accepts an argument of a Subject instance pointer.

You don't need to set this during Tracker construction; you can use the tracker.SetSubject() method afterwards. In fact, you don't need to create a subject at all. If you don't, though, your events won't contain user-specific data such as timezone and language.

OptionNamespace‚Äč

If provided, the namespace argument will be attached to every event fired by the new tracker. This allows you to later identify which tracker fired which event if you have multiple trackers running.

OptionAppId‚Äč

The appId argument lets you set the application ID to any string.

OptionPlatform‚Äč

By default we assume the Tracker will be running in a server environment. To override this provide your own platform string.

OptionBase64Encode‚Äč

By default, unstructured events and custom contexts are encoded into Base64 to ensure that no data is lost or corrupted. You can turn encoding on or off using the Boolean OptionBase64Encode function with either true or false passed in.