Skip to content

Commit

Permalink
infra(Analytics): track app installs and uninstalls (#160)
Browse files Browse the repository at this point in the history
* add tracking

* lint
  • Loading branch information
jakebolam committed Mar 16, 2019
1 parent 3725a30 commit 31678ee
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/serverless-webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const AWS = require('aws-sdk')
const lambda = new AWS.Lambda()

const trackInstall = require('./tasks/trackInstall')
const isMessageForBot = require('./utils/isMessageForBot')

function invokeLambda(payload) {
Expand Down Expand Up @@ -49,6 +50,15 @@ module.exports.handler = async (event, context) => {
const payload =
typeof event.body === 'string' ? JSON.parse(event.body) : event.body

if (name === 'installation') {
await trackInstall(payload)

return {
statusCode: 200,
body: 'Tracked install count',
}
}

if (name !== 'issue_comment') {
return {
statusCode: 201,
Expand Down
33 changes: 33 additions & 0 deletions src/tasks/trackInstall/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const Analytics = require('../../utils/Analytics')

function getTrackName(action) {
if (action === 'deleted') {
return 'Deleted'
} else if (action === 'created') {
return 'Created'
} else {
return 'Unknown'
}
}

async function trackInstall(payload) {
const analytics = new Analytics({
owner: payload.installation.account.login,
user: payload.sender.login,
log: {
info: console.log, // eslint-disable-line no-console
error: console.error, // eslint-disable-line no-console
},
})

const trackName = getTrackName(payload.action)
analytics.track(`trackInstallation${trackName}`, {
action: payload.action,
installation: payload.installation,
repositories: payload.repositories && payload.repositories.length,
})

await analytics.finishQueue()
}

module.exports = trackInstall
21 changes: 21 additions & 0 deletions test/serverless-webhook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ const {
describe('Serverless Webhook', () => {
const mockContext = {}

test('If installation track the install state', async () => {
const mockEvent = {
headers: {
'x-github-event': 'installation',
},
body: {
installation: {
account: {
login: 'testaccount',
},
},
sender: {
login: 'jakebolam',
},
},
}
const response = await serverlessWebhookHandler(mockEvent, mockContext)
expect(response.body).toEqual('Tracked install count')
// TODO: expect lambda.invoke TO NOT BE CALLED
})

test('If not an issue comment, exit', async () => {
const mockEvent = {
headers: {
Expand Down

0 comments on commit 31678ee

Please sign in to comment.