Skip to content

Commit 7cbaa77

Browse files
authored
Merge pull request #4386 from FlowFuse/4381-invites-posthog
Track server-side invitation events with PostHog
2 parents 572e808 + 5bfd259 commit 7cbaa77

7 files changed

Lines changed: 73 additions & 6 deletions

File tree

forge/db/controllers/Invitation.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ module.exports = {
128128
invitor,
129129
role
130130
})
131+
132+
// record acceptance in product analytics tool
133+
app.product.capture(invitedUser.email, '$ff-invite-accepted', {
134+
'accepted-at': new Date().toISOString(),
135+
'invite-id': invitation.hashid
136+
}, {
137+
team: invitation.team.hashid
138+
})
131139
},
132140

133141
rejectInvitation: async (app, invitation, user) => {

forge/forge.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const housekeeper = require('./housekeeper')
1616
const license = require('./licensing')
1717
const notifications = require('./notifications')
1818
const postoffice = require('./postoffice')
19+
const product = require('./product')
1920
const routes = require('./routes')
2021
const settings = require('./settings')
2122
const { finishSetup } = require('./setup')
@@ -438,6 +439,8 @@ module.exports = async (options = {}) => {
438439
// Post Office : handles email
439440
await server.register(postoffice)
440441
await server.register(notifications)
442+
// Product service handles reporting to PostHog
443+
await server.register(product, runtimeConfig.telemetry.frontend?.posthog)
441444
// Comms : real-time communication broker
442445
await server.register(comms)
443446
// Containers:

forge/product/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const fp = require('fastify-plugin')
2+
const { PostHog } = require('posthog-node')
3+
4+
module.exports = fp(async function (app, _opts) {
5+
let client
6+
if (_opts && _opts.apikey) {
7+
const apiHost = _opts?.apiurl || 'https://app.posthog.com'
8+
const apiKey = _opts?.apikey
9+
const options = {
10+
host: apiHost
11+
}
12+
13+
client = new PostHog(apiKey, options)
14+
}
15+
16+
function capture (distinctId, event, properties, groups) {
17+
if (client) {
18+
if (!properties) {
19+
properties = {}
20+
}
21+
client.capture({ distinctId, event, properties, groups })
22+
}
23+
}
24+
25+
app.decorate('product', {
26+
capture
27+
})
28+
}, { name: 'app.product' })

forge/routes/ui/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module.exports = async function (app) {
4343
}
4444

4545
if (telemetry.frontend.posthog?.apikey) {
46+
// add to frontend
4647
const apihost = telemetry.frontend.posthog.apiurl || 'https://app.posthog.com'
4748
const apikey = telemetry.frontend.posthog.apikey
4849
const options = {

frontend/src/api/user.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,6 @@ const getTeamInvitations = async () => {
104104
}
105105
const acceptTeamInvitation = async (invitationId, teamId) => {
106106
return client.patch('/api/v1/user/invitations/' + invitationId).then(res => {
107-
product.capture('$ff-invite-accepted', {
108-
'invite-id': invitationId,
109-
'accepted-at': (new Date()).toISOString()
110-
}, {
111-
team: teamId
112-
})
113107
return res.data
114108
})
115109
}

package-lock.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
"pg": "^8.11.3",
9696
"pino": "^8.15.1",
9797
"pino-pretty": "^10.0.0",
98+
"posthog-node": "^4.2.0",
9899
"qrcode": "^1.5.3",
99100
"random-words": "~2.0.0",
100101
"semver": "~7.6.0",

0 commit comments

Comments
 (0)