-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Example App | ||
|
||
This example shows how to set up the golang tracker to send events to a Snowplow pipeline. | ||
|
||
#### Installation | ||
- Install the golang tracker. | ||
|
||
`$host go get "github.com/snowplow/snowplow-golang-tracker/v3/tracker"` | ||
|
||
#### Usage | ||
Navigate to the example folder. | ||
|
||
`cd examples` | ||
|
||
To send events to your pipeline, run `go run main.go {{your_collector_endpoint}}`. You should see 4 events in your pipleine. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
sphelp "github.com/snowplow/snowplow-golang-tracker/v3/pkg/common" | ||
storagememory "github.com/snowplow/snowplow-golang-tracker/v3/pkg/storage/memory" | ||
sp "github.com/snowplow/snowplow-golang-tracker/v3/tracker" | ||
) | ||
|
||
func getUrlFromArgs() (string, error) { | ||
if len(os.Args) != 2 { | ||
return "", errors.New("collector endpoint is required") | ||
} | ||
return os.Args[1], nil | ||
} | ||
|
||
func main() { | ||
collectorUri, err := getUrlFromArgs() | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
emitter := sp.InitEmitter( | ||
sp.RequireCollectorUri(collectorUri), | ||
sp.RequireStorage(*storagememory.Init()), | ||
sp.OptionRequestType("POST"), | ||
sp.OptionProtocol("http"), | ||
sp.OptionSendLimit(4), | ||
) | ||
|
||
subject := sp.InitSubject() | ||
subject.SetLanguage("en") | ||
subject.SetScreenResolution(1280, 720) | ||
|
||
tracker := sp.InitTracker( | ||
sp.RequireEmitter(emitter), | ||
sp.OptionSubject(subject), | ||
) | ||
|
||
fmt.Println("Sending events to " + emitter.GetCollectorUrl()) | ||
|
||
pageView := sp.PageViewEvent{ | ||
PageUrl: sphelp.NewString("acme.com"), | ||
} | ||
tracker.TrackPageView(pageView) | ||
|
||
screenView := sp.ScreenViewEvent{ | ||
Name: sphelp.NewString("name"), | ||
Id: sphelp.NewString("Screen ID"), | ||
} | ||
tracker.TrackScreenView(screenView) | ||
|
||
structEvent := sp.StructuredEvent{ | ||
Category: sphelp.NewString("shop"), | ||
Action: sphelp.NewString("add-to-basket"), | ||
Property: sphelp.NewString("pcs"), | ||
Value: sphelp.NewFloat64(2), | ||
} | ||
|
||
tracker.TrackStructEvent(structEvent) | ||
|
||
data := map[string]interface{}{ | ||
"targetUrl": "https://www.snowplow.io", | ||
} | ||
|
||
sdj := sp.InitSelfDescribingJson("iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-1", data) | ||
|
||
sde := sp.SelfDescribingEvent{Event: sdj} | ||
|
||
tracker.TrackSelfDescribingEvent(sde) | ||
|
||
tracker.Emitter.Stop() | ||
tracker.BlockingFlush(5, 10) | ||
|
||
} |