Import the library
Import the Golang Tracker library like so:
Code language: PHP (php)
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):
Code language: PHP (php)
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
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:
|The emitter to which events are sent||Yes|
|The user being tracked||No|
|The name of the tracker instance||No||“|
|The application ID||No||“|
|The platform the Tracker is running on||No|
|Whether to enable base 64 encoding||No|
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), )
Accepts an argument of an Emitter instance pointer; if the object is
panic. See Emitters for more on emitter configuration.
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.
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.
appId argument lets you set the application ID to any string.
By default we assume the Tracker will be running in a server environment. To override this provide your own platform string.
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
false passed in.