Skip to content

Commit

Permalink
chore(fe2): datadog user identification setup
Browse files Browse the repository at this point in the history
  • Loading branch information
fabis94 committed Mar 18, 2024
1 parent 7d42ccd commit 15b7bf3
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
13 changes: 13 additions & 0 deletions packages/frontend-2/lib/auth/composables/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ export const useOnAuthStateChange = () => {
}
}

export const useGetInitialAuthState = () => {
const waitForUser = useWaitForActiveUser()
const resolveDistinctId = useResolveUserDistinctId()

return async () => {
const user = await waitForUser()
return {
user: user?.data?.activeUser,
distinctId: resolveDistinctId(user?.data?.activeUser)
}
}
}

/**
* Composable that builds a function for resetting the active auth state.
* This means resetting mixpanel identification, wiping apollo `me` cache etc.
Expand Down
1 change: 1 addition & 0 deletions packages/frontend-2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@apollo/client": "^3.9.6",
"@artmizu/nuxt-prometheus": "^2.2.1",
"@datadog/browser-rum": "^5.11.0",
"@headlessui/vue": "^1.7.13",
"@heroicons/vue": "^2.0.12",
"@speckle/shared": "workspace:^",
Expand Down
39 changes: 36 additions & 3 deletions packages/frontend-2/plugins/002-rum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { trimStart } from 'lodash-es'
import { useOnAuthStateChange } from '~/lib/auth/composables/auth'
import {
useGetInitialAuthState,
useOnAuthStateChange
} from '~/lib/auth/composables/auth'
import { useCreateErrorLoggingTransport } from '~/lib/core/composables/error'
import type { Plugin } from 'nuxt/dist/app/nuxt'

Expand Down Expand Up @@ -71,12 +74,31 @@ async function initRumClient(app: PluginNuxtApp) {
// }
})
}

// Datadog
const datadog = window.DD_RUM
if (keys.datadog && datadog) {
await onAuthStateChange(
(user, { resolveDistinctId }) => {
const distinctId = resolveDistinctId(user)
if (distinctId && user) {
datadog.setUser({
id: distinctId
})
} else {
datadog.clearUser()
}
},
{ immediate: true }
)
}
}

async function initRumServer(app: PluginNuxtApp) {
const registerErrorTransport = useCreateErrorLoggingTransport()
const { keys, baseUrl, speckleServerVersion, debug, debugCoreWebVitals } =
resolveInitParams(app)
const initUser = useGetInitialAuthState()

// CWV
if (debugCoreWebVitals) {
Expand Down Expand Up @@ -167,17 +189,24 @@ async function initRumServer(app: PluginNuxtApp) {
datadogEnv
} = keys.datadog

const { distinctId } = await initUser()

app.hook('app:rendered', (context) => {
context.ssrContext!.head.push({
script: [
{
innerHTML: `
innerHTML:
`
(function(h,o,u,n,d) {
h=h[d]=h[d]||{q:[],onReady:function(c){h.q.push(c)}}
d=o.createElement(u);d.async=1;d.src=n
n=o.getElementsByTagName(u)[0];n.parentNode.insertBefore(d,n)
})(window,document,'script','https://www.datadoghq-browser-agent.com/eu1/v5/datadog-rum.js','DD_RUM')
window.DD_RUM.onReady(function() {
` +
(distinctId ? `window.DD_RUM.setUser({ id: '${distinctId}' });` : '') +
`
window.DD_RUM.setGlobalContextProperty('serverBaseUrl', '${baseUrl}');
window.DD_RUM.init({
clientToken: '${datadogClientToken}',
applicationId: '${datadogAppId}',
Expand Down Expand Up @@ -219,7 +248,11 @@ function resolveInitParams(app: PluginNuxtApp) {
const logger = useLogger()
const raygun = raygunKey?.length ? raygunKey : null
const datadog =
datadogClientToken?.length && datadogAppId?.length
datadogClientToken?.length &&
datadogAppId?.length &&
datadogSite?.length &&
datadogService?.length &&
datadogEnv?.length
? { datadogClientToken, datadogAppId, datadogSite, datadogService, datadogEnv }
: null

Expand Down
1 change: 1 addition & 0 deletions packages/frontend-2/type-augmentations/window.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare global {
interface Window {
rg4js?: import('raygun4js').RaygunV2
DD_RUM?: import('@datadog/browser-rum').RumGlobal
}
}

Expand Down
32 changes: 32 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7255,6 +7255,37 @@ __metadata:
languageName: node
linkType: hard

"@datadog/browser-core@npm:5.11.0":
version: 5.11.0
resolution: "@datadog/browser-core@npm:5.11.0"
checksum: fecb51873f6bd4fc6460b6c868ffa6c2670275c268aafe4543b6fe9ffaacaf56f8e2a7bfee388df3d0aad030168b1d127234ec9182c0333748fb0b004de76f40
languageName: node
linkType: hard

"@datadog/browser-rum-core@npm:5.11.0":
version: 5.11.0
resolution: "@datadog/browser-rum-core@npm:5.11.0"
dependencies:
"@datadog/browser-core": 5.11.0
checksum: c9e8502272c31dd1bdfc9106627d97f74ca240f51a0ae97f73a461295d3c81a3411f6eb04fbca73aad5158430342f4fc7a22236bc4377a127a07e4cbcdfa9a19
languageName: node
linkType: hard

"@datadog/browser-rum@npm:^5.11.0":
version: 5.11.0
resolution: "@datadog/browser-rum@npm:5.11.0"
dependencies:
"@datadog/browser-core": 5.11.0
"@datadog/browser-rum-core": 5.11.0
peerDependencies:
"@datadog/browser-logs": 5.11.0
peerDependenciesMeta:
"@datadog/browser-logs":
optional: true
checksum: 05d3dc0d0e93487bfe5942d87866c881bb58c00dabc86ddc143d1c852927578a14e268fe6ee0474d2b672498b39d9c76328640c66ccc965bae76d59f8b7b1870
languageName: node
linkType: hard

"@discoveryjs/json-ext@npm:^0.5.0, @discoveryjs/json-ext@npm:^0.5.3, @discoveryjs/json-ext@npm:^0.5.7":
version: 0.5.7
resolution: "@discoveryjs/json-ext@npm:0.5.7"
Expand Down Expand Up @@ -13812,6 +13843,7 @@ __metadata:
"@babel/preset-env": ^7.19.4
"@babel/preset-react": ^7.18.6
"@babel/preset-typescript": ^7.18.6
"@datadog/browser-rum": ^5.11.0
"@graphql-codegen/cli": ^2.13.6
"@graphql-codegen/client-preset": ^1.2.5
"@headlessui/vue": ^1.7.13
Expand Down

0 comments on commit 15b7bf3

Please sign in to comment.