Pirsch Docs > API and SDKs > Go SDK

Go SDK

12. Dec, 2020

Go Reference

The Go SDK can be found on GitHub.

Installation

1
go get github.com/pirsch-analytics/pirsch-go-sdk

Create a Client

To use the API, you need to create a client on the settings page first and use the client ID, the secret, and hostname to set up the SDK.

1
2
3
import "github.com/pirsch-analytics/pirsch-go-sdk"

client := pirsch.NewClient("client_id", "client_secret", "example.com", nil)

From here on we can make API calls through the client. It will automatically update the access token using the credentials you provided. The last parameter is optional and can be set to nil.

Send a Page Hit

To monitor your website traffic, you need to send hits to Pirsch. This is done by calling the Hit method from a handler function.

1
2
3
4
5
6
7
8
9
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    go func() {
        if err := client.Hit(r); err != nil {
            log.Println(err)
        }
    }()

    w.Write([]byte("<h1>Hello from Pirsch!</h1>"))
})

Hit takes the http.Request object as an input and sends all relevant data to Pirsch. This is done synchronously, so you might want to do it asynchronously by calling it using the go command, like in the example above. Note that the handler above accepts all requests made by a client and will therefore lead to a lot of different paths being tracked. Usually you would make sure that only the page itself gets monitored by checking the requested path.

1
2
3
if r.URL.Path == "/" {
    // send hit
}

You can send a hit whenever you want. If you have a page with dynamic content for example, you can check if the content was found and send a hit in that case, or otherwise ignore it.