@pbvision/cloud-run-service 0.0.31 → 0.0.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/analytics.js +10 -3
- package/test/unit-test-analytics.js +43 -0
package/package.json
CHANGED
package/src/analytics.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import assert from 'node:assert'
|
|
2
|
-
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import crypto, { randomUUID } from 'node:crypto'
|
|
3
3
|
|
|
4
4
|
import { DatabaseAPI, EXCEPTIONS } from '@pbvision/fastify-firestore-service'
|
|
5
5
|
import UAParser from 'ua-parser-js'
|
|
@@ -29,14 +29,21 @@ export class DatabaseAPIWithAnalytics extends DatabaseAPI {
|
|
|
29
29
|
return super.postCommit(respData)
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
logAnalyticsEvent (mixpanelUserId, eventName, inputProperties, deviceId = null) {
|
|
32
|
+
logAnalyticsEvent (mixpanelUserId, eventName, inputProperties = {}, deviceId = null, insertId = null) {
|
|
33
|
+
if (insertId) {
|
|
34
|
+
// insert id must be <= 36 chars & only have alphanumeric & hyphen chars
|
|
35
|
+
insertId = crypto.createHash('md5').update(insertId).digest('hex')
|
|
36
|
+
} else {
|
|
37
|
+
insertId = randomUUID()
|
|
38
|
+
}
|
|
39
|
+
|
|
33
40
|
this.__analyticsEvents.push({
|
|
34
41
|
event: eventName,
|
|
35
42
|
properties: addSenderId(mixpanelUserId, {
|
|
36
43
|
...inputProperties,
|
|
37
44
|
token: mixpanelToken,
|
|
38
45
|
time: new Date().getTime(),
|
|
39
|
-
$insert_id:
|
|
46
|
+
$insert_id: insertId,
|
|
40
47
|
ip: this.req.ip
|
|
41
48
|
}, deviceId)
|
|
42
49
|
})
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import crypto from 'node:crypto'
|
|
2
|
+
|
|
1
3
|
import { mixpanelToken } from '../src/analytics.js'
|
|
2
4
|
|
|
3
5
|
import { AppTest, runTests } from './base-test.js'
|
|
@@ -139,6 +141,47 @@ class AnalyticsTest extends AppTest {
|
|
|
139
141
|
}])
|
|
140
142
|
}
|
|
141
143
|
|
|
144
|
+
async testCustomInsertionId () {
|
|
145
|
+
await this.app.post('/analytics')
|
|
146
|
+
.send({
|
|
147
|
+
eventCalls: [
|
|
148
|
+
['some uid', 'some event', { cool: 1, hi: 'world' }, null, 'xx']
|
|
149
|
+
]
|
|
150
|
+
}).expect(200)
|
|
151
|
+
const calls = this.fetchMock.mock.calls
|
|
152
|
+
expect(calls.length).toBe(1)
|
|
153
|
+
const actualBody = JSON.parse(calls[0][1].body)
|
|
154
|
+
expect(actualBody).toEqual([{
|
|
155
|
+
event: 'some event',
|
|
156
|
+
properties: expect.objectContaining({
|
|
157
|
+
cool: 1,
|
|
158
|
+
hi: 'world',
|
|
159
|
+
$user_id: 'some uid',
|
|
160
|
+
token: mixpanelToken,
|
|
161
|
+
$insert_id: crypto.createHash('md5').update('xx').digest('hex')
|
|
162
|
+
})
|
|
163
|
+
}])
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async testOmittingEventProperties () {
|
|
167
|
+
await this.app.post('/analytics')
|
|
168
|
+
.send({
|
|
169
|
+
eventCalls: [
|
|
170
|
+
['some uid', 'some event']
|
|
171
|
+
]
|
|
172
|
+
}).expect(200)
|
|
173
|
+
const calls = this.fetchMock.mock.calls
|
|
174
|
+
expect(calls.length).toBe(1)
|
|
175
|
+
const actualBody = JSON.parse(calls[0][1].body)
|
|
176
|
+
expect(actualBody).toEqual([{
|
|
177
|
+
event: 'some event',
|
|
178
|
+
properties: expect.objectContaining({
|
|
179
|
+
$user_id: 'some uid',
|
|
180
|
+
token: mixpanelToken
|
|
181
|
+
})
|
|
182
|
+
}])
|
|
183
|
+
}
|
|
184
|
+
|
|
142
185
|
async testUserAgent () {
|
|
143
186
|
await this.sendBasicEvent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36')
|
|
144
187
|
const calls = this.fetchMock.mock.calls
|