forked from PostHog/hubspot-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
320 lines (281 loc) · 11.7 KB
/
index.js
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
const NEXT_CONTACT_BATCH_KEY = 'next_hubspot_contacts_url'
const SYNC_LAST_COMPLETED_DATE_KEY = 'last_job_complete_day'
export const jobs = {
'Clear storage': async (_, { storage }) => {
await storage.del(NEXT_CONTACT_BATCH_KEY)
await storage.del(SYNC_LAST_COMPLETED_DATE_KEY)
}
}
export async function setupPlugin({ config, global }) {
global.sync_mode = config.sync_mode
global.hubspotAuth = `hapikey=${config.hubspotApiKey}`
global.posthogUrl = config.postHogUrl
global.apiToken = config.postHogApiToken
global.projectToken = config.postHogProjectToken
global.syncScoresIntoPosthog = global.posthogUrl && global.apiToken && global.projectToken
const authResponse = await fetchWithRetry(
`https://api.hubapi.com/crm/v3/objects/contacts?limit=1&paginateAssociations=false&archived=false&${global.hubspotAuth}`
)
if (!statusOk(authResponse)) {
throw new Error('Unable to connect to Hubspot. Please make sure your API key is correct.')
}
}
async function updateHubspotScore(email, hubspotScore, global) {
let updated = false
const _userRes = await fetch(`${global.posthogUrl}/api/person/?token=${global.projectToken}&email=${email}`, {
method: 'GET',
headers: { Authorization: `Bearer ${global.apiToken}` },
})
const userResponse = await _userRes.json()
if (userResponse['results'] && userResponse['results'].length > 0) {
for (const loadedUser of userResponse['results']) {
const userId = loadedUser['id']
const distinct_id = loadedUser['distinct_id'][0]
const score = parseInt(hubspotScore, 10)
posthog.identify(distinct_id, {hubspot_score: score})
posthog.capture('hubspot score updated', {hubspot_score: score})
if (userId) {
const _updateRes = await fetch(
`${global.posthogUrl}/api/person/${userId}/?token=${global.projectToken}`,
{
method: 'PATCH',
headers: {
Authorization: `Bearer ${global.apiToken}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
properties: {
hubspot_score: parseInt(hubspotScore, 10)
},
}),
}
)
updated = true
}
}
}
return updated
}
async function getHubspotContacts(global, storage) {
const properties = ['email', 'hubspotscore']
let requestUrl = await storage.get(NEXT_CONTACT_BATCH_KEY)
if (!requestUrl) {
const lastFinishDate = await storage.get(SYNC_LAST_COMPLETED_DATE_KEY)
const dateObj = new Date()
const todayStr = `${dateObj.getUTCFullYear()}-${dateObj.getUTCMonth()}-${dateObj.getUTCDate()}`
if (todayStr === lastFinishDate) & (global.sync_mode === "production") {
return []
}
console.log('Starting score sync job...')
posthog.capture('hubspot score sync started')
}
// start fresh - begin processing all contacts
requestUrl = `https://api.hubapi.com/crm/v3/objects/contacts?limit=100&paginateAssociations=false&archived=false&${
global.hubspotAuth
}&properties=${properties.join(',')}`
}
const loadedContacts = []
const authResponse = await fetchWithRetry(requestUrl)
const res = await authResponse.json()
if (!statusOk(authResponse) || res.status === 'error') {
const errorMessage = res.message ?? ''
console.error(
`Unable to get contacts from Hubspot. Status Code: ${authResponse.status}. Error message: ${errorMessage}`
)
}
if (res && res['results']) {
res['results'].forEach((hubspotContact) => {
const props = hubspotContact['properties']
loadedContacts.push({ email: props['email'], score: props['hubspotscore'] })
})
}
let nextContactBatch
res['paging'] && res['paging']['next']
? (nextContactBatch = res['paging']['next']['link'] + `&${global.hubspotAuth}`)
: null
await storage.set(NEXT_CONTACT_BATCH_KEY, nextContactBatch)
console.log(`Loaded ${loadedContacts.length} Contacts from Hubspot`)
return loadedContacts
export async function runEveryMinute({ config, global, storage }) {
if (!global.syncScoresIntoPosthog) {
console.log('Not syncing Hubspot Scores into PostHog - config not set.')
}
const loadedContacts = await getHubspotContacts(global, storage)
let skipped = 0
let num_updated = 0
let num_processed = 0
let num_errors = 0
for (const hubspotContact of loadedContacts) {
console.log(`Processed...${num_processed} Person updates`)
const email = hubspotContact['email']
const score = hubspotContact['score']
try {
const updated = await updateHubspotScore(email, score, global)
if (updated) {
num_updated += 1
console.log(`Updated Person ${email} with score ${score}`)
} else {
skipped += 1
}
} catch (error) {
console.log(`Error updating Hubspot score for ${email} - Skipping`)
num_errors += 1
}
num_processed += 1
}
console.log(
`Successfully updated Hubspot scores for ${num_updated} records, skipped ${skipped} records, processed ${loadedContacts.length} Hubspot Contacts, errors: ${num_errors} `
)
const nextContactBatch = await storage.get(NEXT_CONTACT_BATCH_KEY)
if (!nextContactBatch) {
posthog.capture('hubspot contact sync all contacts completed', { num_updated: num_updated })
const dateObj = new Date()
await storage.set(
SYNC_LAST_COMPLETED_DATE_KEY,
`${dateObj.getUTCFullYear()}-${dateObj.getUTCMonth()}-${dateObj.getUTCDate()}`
)
} else {
posthog.capture('hubspot contact sync batch completed', { num_updated: num_updated })
}
}
export async function onEvent(event, { config, global }) {
const triggeringEvents = (config.triggeringEvents || '').split(',')
if (triggeringEvents.indexOf(event.event) >= 0) {
const email = getEmailFromEvent(event)
if (email) {
const emailDomainsToIgnore = (config.ignoredEmails || '').split(',')
if (emailDomainsToIgnore.indexOf(email.split('@')[1]) >= 0) {
return
}
await createHubspotContact(
email,
{
...(event['$set'] ?? {}),
...(event['properties'] ?? {}),
},
global.hubspotAuth,
config.additionalPropertyMappings,
event['timestamp']
)
}
}
}
async function createHubspotContact(email, properties, authQs, additionalPropertyMappings, eventSendTime) {
let hubspotFilteredProps = {}
for (const [key, val] of Object.entries(properties)) {
if (hubspotPropsMap[key]) {
hubspotFilteredProps[hubspotPropsMap[key]] = val
}
}
if (additionalPropertyMappings) {
for (let mapping of additionalPropertyMappings.split(',')) {
const [postHogProperty, hubSpotProperty] = mapping.split(':')
if (postHogProperty && hubSpotProperty) {
// special case to convert an event's timestamp to the format Hubspot uses them
if (postHogProperty === 'sent_at' || postHogProperty === 'created_at') {
const d = new Date(eventSendTime)
d.setUTCHours(0, 0, 0, 0)
hubspotFilteredProps[hubSpotProperty] = d.getTime()
} else if (postHogProperty in properties) {
hubspotFilteredProps[hubSpotProperty] = properties[postHogProperty]
}
}
}
}
const addContactResponse = await fetchWithRetry(
`https://api.hubapi.com/crm/v3/objects/contacts?${authQs}`,
{
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ properties: { email: email, ...hubspotFilteredProps } }),
},
'POST'
)
const addContactResponseJson = await addContactResponse.json()
if (!statusOk(addContactResponse) || addContactResponseJson.status === 'error') {
const errorMessage = addContactResponseJson.message ?? ''
console.log(
`Unable to add contact ${email} to Hubspot. Status Code: ${addContactResponse.status}. Error message: ${errorMessage}`
)
if (addContactResponse.status === 409) {
const existingIdRegex = /Existing ID: ([0-9]+)/
const existingId = addContactResponseJson.message.match(existingIdRegex)
console.log(`Attempting to update contact ${email} instead...`)
const updateContactResponse = await fetchWithRetry(
`https://api.hubapi.com/crm/v3/objects/contacts/${existingId[1]}?${authQs}`,
{
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ properties: { email: email, ...hubspotFilteredProps } }),
},
'PATCH'
)
const updateResponseJson = await updateContactResponse.json()
if (!statusOk(updateContactResponse)) {
const errorMessage = updateResponseJson.message ?? ''
console.log(
`Unable to update contact ${email} to Hubspot. Status Code: ${updateContactResponse.status}. Error message: ${errorMessage}`
)
} else {
console.log(`Successfully updated Hubspot Contact for ${email}`)
}
}
} else {
console.log(`Created Hubspot Contact for ${email}`)
}
}
async function fetchWithRetry(url, options = {}, method = 'GET', isRetry = false) {
try {
const res = await fetch(url, { method: method, ...options })
return res
} catch {
if (isRetry) {
throw new Error(`${method} request to ${url} failed.`)
}
const res = await fetchWithRetry(url, options, (method = method), (isRetry = true))
return res
}
}
function statusOk(res) {
return String(res.status)[0] === '2'
}
function isEmail(email) {
const re =
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return re.test(String(email).toLowerCase())
}
function getEmailFromEvent(event) {
if (isEmail(event.distinct_id)) {
return event.distinct_id
} else if (event['$set'] && Object.keys(event['$set']).includes('email')) {
if (isEmail(event['$set']['email'])) {
return event['$set']['email']
}
} else if (event['properties'] && Object.keys(event['properties']).includes('email')) {
if (isEmail(event['properties']['email'])) {
return event['properties']['email']
}
}
return null
}
const hubspotPropsMap = {
companyName: 'company',
company_name: 'company',
company: 'company',
lastName: 'lastname',
last_name: 'lastname',
lastname: 'lastname',
firstName: 'firstname',
first_name: 'firstname',
firstname: 'firstname',
phone_number: 'phone',
phoneNumber: 'phone',
phone: 'phone',
website: 'website',
domain: 'website',
company_website: 'website',
companyWebsite: 'website',
}