-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analytics): refactoring middleware
- Loading branch information
1 parent
6f820ec
commit 90171ab
Showing
5 changed files
with
294 additions
and
241 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
packages/blockchain-wallet-v4-frontend/src/middleware/analyticsMiddleware/analytics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { AnalyticsPayload } from 'middleware/analyticsMiddleware/types' | ||
import { AnalyticsKey } from 'middleware/analyticsMiddleware/types' | ||
import { generateUniqueUserId } from 'middleware/analyticsMiddleware/utils' | ||
import { v4 } from 'uuid' | ||
|
||
import queuevent from 'utils/queuevent' | ||
|
||
const analytics = queuevent<AnalyticsKey, AnalyticsPayload>({ | ||
queueCallback: async (rawEvents) => { | ||
const res = await fetch('/wallet-options-v4.json') | ||
const options = await res.json() | ||
|
||
const analyticsURL = `${options.domains.api}/events/publish` | ||
|
||
const id = rawEvents.find((event) => event.payload.id)?.payload.id | ||
|
||
const randomId = v4() | ||
|
||
const nabuId = rawEvents.find((event) => event.payload.nabuId)?.payload.nabuId || null | ||
|
||
const context: { traits: { nabu_id: string | null } } = { | ||
traits: { | ||
nabu_id: nabuId | ||
} | ||
} | ||
|
||
const events = rawEvents.map((event) => { | ||
const name = event.key | ||
|
||
const { id, nabuId, originalTimestamp, type, ...properties } = event.payload | ||
|
||
return { | ||
id: id ? generateUniqueUserId(id) : generateUniqueUserId(randomId), | ||
nabuId, | ||
name, | ||
originalTimestamp, | ||
properties, | ||
type | ||
} | ||
}) | ||
|
||
await fetch(analyticsURL, { | ||
body: JSON.stringify({ | ||
context, | ||
events, | ||
id: id ? generateUniqueUserId(id) : generateUniqueUserId(randomId) | ||
}), | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
method: 'POST' | ||
}) | ||
}, | ||
queueName: 'analytics' | ||
}) | ||
|
||
export default analytics |
Oops, something went wrong.