Skip to content

Commit

Permalink
refactor: code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed Dec 26, 2023
1 parent 7c399b1 commit 2b79e3a
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions www/netlify/functions/trackGenerateComponent.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
const { handler: actualHandler } = require('./sendAnalyticsData');
const { v4: uuidv4 } = require('uuid');
const Analytics = require('analytics-node');
const { COMPONENT_GENERATED_EVENT } = require('../../segment-events');

const analytics = new Analytics(process.env.SEGMENT_KEY);

exports.handler = async function eventHandler(event) {
const body = JSON.parse(event.body);
event.body = JSON.stringify({
...body,
eventId: COMPONENT_GENERATED_EVENT,
properties: { componentName: body.componentName },
// Only allow POST
if (event.httpMethod !== 'POST') {
return { statusCode: 405, body: 'Method Not Allowed' };
}
const { componentName } = JSON.parse(event.body);
// dispatch event to Segment
analytics.track({
anonymousId: uuidv4(),
event: COMPONENT_GENERATED_EVENT,
properties: { componentName },
});

return actualHandler(event);
return {
statusCode: 200,
body: JSON.stringify({ success: true }),
};
};

0 comments on commit 2b79e3a

Please sign in to comment.