-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor event creation and move clients
- Loading branch information
1 parent
5cb6719
commit a4fa86a
Showing
58 changed files
with
468 additions
and
229 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
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
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,35 @@ | ||
package client_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/http/httputil" | ||
"testing" | ||
|
||
"github.com/foomo/sesamy-go/pkg/client" | ||
"github.com/foomo/sesamy-go/pkg/encoding/gtag" | ||
"github.com/foomo/sesamy-go/pkg/sesamy" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/zaptest" | ||
) | ||
|
||
func TestNewGtag(t *testing.T) { | ||
l := zaptest.NewLogger(t) | ||
|
||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
t.Helper() | ||
out, err := httputil.DumpRequest(r, true) | ||
require.NoError(t, err) | ||
t.Log(string(out)) | ||
})) | ||
|
||
c := client.NewGTag(l, s.URL, "GA-XXXXXX") | ||
incomingReq, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, "/foo/bar", nil) | ||
require.NoError(t, err) | ||
|
||
err = c.Send(incomingReq, >ag.Payload{ | ||
EventName: gtag.Set(sesamy.EventName("page_view")), | ||
}) | ||
require.NoError(t, err) | ||
} |
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,75 @@ | ||
package client | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/foomo/sesamy-go/pkg/encoding/gtag" | ||
"github.com/foomo/sesamy-go/pkg/session" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func GTagMiddlewareRichsstsse(next GTagHandler) GTagHandler { | ||
v := "" | ||
return func(r *http.Request, payload *gtag.Payload) error { | ||
payload.Richsstsse = &v | ||
return next(r, payload) | ||
} | ||
} | ||
|
||
func GTagMiddlewareTrackingID(v string) GTagMiddleware { | ||
return func(next GTagHandler) GTagHandler { | ||
return func(r *http.Request, payload *gtag.Payload) error { | ||
payload.TrackingID = &v | ||
return next(r, payload) | ||
} | ||
} | ||
} | ||
|
||
func GTagMiddlewarProtocolVersion(v string) GTagMiddleware { | ||
return func(next GTagHandler) GTagHandler { | ||
return func(r *http.Request, payload *gtag.Payload) error { | ||
payload.ProtocolVersion = &v | ||
return next(r, payload) | ||
} | ||
} | ||
} | ||
|
||
func GTagMiddlewarIsDebug(next GTagHandler) GTagHandler { | ||
v := "1" | ||
return func(r *http.Request, payload *gtag.Payload) error { | ||
if session.IsGTMDebug(r) { | ||
payload.IsDebug = &v | ||
} | ||
return next(r, payload) | ||
} | ||
} | ||
|
||
func GTagMiddlewarClientID(next GTagHandler) GTagHandler { | ||
return func(r *http.Request, payload *gtag.Payload) error { | ||
value, err := session.ParseGAClientID(r) | ||
if err != nil && !errors.Is(err, http.ErrNoCookie) { | ||
return errors.Wrap(err, "failed to parse client cookie") | ||
} | ||
if value != "" { | ||
payload.ClientID = &value | ||
} | ||
return next(r, payload) | ||
} | ||
} | ||
|
||
func GTagMiddlewarSessionID(trackingID string) GTagMiddleware { | ||
trackingID = strings.Split(trackingID, "-")[1] | ||
return func(next GTagHandler) GTagHandler { | ||
return func(r *http.Request, payload *gtag.Payload) error { | ||
value, err := session.ParseGASessionID(r, trackingID) | ||
if err != nil && !errors.Is(err, http.ErrNoCookie) { | ||
return errors.Wrap(err, "failed to parse session cookie") | ||
} | ||
if value != "" { | ||
payload.SessionID = &value | ||
} | ||
return next(r, payload) | ||
} | ||
} | ||
} |
Oops, something went wrong.