Skip to content

Commit

Permalink
refactor: refactoring after review
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed Oct 31, 2023
1 parent e6ccd35 commit 9787ccb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bin/paragon-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ const COMMANDS = {
const [command, ...commandArgs] = process.argv.slice(2);
const executor = COMMANDS[command];

sendTrackInfo(command, 'trackCLICommands');
sendTrackInfo('openedx.paragon.cli-command.used', { command });

if (!executor) {
// eslint-disable-next-line no-console
Expand Down
2 changes: 1 addition & 1 deletion component-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ program
.argument('<ComponentName>', 'Component must have a name', validateComponentName)
.action((componentName) => {
// send data to analytics
sendTrackInfo(componentName, 'trackGenerateComponent');
sendTrackInfo('openedx.paragon.functions.track-generate-component.created', { componentName });
const componentDir = path.resolve(__dirname, `../src/${componentName}`);
// create directory for the component files
fs.mkdirSync(componentDir);
Expand Down
10 changes: 5 additions & 5 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ const axios = require('axios');

/**
* Sends request to the Netlify function to inform about specified event.
* @param {string} eventName - tracking event name
* @param {string} trackFunctionName - tracking function name
* @param {string} eventId - tracking event id
* @param {object} properties - tracking properties
*/
function sendTrackInfo(eventName, trackFunctionName) {
function sendTrackInfo(eventId, properties) {
const { BASE_URL, TRACK_ANONYMOUS_ANALYTICS } = process.env;
if (TRACK_ANONYMOUS_ANALYTICS) {
const url = `${BASE_URL}/.netlify/functions/${trackFunctionName}`;
axios.post(url, { eventName })
const url = `${BASE_URL}/.netlify/functions/sendTrackData`;
axios.post(url, { eventId, properties })
.then(result => {
// eslint-disable-next-line no-console
console.log(`Track info is successfully sent (status ${result.status})`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ exports.handler = async function eventHandler(event) {
if (event.httpMethod !== 'POST') {
return { statusCode: 405, body: 'Method Not Allowed' };
}
const { eventName } = JSON.parse(event.body);
const { eventId, properties } = JSON.parse(event.body);
// dispatch event to Segment
analytics.track({
anonymousId: uuidv4(),
event: 'openedx.paragon.functions.track-cli-commands.run',
properties: { eventName },
event: eventId,
properties,
});

return {
Expand Down
25 changes: 7 additions & 18 deletions www/netlify/functions/trackGenerateComponent.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
const { v4: uuidv4 } = require('uuid');
const Analytics = require('analytics-node');

const analytics = new Analytics(process.env.SEGMENT_KEY);
const { handler: actualHandler } = require('./sendAnalyticsData');

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

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

0 comments on commit 9787ccb

Please sign in to comment.