From 2b79e3aeaf2ab15cd782b93f206ff592d5cfe10f Mon Sep 17 00:00:00 2001 From: PKulkoRaccoonGang Date: Tue, 26 Dec 2023 22:01:19 +0200 Subject: [PATCH] refactor: code refactoring --- .../functions/trackGenerateComponent.js | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/www/netlify/functions/trackGenerateComponent.js b/www/netlify/functions/trackGenerateComponent.js index 52b21f548d..5f44db94a7 100644 --- a/www/netlify/functions/trackGenerateComponent.js +++ b/www/netlify/functions/trackGenerateComponent.js @@ -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 }), + }; };