Skip to content

Commit

Permalink
Added route to track user analytics on signup, ping Slack webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisischris committed Oct 2, 2024
1 parent 31fefee commit 3fd15c3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Fastify from 'fastify';
import { addProperties, addReports, addResults, addScans, deleteProperties, deleteReports, deleteUser, getApikey, getCharts, getFilters, getProperties, getReports, getResultsAll, getResultsMessages, getResultsSchema, getResultsTags, getResultsUrls, getScans, getUpdates, help, updateProperties, updateReports } from '#src/routes';
import { addProperties, addReports, addResults, addScans, deleteProperties, deleteReports, deleteUser, getApikey, getCharts, getFilters, getProperties, getReports, getResultsAll, getResultsMessages, getResultsSchema, getResultsTags, getResultsUrls, getScans, getUpdates, help, trackUser, updateProperties, updateReports } from '#src/routes';
import { CognitoJwtVerifier } from 'aws-jwt-verify';
import { db } from './utils';
import { getScan } from './routes/getScan';
Expand Down Expand Up @@ -64,6 +64,7 @@ fastify.delete('/delete/user', {}, async (request, reply) => deleteUser({ reques

// MISC requests
fastify.post('/help', {}, async (request, reply) => help({ request, reply }));
fastify.post('/track/user', {}, async (request, reply) => trackUser({ request, reply }));

fastify.listen({ port: 3000 }, (err) => {
console.log(`Server listening on ${fastify.server.address().port}`)
Expand Down
3 changes: 2 additions & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export * from './webhookMonitorUpdate'
export * from './getFilters'
export * from './getCharts'
export * from './getApikey'
export * from './deleteUser'
export * from './deleteUser'
export * from './trackUser'
39 changes: 39 additions & 0 deletions src/routes/trackUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { jwtClaims } from '#src/app';
import { db } from '#src/utils';

export const trackUser = async ({ request, reply }) => {
await db.connect();
const user = (await db.query({
text: `SELECT * FROM "users" WHERE "id"=$1`,
values: [jwtClaims.sub],
})).rows?.[0];

const analytics = {
country: request.raw.headers?.['cloudfront-viewer-country-name'],
state: request.raw.headers?.['cloudfront-viewer-country-region-name'],
city: request.raw.headers?.['cloudfront-viewer-city'],
zip: request.raw.headers?.['cloudfront-viewer-postal-code'],
ip: request.raw.headers?.['cloudfront-viewer-address'],
device: request.raw.headers?.['cloudfront-is-desktop-viewer'] === 'true' ? 'desktop' :
request.raw.headers?.['cloudfront-is-tablet-viewer'] === 'true' ? 'tablet' :
request.raw.headers?.['cloudfront-is-mobile-viewer'] === 'true' ? 'mobile' : 'unknown',
os: request.raw.headers?.['cloudfront-is-ios-viewer'] === 'true' ? 'ios' :
request.raw.headers?.['cloudfront-is-ios-viewer'] === 'true' ? 'android' :
request.raw.headers?.['sec-ch-ua-platform']?.replaceAll('"', ''),
};
await db.query(`UPDATE "users" SET "analytics"=$1 WHERE "id"=$2`, [JSON.stringify(analytics), jwtClaims.sub]);

// Send Slack notification
await fetch(process.env.SLACK_WEBHOOK, {
method: 'POST',
body: JSON.stringify({
text: `*${user.first_name} ${user.last_name} (${user.email})* just signed up for *Equalify* from *${analytics?.city}, ${analytics?.state}* on *${analytics?.device}*`
})
})

await db.clean();
return {
status: 'success',
message: 'User tracked successfully',
};
}

0 comments on commit 3fd15c3

Please sign in to comment.