Skip to content

Commit

Permalink
Merge pull request #4386 from FlowFuse/4381-invites-posthog
Browse files Browse the repository at this point in the history
Track server-side invitation events with PostHog
  • Loading branch information
Steve-Mcl committed Aug 27, 2024
2 parents 572e808 + 5bfd259 commit 7cbaa77
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 6 deletions.
8 changes: 8 additions & 0 deletions forge/db/controllers/Invitation.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ module.exports = {
invitor,
role
})

// record acceptance in product analytics tool
app.product.capture(invitedUser.email, '$ff-invite-accepted', {
'accepted-at': new Date().toISOString(),
'invite-id': invitation.hashid
}, {
team: invitation.team.hashid
})
},

rejectInvitation: async (app, invitation, user) => {
Expand Down
3 changes: 3 additions & 0 deletions forge/forge.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const housekeeper = require('./housekeeper')
const license = require('./licensing')
const notifications = require('./notifications')
const postoffice = require('./postoffice')
const product = require('./product')
const routes = require('./routes')
const settings = require('./settings')
const { finishSetup } = require('./setup')
Expand Down Expand Up @@ -438,6 +439,8 @@ module.exports = async (options = {}) => {
// Post Office : handles email
await server.register(postoffice)
await server.register(notifications)
// Product service handles reporting to PostHog
await server.register(product, runtimeConfig.telemetry.frontend?.posthog)
// Comms : real-time communication broker
await server.register(comms)
// Containers:
Expand Down
28 changes: 28 additions & 0 deletions forge/product/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fp = require('fastify-plugin')
const { PostHog } = require('posthog-node')

module.exports = fp(async function (app, _opts) {
let client
if (_opts && _opts.apikey) {
const apiHost = _opts?.apiurl || 'https://app.posthog.com'
const apiKey = _opts?.apikey
const options = {
host: apiHost
}

client = new PostHog(apiKey, options)
}

function capture (distinctId, event, properties, groups) {
if (client) {
if (!properties) {
properties = {}
}
client.capture({ distinctId, event, properties, groups })
}
}

app.decorate('product', {
capture
})
}, { name: 'app.product' })
1 change: 1 addition & 0 deletions forge/routes/ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = async function (app) {
}

if (telemetry.frontend.posthog?.apikey) {
// add to frontend
const apihost = telemetry.frontend.posthog.apiurl || 'https://app.posthog.com'
const apikey = telemetry.frontend.posthog.apikey
const options = {
Expand Down
6 changes: 0 additions & 6 deletions frontend/src/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,6 @@ const getTeamInvitations = async () => {
}
const acceptTeamInvitation = async (invitationId, teamId) => {
return client.patch('/api/v1/user/invitations/' + invitationId).then(res => {
product.capture('$ff-invite-accepted', {
'invite-id': invitationId,
'accepted-at': (new Date()).toISOString()
}, {
team: teamId
})
return res.data
})
}
Expand Down
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"pg": "^8.11.3",
"pino": "^8.15.1",
"pino-pretty": "^10.0.0",
"posthog-node": "^4.2.0",
"qrcode": "^1.5.3",
"random-words": "~2.0.0",
"semver": "~7.6.0",
Expand Down

0 comments on commit 7cbaa77

Please sign in to comment.