forked from GetStream/stream-go2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics_test.go
101 lines (91 loc) · 4.14 KB
/
analytics_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package stream_test
import (
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
stream "github.com/GetStream/stream-go2"
)
func TestAnalyticsTrackEngagement(t *testing.T) {
client, requester := newClient(t)
analytics := client.Analytics()
event1 := stream.EngagementEvent{}.
WithLabel("click").
WithForeignID("abcdef").
WithUserData(stream.NewUserData().Int(12345).Alias("John Doe")).
WithFeedID("timeline:123").
WithLocation("hawaii").
WithPosition(42).
WithBoost(10)
event2 := stream.EngagementEvent{}.
WithLabel("share").
WithForeignID("aabbccdd").
WithUserData(stream.NewUserData().String("bob")).
WithFeedID("timeline:123").
WithFeatures(
stream.NewEventFeature("color", "red"),
stream.NewEventFeature("size", "xxl"),
)
err := analytics.TrackEngagement(event1, event2)
require.NoError(t, err)
expectedURL := "https://analytics.stream-io-api.com/analytics/v1.0/engagement/?api_key=key"
expectedBody := `{"content_list":[{"boost":10,"content":"abcdef","feed_id":"timeline:123","label":"click","location":"hawaii","position":42,"user_data":{"alias":"John Doe","id":12345}},{"content":"aabbccdd","features":[{"group":"color","value":"red"},{"group":"size","value":"xxl"}],"feed_id":"timeline:123","label":"share","user_data":"bob"}]}`
testRequest(t, requester.req, http.MethodPost, expectedURL, expectedBody)
}
func TestAnalyticsTrackImpression(t *testing.T) {
client, requester := newClient(t)
analytics := client.Analytics()
imp := stream.ImpressionEventsData{}.
WithForeignIDs("a", "b", "c", "d").
WithUserData(stream.NewUserData().Int(123)).
WithFeedID("timeline:123").
WithFeatures(
stream.NewEventFeature("color", "red"),
stream.NewEventFeature("size", "xxl"),
).
WithLocation("hawaii").
WithPosition(42)
err := analytics.TrackImpression(imp)
require.NoError(t, err)
expectedURL := "https://analytics.stream-io-api.com/analytics/v1.0/impression/?api_key=key"
expectedBody := `{"content_list":["a","b","c","d"],"features":[{"group":"color","value":"red"},{"group":"size","value":"xxl"}],"feed_id":"timeline:123","location":"hawaii","position":42,"user_data":123}`
testRequest(t, requester.req, http.MethodPost, expectedURL, expectedBody)
}
func TestAnalyticsRedirectAndTrack(t *testing.T) {
client, _ := newClient(t)
analytics := client.Analytics()
event1 := stream.EngagementEvent{}.
WithLabel("click").
WithForeignID("abcdef").
WithUserData(stream.NewUserData().Int(12345).Alias("John Doe")).
WithFeedID("timeline:123").
WithLocation("hawaii").
WithPosition(42).
WithBoost(10)
event2 := stream.EngagementEvent{}.
WithLabel("share").
WithForeignID("aabbccdd").
WithUserData(stream.NewUserData().String("bob")).
WithFeedID("timeline:123").
WithFeatures(
stream.NewEventFeature("color", "red"),
stream.NewEventFeature("size", "xxl"),
)
imp := stream.ImpressionEventsData{}.
WithForeignIDs("a", "b", "c", "d").
WithUserData(stream.NewUserData().Int(123)).
WithFeedID("timeline:123").
WithFeatures(
stream.NewEventFeature("color", "red"),
stream.NewEventFeature("size", "xxl"),
).
WithLocation("hawaii").
WithPosition(42)
link, err := analytics.RedirectAndTrack("foo.bar.baz", event1, event2, imp)
require.NoError(t, err)
query, err := url.ParseQuery(`api_key=key&authorization=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY3Rpb24iOiIqIiwicmVzb3VyY2UiOiJyZWRpcmVjdF9hbmRfdHJhY2siLCJ1c2VyX2lkIjoiKiJ9.A1vy9pFwLw5s6qn0chkhRcoy974A16a0lE-x5Vtxb-o&events=[{"boost":10,"content":"abcdef","feed_id":"timeline:123","label":"click","location":"hawaii","position":42,"user_data":{"alias":"John Doe","id":12345}},{"content":"aabbccdd","features":[{"group":"color","value":"red"},{"group":"size","value":"xxl"}],"feed_id":"timeline:123","label":"share","user_data":"bob"},{"content_list":["a","b","c","d"],"features":[{"group":"color","value":"red"},{"group":"size","value":"xxl"}],"feed_id":"timeline:123","location":"hawaii","position":42,"user_data":123}]&stream-auth-type=jwt&url=foo.bar.baz`)
require.NoError(t, err)
expected := "https://analytics.stream-io-api.com/analytics/v1.0/redirect/?" + query.Encode()
assert.Equal(t, expected, link)
}