-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add telemetry tracking to npm scripts #5
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds telemetry tracking to npm scripts by introducing a bash script that sends events to PostHog when running build, dev, and start commands. The implementation checks for user consent via a config file before sending telemetry data.
Key Changes:
- Created a bash script (
sendEvent.sh) that wraps npm script commands and sends telemetry events to PostHog - Modified all npm scripts (build, start, dev) to execute through the telemetry wrapper script
Reviewed Changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| templates/minimal/typescript/scripts/sendEvent.sh | New bash script that sends telemetry events to PostHog and executes the wrapped command |
| templates/minimal/typescript/package.json | Updated npm scripts to wrap commands with the telemetry tracking script |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| shift 2 # Remove event_name and -- | ||
| COMMAND="$@" # Everything after -- is the command | ||
|
|
||
| POSTHOG_API_KEY="phc_BiKfywNft5iBI8N7MxmuVCkb4GGZj4mDFXYPmOPUAI8" |
Copilot
AI
Oct 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PostHog API key is hardcoded in the script. This is a security risk as the key is exposed in version control and could be misused. Consider storing the API key in an environment variable or secure configuration file.
| POSTHOG_API_KEY="phc_BiKfywNft5iBI8N7MxmuVCkb4GGZj4mDFXYPmOPUAI8" | |
| # Read PostHog API key from environment variable | |
| if [ -z "$POSTHOG_API_KEY" ]; then | |
| echo "Error: POSTHOG_API_KEY environment variable is not set." | |
| exit 2 | |
| fi |
| fi | ||
|
|
||
| # Execute the command regardless of telemetry setting | ||
| exec $COMMAND |
Copilot
AI
Oct 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The command execution is not properly quoted, which will cause issues with commands containing spaces or special characters. Change to exec $@ or eval \"$COMMAND\" to properly handle arguments.
| exec $COMMAND | |
| exec "$@" |
| "command": "'"${COMMAND}"'", | ||
| "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" | ||
| } | ||
| }' |
Copilot
AI
Oct 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The curl request runs in the foreground, which will block npm script execution until the HTTP request completes or times out. Add & at the end of the curl command to run it in the background and avoid blocking the user's command.
| }' | |
| }' & |
| curl -s -X POST "${POSTHOG_HOST}/capture/" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{ | ||
| "api_key": "'"${POSTHOG_API_KEY}"'", | ||
| "event": "'"${EVENT_NAME}"'", | ||
| "distinct_id": "'"${user_uuid}"'", | ||
| "properties": { | ||
| "command": "'"${COMMAND}"'", | ||
| "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" | ||
| } | ||
| }' |
Copilot
AI
Oct 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The COMMAND and EVENT_NAME variables are directly interpolated into JSON without sanitization, creating a potential injection vulnerability if these values contain special characters like quotes or backslashes. Use jq to properly escape JSON values or implement proper string escaping.
| curl -s -X POST "${POSTHOG_HOST}/capture/" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{ | |
| "api_key": "'"${POSTHOG_API_KEY}"'", | |
| "event": "'"${EVENT_NAME}"'", | |
| "distinct_id": "'"${user_uuid}"'", | |
| "properties": { | |
| "command": "'"${COMMAND}"'", | |
| "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" | |
| } | |
| }' | |
| if ! command -v jq >/dev/null 2>&1; then | |
| echo "Error: jq is required but not installed." >&2 | |
| exit 2 | |
| fi | |
| curl -s -X POST "${POSTHOG_HOST}/capture/" \ | |
| -H "Content-Type: application/json" \ | |
| -d "$( | |
| jq -n \ | |
| --arg api_key "$POSTHOG_API_KEY" \ | |
| --arg event "$EVENT_NAME" \ | |
| --arg distinct_id "$user_uuid" \ | |
| --arg command "$COMMAND" \ | |
| --arg timestamp "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ | |
| '{ | |
| api_key: $api_key, | |
| event: $event, | |
| distinct_id: $distinct_id, | |
| properties: { | |
| command: $command, | |
| timestamp: $timestamp | |
| } | |
| }' | |
| )" |
Send events to posthog when running npm scripts for build, dev and start