@pbvision/cloud-run-service 0.0.31 → 0.0.33

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pbvision/cloud-run-service",
3
- "version": "0.0.31",
3
+ "version": "0.0.33",
4
4
  "description": "fastify-firestore-service Web Framework on Cloud Run",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
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: randomUUID(),
46
+ $insert_id: insertId,
40
47
  ip: this.req.ip
41
48
  }, deviceId)
42
49
  })
@@ -140,12 +147,19 @@ export class DatabaseAPIWithAnalytics extends DatabaseAPI {
140
147
 
141
148
  function addSenderId (mixpanelUserId, properties, deviceId) {
142
149
  if (mixpanelUserId.startsWith('$device:')) {
143
- properties.$device_id = mixpanelUserId
150
+ properties.distinct_id = mixpanelUserId
151
+ properties.$device_id = mixpanelUserId.substring(8)
144
152
  } else {
145
- properties.$user_id = mixpanelUserId
153
+ properties.distinct_id = properties.$user_id = mixpanelUserId
146
154
  if (deviceId) {
147
- properties.$device_id = deviceId
155
+ // istanbul ignore else
156
+ if (deviceId.startsWith('$device')) {
157
+ properties.$device_id = deviceId.substring(8)
158
+ } else {
159
+ properties.$device_id = deviceId
160
+ }
148
161
  }
149
162
  }
163
+ properties.distinct_id = mixpanelUserId
150
164
  return properties
151
165
  }
@@ -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'
@@ -111,7 +113,8 @@ class AnalyticsTest extends AppTest {
111
113
  properties: expect.objectContaining({
112
114
  cool: 1,
113
115
  hi: 'world',
114
- $device_id: '$device:xyz',
116
+ distinct_id: '$device:xyz',
117
+ $device_id: 'xyz',
115
118
  token: mixpanelToken
116
119
  })
117
120
  }])
@@ -133,7 +136,49 @@ class AnalyticsTest extends AppTest {
133
136
  cool: 1,
134
137
  hi: 'world',
135
138
  $user_id: 'some uid',
136
- $device_id: '$device:xyz',
139
+ distinct_id: 'some uid',
140
+ $device_id: 'xyz',
141
+ token: mixpanelToken
142
+ })
143
+ }])
144
+ }
145
+
146
+ async testCustomInsertionId () {
147
+ await this.app.post('/analytics')
148
+ .send({
149
+ eventCalls: [
150
+ ['some uid', 'some event', { cool: 1, hi: 'world' }, null, 'xx']
151
+ ]
152
+ }).expect(200)
153
+ const calls = this.fetchMock.mock.calls
154
+ expect(calls.length).toBe(1)
155
+ const actualBody = JSON.parse(calls[0][1].body)
156
+ expect(actualBody).toEqual([{
157
+ event: 'some event',
158
+ properties: expect.objectContaining({
159
+ cool: 1,
160
+ hi: 'world',
161
+ $user_id: 'some uid',
162
+ token: mixpanelToken,
163
+ $insert_id: crypto.createHash('md5').update('xx').digest('hex')
164
+ })
165
+ }])
166
+ }
167
+
168
+ async testOmittingEventProperties () {
169
+ await this.app.post('/analytics')
170
+ .send({
171
+ eventCalls: [
172
+ ['some uid', 'some event']
173
+ ]
174
+ }).expect(200)
175
+ const calls = this.fetchMock.mock.calls
176
+ expect(calls.length).toBe(1)
177
+ const actualBody = JSON.parse(calls[0][1].body)
178
+ expect(actualBody).toEqual([{
179
+ event: 'some event',
180
+ properties: expect.objectContaining({
181
+ $user_id: 'some uid',
137
182
  token: mixpanelToken
138
183
  })
139
184
  }])