@pbvision/cloud-run-service 0.0.30 → 0.0.31

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.30",
3
+ "version": "0.0.31",
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
@@ -21,7 +21,6 @@ export class DatabaseAPIWithAnalytics extends DatabaseAPI {
21
21
  super(fastify, req, reply)
22
22
  this.__analyticsEvents = []
23
23
  this.__analyticsUserProfileUpdates = {} // uid to $set/$set_once to changes
24
- this.__analyticsSent = false
25
24
  }
26
25
 
27
26
  async postCommit (respData) {
@@ -31,7 +30,6 @@ export class DatabaseAPIWithAnalytics extends DatabaseAPI {
31
30
  }
32
31
 
33
32
  logAnalyticsEvent (mixpanelUserId, eventName, inputProperties, deviceId = null) {
34
- assert(this.__analyticsSent === false)
35
33
  this.__analyticsEvents.push({
36
34
  event: eventName,
37
35
  properties: addSenderId(mixpanelUserId, {
@@ -60,14 +58,13 @@ export class DatabaseAPIWithAnalytics extends DatabaseAPI {
60
58
  }
61
59
 
62
60
  async sendAnalyticsEvents () {
63
- // istanbul ignore if
64
- if (this.__analyticsSent) {
65
- return
66
- }
67
- this.__analyticsSent = true
61
+ const events = this.__analyticsEvents
62
+ this.__analyticsEvents = []
63
+ const userProfileUpdates = this.__analyticsUserProfileUpdates
64
+ this.__analyticsUserProfileUpdates = {}
68
65
 
69
66
  const promises = []
70
- if (this.__analyticsEvents.length) {
67
+ if (events.length) {
71
68
  const uaData = {}
72
69
  const parser = new UAParser(this.req.headers['user-agent'])
73
70
  const browser = parser.getBrowser()
@@ -92,7 +89,7 @@ export class DatabaseAPIWithAnalytics extends DatabaseAPI {
92
89
  }
93
90
  }
94
91
 
95
- for (const x of this.__analyticsEvents) {
92
+ for (const x of events) {
96
93
  Object.assign(x.properties, uaData)
97
94
  }
98
95
  // send all the events in one Mixpanel API call
@@ -100,12 +97,12 @@ export class DatabaseAPIWithAnalytics extends DatabaseAPI {
100
97
  method: 'POST',
101
98
  url: 'https://api.mixpanel.com/track',
102
99
  headers: { accept: 'text/plain' },
103
- body: this.__analyticsEvents
100
+ body: events
104
101
  }))
105
102
  }
106
103
  const sent = []
107
- for (const type of Object.keys(this.__analyticsUserProfileUpdates)) {
108
- const updatesByUser = this.__analyticsUserProfileUpdates[type]
104
+ for (const type of Object.keys(userProfileUpdates)) {
105
+ const updatesByUser = userProfileUpdates[type]
109
106
  const body = []
110
107
  for (const uid of Object.keys(updatesByUser)) {
111
108
  const updates = updatesByUser[uid]
@@ -54,12 +54,18 @@ export class TestAnalyticsAPI extends DatabaseAPIWithAnalytics {
54
54
  async computeResponse () {
55
55
  // istanbul ignore next
56
56
  assert(isUnitTesting || isLocalhost)
57
- const { eventCalls, profileUpdates } = this.req.body
57
+ const { eventCalls, profileUpdates, moreEventCalls } = this.req.body
58
58
  for (const x of (eventCalls ?? [])) {
59
59
  this.logAnalyticsEvent(...x)
60
60
  }
61
61
  for (const x of (profileUpdates ?? [])) {
62
62
  this.updateAnalyticsUserProfile(...x)
63
63
  }
64
+ if (moreEventCalls) {
65
+ await this.sendAnalyticsEvents()
66
+ for (const x of moreEventCalls) {
67
+ this.logAnalyticsEvent(...x)
68
+ }
69
+ }
64
70
  }
65
71
  }
@@ -63,6 +63,39 @@ class AnalyticsTest extends AppTest {
63
63
  }).expect(expRespCode)
64
64
  }
65
65
 
66
+ async testSendInBatches () {
67
+ await this.app.post('/analytics')
68
+ .send({
69
+ eventCalls: [
70
+ ['xyz', 'some event', { cool: 1, hi: 'world' }]
71
+ ],
72
+ moreEventCalls: [
73
+ ['xyz', 'another event', { x: 2 }]
74
+ ]
75
+ }).expect(200)
76
+ const calls = this.fetchMock.mock.calls
77
+ expect(calls.length).toBe(2)
78
+ const actualBody = JSON.parse(calls[0][1].body)
79
+ expect(actualBody).toEqual([{
80
+ event: 'some event',
81
+ properties: expect.objectContaining({
82
+ cool: 1,
83
+ hi: 'world',
84
+ $user_id: 'xyz',
85
+ token: mixpanelToken
86
+ })
87
+ }])
88
+ const actualBody2 = JSON.parse(calls[1][1].body)
89
+ expect(actualBody2).toEqual([{
90
+ event: 'another event',
91
+ properties: expect.objectContaining({
92
+ x: 2,
93
+ $user_id: 'xyz',
94
+ token: mixpanelToken
95
+ })
96
+ }])
97
+ }
98
+
66
99
  async testDeviceId () {
67
100
  await this.app.post('/analytics')
68
101
  .send({