-
Notifications
You must be signed in to change notification settings - Fork 387
/
hubspot.go
352 lines (305 loc) · 10.1 KB
/
hubspot.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package analytics
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/sync2"
)
var mon = monkit.Package()
const (
eventPrefix = "pe44965639"
expiryBufferTime = 5 * time.Minute
// string template for hubspot submission form. %s is a placeholder for the form(ID) being submitted.
hubspotFormTemplate = "https://api.hsforms.com/submissions/v3/integration/submit/44965639/%s"
// this form ID is the minimal account form.
minimalFormID = "ad4cd7b6-1c9c-44b9-b41f-b48611739620"
// The hubspot lifecycle stage of all new accounts (Product Qualified Lead).
lifecycleStage = "170987588"
)
// HubSpotConfig is a configuration struct for Concurrent Sending of Events.
type HubSpotConfig struct {
RefreshToken string `help:"hubspot refresh token" default:""`
TokenAPI string `help:"hubspot token refresh API" default:"https://api.hubapi.com/oauth/v1/token"`
ClientID string `help:"hubspot client ID" default:""`
ClientSecret string `help:"hubspot client secret" default:""`
ChannelSize int `help:"the number of events that can be in the queue before dropping" default:"1000"`
ConcurrentSends int `help:"the number of concurrent api requests that can be made" default:"4"`
DefaultTimeout time.Duration `help:"the default timeout for the hubspot http client" default:"10s"`
}
// HubSpotEvent is a configuration struct for sending API request to HubSpot.
type HubSpotEvent struct {
Data map[string]interface{}
Endpoint string
Method *string
}
// HubSpotEvents is a configuration struct for sending Events data to HubSpot.
type HubSpotEvents struct {
log *zap.Logger
config HubSpotConfig
events chan []HubSpotEvent
refreshToken string
tokenAPI string
satelliteName string
worker sync2.Limiter
httpClient *http.Client
clientID string
clientSecret string
accessTokenData *TokenData
mutex sync.Mutex
}
// TokenData contains data related to the Hubspot access token.
type TokenData struct {
AccessToken string
ExpiresAt time.Time
}
// NewHubSpotEvents for sending user events to HubSpot.
func NewHubSpotEvents(log *zap.Logger, config HubSpotConfig, satelliteName string) *HubSpotEvents {
return &HubSpotEvents{
log: log,
config: config,
events: make(chan []HubSpotEvent, config.ChannelSize),
refreshToken: config.RefreshToken,
tokenAPI: config.TokenAPI,
clientID: config.ClientID,
clientSecret: config.ClientSecret,
satelliteName: satelliteName,
worker: *sync2.NewLimiter(config.ConcurrentSends),
httpClient: &http.Client{
Timeout: config.DefaultTimeout,
},
}
}
// Run for concurrent API requests.
func (q *HubSpotEvents) Run(ctx context.Context) error {
defer q.worker.Wait()
for {
if err := ctx.Err(); err != nil {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
case ev := <-q.events:
q.worker.Go(ctx, func() {
err := q.Handle(ctx, ev)
if err != nil {
q.log.Error("Sending hubspot event", zap.Error(err))
}
})
}
}
}
// EnqueueCreateUserMinimal is for creating user in HubSpot using the minimal form.
func (q *HubSpotEvents) EnqueueCreateUserMinimal(fields TrackCreateUserFields) {
newField := func(name string, value interface{}) map[string]interface{} {
return map[string]interface{}{
"name": name,
"value": value,
}
}
formFields := []map[string]interface{}{
newField("email", fields.Email),
newField("origin_header", fields.OriginHeader),
newField("signup_referrer", fields.Referrer),
newField("account_created", "true"),
newField("signup_partner", fields.UserAgent),
newField("lifecyclestage", lifecycleStage),
}
if fields.SignupCaptcha != nil {
formFields = append(formFields, newField("signup_captcha_score", *fields.SignupCaptcha))
}
properties := map[string]interface{}{
"userid": fields.ID.String(),
"email": fields.Email,
"satellite_selected": q.satelliteName,
}
formURL := fmt.Sprintf(hubspotFormTemplate, minimalFormID)
data := map[string]interface{}{
"fields": formFields,
}
if fields.HubspotUTK != "" {
data["context"] = map[string]interface{}{
"hutk": fields.HubspotUTK,
}
}
createUser := HubSpotEvent{
Endpoint: formURL,
Data: data,
}
sendUserEvent := HubSpotEvent{
Endpoint: "https://api.hubapi.com/events/v3/send",
Data: map[string]interface{}{
"email": fields.Email,
"eventName": eventPrefix + "_" + strings.ToLower(q.satelliteName) + "_" + "account_created",
"properties": properties,
},
}
select {
case q.events <- []HubSpotEvent{createUser, sendUserEvent}:
default:
q.log.Error("create user hubspot event failed, event channel is full")
}
}
// EnqueueUserOnboardingInfo is for sending post-creation information to Hubspot, that the user enters after login during onboarding.
func (q *HubSpotEvents) EnqueueUserOnboardingInfo(fields TrackOnboardingInfoFields) {
fullName := fields.FullName
names := strings.SplitN(fullName, " ", 2)
var firstName string
var lastName string
if len(names) > 1 {
firstName = names[0]
lastName = names[1]
} else {
firstName = fullName
}
properties := map[string]interface{}{
"email": fields.Email,
"firstname": firstName,
"lastname": lastName,
"storage_use": fields.StorageUseCase,
}
if fields.Type == Professional {
properties["have_sales_contact"] = fields.HaveSalesContact
properties["interested_in_partnering"] = fields.InterestedInPartnering
properties["company_size"] = fields.EmployeeCount
properties["company"] = fields.CompanyName
properties["title"] = fields.JobTitle
properties["storage_needs"] = fields.StorageNeeds
properties["functional_area"] = fields.FunctionalArea
}
updateContactEndpoint := fmt.Sprintf("https://api.hubapi.com/crm/v3/objects/contacts/%s?idProperty=email", url.QueryEscape(fields.Email))
method := http.MethodPatch
onboardingInfoEvent := HubSpotEvent{
Endpoint: updateContactEndpoint,
Method: &method,
Data: map[string]interface{}{
"properties": properties,
},
}
select {
case q.events <- []HubSpotEvent{onboardingInfoEvent}:
default:
q.log.Error("update user properties hubspot event failed, event channel is full")
}
}
// handleSingleEvent for handle the single HubSpot API request.
func (q *HubSpotEvents) handleSingleEvent(ctx context.Context, ev HubSpotEvent) (err error) {
payloadBytes, err := json.Marshal(ev.Data)
if err != nil {
return Error.New("json marshal failed: %w", err)
}
method := http.MethodPost
if ev.Method != nil {
method = *ev.Method
}
req, err := http.NewRequestWithContext(ctx, method, ev.Endpoint, bytes.NewReader(payloadBytes))
if err != nil {
return Error.New("new request failed: %w", err)
}
token, err := q.getAccessToken(ctx)
if err != nil {
return Error.New("token request failed: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
resp, err := q.httpClient.Do(req)
if err != nil {
q.log.Error("send request failed", zap.Error(err))
return Error.New("send request failed: %w", err)
}
defer func() {
err = errs.Combine(err, resp.Body.Close())
}()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
type hsError struct {
Message string `json:"message"`
In string `json:"in"`
}
var data struct {
Message string `json:"message"`
Errors []hsError `json:"errors"`
}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return Error.New("decoding response failed: %w", err)
}
return Error.New("sending event failed: %s - %v", data.Message, data.Errors)
}
return err
}
// Handle for handle the HubSpot API requests.
func (q *HubSpotEvents) Handle(ctx context.Context, events []HubSpotEvent) (err error) {
defer mon.Task()(&ctx)(&err)
for _, ev := range events {
err := q.handleSingleEvent(ctx, ev)
if err != nil {
return Error.New("handle event: %w", err)
}
}
return nil
}
// getAccessToken returns an access token for hubspot.
// It fetches a new token if there isn't one already or the old one is about to expire in expiryBufferTime.
// It locks q.mutex to ensure only one goroutine is able to request for a token.
func (q *HubSpotEvents) getAccessToken(ctx context.Context) (token string, err error) {
defer mon.Task()(&ctx)(&err)
q.mutex.Lock()
defer q.mutex.Unlock()
if q.accessTokenData == nil || q.accessTokenData.ExpiresAt.Add(-expiryBufferTime).Before(time.Now()) {
q.accessTokenData, err = q.getAccessTokenFromHubspot(ctx)
if err != nil {
return "", err
}
}
return q.accessTokenData.AccessToken, nil
}
// getAccessTokenFromHubspot gets a new access token from hubspot.
// Expects q.mutex to be locked.
func (q *HubSpotEvents) getAccessTokenFromHubspot(ctx context.Context) (_ *TokenData, err error) {
defer mon.Task()(&ctx)(&err)
values := make(url.Values)
values.Set("grant_type", "refresh_token")
values.Set("client_id", q.clientID)
values.Set("client_secret", q.clientSecret)
values.Set("refresh_token", q.refreshToken)
encoded := values.Encode()
buff := bytes.NewBufferString(encoded)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, q.tokenAPI, buff)
if err != nil {
return nil, Error.New("new request failed: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := q.httpClient.Do(req)
if err != nil {
return nil, Error.New("send request failed: %w", err)
}
defer func() {
err = errs.Combine(err, resp.Body.Close())
}()
if resp.StatusCode != http.StatusOK {
return nil, Error.New("send request failed: %w", err)
}
var tokenData struct {
ExpiresIn int `json:"expires_in"`
AccessToken string `json:"access_token"`
}
err = json.NewDecoder(resp.Body).Decode(&tokenData)
if err != nil {
return nil, Error.New("decode response failed: %w", err)
}
return &TokenData{
AccessToken: tokenData.AccessToken,
ExpiresAt: time.Now().Add(time.Duration(tokenData.ExpiresIn * 1000)),
}, nil
}