This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
analytics.go
158 lines (130 loc) · 4.59 KB
/
analytics.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package busetabot
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)
// Application details
const (
ApplicationName = "Bus Eta Bot"
ApplicationID = "github.com/yi-jiayu/bus-eta-bot"
ApplicationVersion = Version
)
// Measurement Protocol constants
const (
MeasurementProtocolVersion = "1"
MeasurementProtocolEndpoint = "https://www.google-analytics.com/collect"
MeasurementProtocolValidationEndpoint = "https://www.google-analytics.com/debug/collect"
)
// Event categories
const (
CategoryCommand = "command"
CategoryMessage = "message"
CategoryInlineQuery = "inline_query"
CategoryChosenInlineResult = "chosen_inline_result"
CategoryCallback = "callback_query"
CategoryError = "error"
)
// Event actions
const (
ActionEtaCommandWithArgs = "eta_command_with_args"
ActionEtaCommandWithoutArgs = "eta_command_without_args"
ActionStartCommand = "start_command"
ActionAboutCommand = "about_command"
ActionVersionCommand = "version_command"
ActionHelpCommand = "help_command"
ActionPrivacyCommand = "privacy_command"
ActionFeedbackCommand = "feedback_command"
ActionEtaTextMessage = "eta_text_message"
ActionContinuedTextMessage = "continued_text_message"
ActionIgnoredTextMessage = "ignored_text_message"
ActionLocationMessage = "location_message"
ActionNewInlineQuery = "new_inline_query"
ActionNewNearbyInlineQuery = "new_nearby_inline_query"
ActionOffsetInlineQuery = "offset_inline_query"
ActionChosenInlineResult = "chosen_inline_result"
ActionChosenNearbyInlineResult = "chosen_nearby_inline_result"
ActionRefreshCallback = "refresh_callback"
ActionResendCallback = "resend_callback"
ActionEtaCallback = "eta_callback"
ActionEtaDemoCallback = "eta_demo_callback"
ActionEtaFromLocationCallback = "eta_from_location_callback"
ActionAddFavouriteCalback = "add_favourite_callback"
ActionRemoveFavouriteCalback = "remove_favourite_callback"
ActionCommandError = "command_error"
ActionMessageError = "message_error"
ActionInlineQueryError = "inline_query_error"
ActionCallbackError = "callback_error"
)
// Event labels
const (
LabelInlineMessage = "inline_message"
)
var errStatusCode = errors.New("status code error")
// MeasurementProtocolClient contains the endpoint, tracking id and http client to use to send hits to the Measurement Protocol
type MeasurementProtocolClient struct {
Endpoint string
TrackingID string
Client *http.Client
}
// ValidationServerResponse is a response from the Measurement Protocol Validation Server
type ValidationServerResponse struct {
Results []HitParsingResult `json:"hitParsingResult"`
}
// HitParsingResult contains whether the a hit is valid and additional information fom the validation server
type HitParsingResult struct {
Valid bool `json:"valid"`
Hit string `json:"hit"`
ParserMessages []ParserMessage `json:"parserMessage"`
}
// ParserMessage is a message from the validation server parser.
type ParserMessage struct {
Type string `json:"messageType"`
Description string `json:"description"`
Parameter string `json:"parameter"`
}
// NewMeasurementProtocolClientWithClient constructs a MeasurementProtocolClient with the provided tid and http.Client
func NewMeasurementProtocolClientWithClient(tid string, client *http.Client) MeasurementProtocolClient {
return MeasurementProtocolClient{
Endpoint: MeasurementProtocolEndpoint,
TrackingID: tid,
Client: client,
}
}
// LogEvent logs an event to the Measurement Protocol.
func (c MeasurementProtocolClient) LogEvent(userID int, languageCode, category, action, label string) (*http.Response, error) {
values := url.Values{}
// protocol version
values.Set("v", MeasurementProtocolVersion)
// tid
values.Set("tid", c.TrackingID)
// user ID and language code
values.Set("uid", fmt.Sprintf("%d", userID))
if languageCode != "" {
values.Set("ul", languageCode)
}
// hit type
values.Set("t", "event")
// application details
values.Set("an", ApplicationName)
values.Set("aid", ApplicationID)
values.Set("av", ApplicationVersion)
// event details
values.Set("ec", category)
values.Set("ea", action)
values.Set("el", label)
req, err := http.NewRequest("POST", c.Endpoint, strings.NewReader(values.Encode()))
if err != nil {
return nil, err
}
resp, err := c.Client.Do(req)
if err != nil {
return resp, err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return resp, errStatusCode
}
return resp, nil
}