Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BD-46] feat: added Segment track for CLI scripts #2617

Conversation

PKulkoRaccoonGang
Copy link
Contributor

@PKulkoRaccoonGang PKulkoRaccoonGang commented Sep 11, 2023

Description

  • added Segment track for CLI scripts.

Merge Checklist

  • If your update includes visual changes, have they been reviewed by a designer? Send them a link to the Netlify deploy preview, if applicable.
  • Does your change adhere to the documented style conventions?
  • Do any prop types have missing descriptions in the Props API tables in the documentation site (check deploy preview)?
  • Were your changes tested using all available themes (see theme switcher in the header of the deploy preview, under the "Settings" icon)?
  • Were your changes tested in the example app?
  • Is there adequate test coverage for your changes?
  • Consider whether this change needs to reviewed/QA'ed for accessibility (a11y). If so, please add wittjeff and adamstankiewicz as reviewers on this PR.

Post-merge Checklist

  • Verify your changes were released to NPM at the expected version.
  • If you'd like, share your contribution in #show-and-tell.
  • 🎉 🙌 Celebrate! Thanks for your contribution.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Sep 11, 2023
@openedx-webhooks
Copy link

openedx-webhooks commented Sep 11, 2023

Thanks for the pull request, @PKulkoRaccoonGang!

When this pull request is ready, tag your edX technical lead.

@netlify
Copy link

netlify bot commented Sep 11, 2023

Deploy Preview for paragon-openedx ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit cae891e
🔍 Latest deploy log https://app.netlify.com/sites/paragon-openedx/deploys/65425f1351afae00082d1af6
😎 Deploy Preview https://deploy-preview-2617--paragon-openedx.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@PKulkoRaccoonGang PKulkoRaccoonGang force-pushed the Peter_Kulko/segment-track-for-cli-scripts branch 3 times, most recently from 646851c to c35e0c5 Compare September 11, 2023 15:18
@PKulkoRaccoonGang PKulkoRaccoonGang changed the title Peter kulko/segment track for cli scripts [BD-46] feat: added Segment track for CLI scripts Sep 11, 2023
@openedx-webhooks openedx-webhooks added blended PR is managed through 2U's blended developmnt program and removed open-source-contribution PR author is not from Axim or 2U labels Sep 11, 2023
@PKulkoRaccoonGang PKulkoRaccoonGang marked this pull request as draft September 11, 2023 15:19
@PKulkoRaccoonGang
Copy link
Contributor Author

Waiting #2609

@@ -0,0 +1,22 @@
const axios = require('axios');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we use paragon CLI in production axios it is required to have in dependencies instead of devDependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const executor = COMMANDS[command];

sendTrackInfo(command, 'trackCLICommands');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would track only for specific command

Copy link
Contributor Author

@PKulkoRaccoonGang PKulkoRaccoonGang Oct 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions will differ only in the transmitted parameter, otherwise, the logic will remain unchanged. I think it's better to reuse the code and not duplicate it

@monteri monteri self-requested a review September 12, 2023 09:12
@PKulkoRaccoonGang PKulkoRaccoonGang force-pushed the Peter_Kulko/segment-track-for-cli-scripts branch from 584a448 to f02a941 Compare October 30, 2023 15:03
@PKulkoRaccoonGang PKulkoRaccoonGang force-pushed the Peter_Kulko/segment-track-for-cli-scripts branch from f02a941 to cb9dcb8 Compare October 30, 2023 15:06
@PKulkoRaccoonGang PKulkoRaccoonGang marked this pull request as ready for review October 30, 2023 15:06
@codecov
Copy link

codecov bot commented Oct 30, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.28%. Comparing base (0a809ed) to head (cae891e).
Report is 740 commits behind head on alpha.

Additional details and impacted files
@@            Coverage Diff             @@
##            alpha    #2617      +/-   ##
==========================================
+ Coverage   92.82%   93.28%   +0.46%     
==========================================
  Files         235      215      -20     
  Lines        4237     3635     -602     
  Branches     1029      898     -131     
==========================================
- Hits         3933     3391     -542     
+ Misses        300      239      -61     
- Partials        4        5       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 1 to 23
const { v4: uuidv4 } = require('uuid');
const Analytics = require('analytics-node');

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

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-cli-commands.run',
properties: { eventName },
});

return {
statusCode: 200,
body: JSON.stringify({ success: true }),
};
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to create another netlify function here, instead we should modify the one we have, because they do the same thing.

I would do the following

  1. Rename this file to sendAnalyticsData.js and modify the function as follows:
exports.handler = async function eventHandler(event) {
  // Only allow POST
  if (event.httpMethod !== 'POST') {
    return { statusCode: 405, body: 'Method Not Allowed' };
  }
  const { eventId, properties } = JSON.parse(event.body);
  // dispatch event to Segment
  analytics.track({
    anonymousId: uuidv4(),
    event: eventId,
    properties,
  });

  return {
    statusCode: 200,
    body: JSON.stringify({ success: true }),
  };
};
  1. Keep trackGenerateComponent.js in order not to make a breaking change, old version of Paragon will still send data to this function, so wee need to keep this functionality for now. Although, we probably could reuse our new handler here to avoid code duplication, something like this (not sure if it will work):
const { handler: actualHandler } = require('./sendAnalyticsData');

exports.handler = async function eventHandler(event) {
  const body = JSON.parse(event.body);
  event.body = JSON.stringify({
    ...body,
    eventId: 'openedx.paragon.functions.track-generate-component.created',
    properties: { componentName: body.componentName }
  })
  
  return actualHandler(event);
};
  1. Modify sendTrackInfo function to
function sendTrackInfo(eventId, properties) {
  const { BASE_URL, TRACK_ANONYMOUS_ANALYTICS } = process.env;
  if (TRACK_ANONYMOUS_ANALYTICS) {
    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})`);
      }).catch(error => {
        // eslint-disable-next-line no-console
        console.log(`Track info request failed (${error})`);
      });
  }
}

Then you could easily use this one function for any event you want, e.g. to track component generation you would do:

sendTrackInfo('openedx.paragon.functions.track-generate-component.created', { componentName });

to track CLI commands:

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

basically it will be able to handle any event you want in the future also without any modifications.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@PKulkoRaccoonGang PKulkoRaccoonGang force-pushed the Peter_Kulko/segment-track-for-cli-scripts branch from cb9f70d to 9787ccb Compare October 31, 2023 17:13
@@ -173,6 +174,8 @@ const COMMANDS = {
const [command, ...commandArgs] = process.argv.slice(2);
const executor = COMMANDS[command];

sendTrackInfo('openedx.paragon.cli-command.used', { command });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think it might be a good idea to add additional info about the command, for example if it succeeded or not. What do you think about moving this code into try / catch block below and sending status of execution?
Something like:

try {
    await executor.executor(commandArgs);
    sendTrackInfo('openedx.paragon.cli-command.used', { command, status: 'success' });
  } catch (error) {
    // eslint-disable-next-line no-console
    console.error(chalk.red.bold('An error occurred:', error.message));
    sendTrackInfo('openedx.paragon.cli-command.used', { command, status: 'error', errorMsg: error.message });
    process.exit(1);
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added, thanks

@viktorrusakov viktorrusakov merged commit 9af9a64 into openedx:alpha Nov 9, 2023
8 checks passed
@openedx-webhooks
Copy link

@PKulkoRaccoonGang 🎉 Your pull request was merged! Please take a moment to answer a two question survey so we can improve your experience in the future.

@viktorrusakov viktorrusakov linked an issue Nov 9, 2023 that may be closed by this pull request
@edx-semantic-release
Copy link
Contributor

🎉 This PR is included in version 22.0.0-alpha.14 🎉

The release is available on:

Your semantic-release bot 📦🚀

@openedx-semantic-release-bot

🎉 This PR is included in version 23.0.0-alpha.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

PKulkoRaccoonGang added a commit that referenced this pull request Aug 1, 2024
* feat: added Segment track for CLI scripts

* refactor: refactoring after review

* refactor: refactoring after review

* refactor: refactoring after review

* refactor: code refactoring
PKulkoRaccoonGang added a commit that referenced this pull request Aug 4, 2024
* feat: added Segment track for CLI scripts

* refactor: refactoring after review

* refactor: refactoring after review

* refactor: refactoring after review

* refactor: code refactoring
PKulkoRaccoonGang added a commit that referenced this pull request Aug 4, 2024
* feat: added Segment track for CLI scripts

* refactor: refactoring after review

* refactor: refactoring after review

* refactor: refactoring after review

* refactor: code refactoring
PKulkoRaccoonGang added a commit that referenced this pull request Aug 5, 2024
* feat: added Segment track for CLI scripts

* refactor: refactoring after review

* refactor: refactoring after review

* refactor: refactoring after review

* refactor: code refactoring
@openedx-semantic-release-bot

🎉 This PR is included in version 23.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blended PR is managed through 2U's blended developmnt program released on @alpha released
Projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

[CLI improvements] Segment events
6 participants