feat: use unique machine ID for reporting GA

This commit is contained in:
debugtalk
2021-11-26 16:09:34 +08:00
parent 3a62c7e6a8
commit ae4068edbc
4 changed files with 16 additions and 6 deletions

1
go.mod
View File

@@ -3,6 +3,7 @@ module github.com/httprunner/hrp
go 1.16
require (
github.com/denisbrodbeck/machineid v1.0.1
github.com/getsentry/sentry-go v0.11.0
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/google/uuid v1.3.0

2
go.sum
View File

@@ -78,6 +78,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisbrodbeck/machineid v1.0.1 h1:geKr9qtkB876mXguW2X6TU4ZynleN6ezuMSRhl4D7AQ=
github.com/denisbrodbeck/machineid v1.0.1/go.mod h1:dJUwb7PTidGDeYyUBmXZ2GphQBbjJCrnectwCyxcUSI=
github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=

View File

@@ -6,8 +6,6 @@ import (
"net/url"
"reflect"
"time"
"github.com/google/uuid"
)
const (
@@ -23,11 +21,10 @@ type GAClient struct {
}
// NewGAClient creates a new GAClient object with the trackingID and clientID.
func NewGAClient(trackingID string) *GAClient {
nodeUUID, _ := uuid.NewUUID()
func NewGAClient(trackingID, clientID string) *GAClient {
return &GAClient{
TrackingID: trackingID,
ClientID: nodeUUID.String(),
ClientID: clientID,
Version: "1", // constant v1
httpClient: &http.Client{
Timeout: 5 * time.Second,

View File

@@ -1,5 +1,10 @@
package ga
import (
"github.com/denisbrodbeck/machineid"
"github.com/google/uuid"
)
const (
trackingID = "UA-114587036-1" // Tracking ID for Google Analytics
)
@@ -7,7 +12,12 @@ const (
var gaClient *GAClient
func init() {
gaClient = NewGAClient(trackingID)
clientID, err := machineid.ProtectedID("hrp")
if err != nil {
nodeUUID, _ := uuid.NewUUID()
clientID = nodeUUID.String()
}
gaClient = NewGAClient(trackingID, clientID)
}
func SendEvent(e IEvent) error {