Skip to content

Comments

chore: enable umami on production only#131

Merged
iamEvanYT merged 3 commits intoMultiboxLabs:mainfrom
MHase:chore/analytics-prod-only
Jun 23, 2025
Merged

chore: enable umami on production only#131
iamEvanYT merged 3 commits intoMultiboxLabs:mainfrom
MHase:chore/analytics-prod-only

Conversation

@MHase
Copy link
Contributor

@MHase MHase commented Jun 19, 2025

Refactor of the Umami analytics integration by:

  • extracting the script loader into a separate component
  • loading it only when process.env.NODE_ENV === 'production'

(so analytics won't be affected with fake data during development)

Summary by CodeRabbit

  • New Features

    • Analytics script is now dynamically loaded only in production environments, improving privacy and performance.
  • Refactor

    • Embedded analytics script and related comments were removed from the HTML and replaced with a dynamic loader component.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 19, 2025

Walkthrough

The changes remove the hardcoded Umami Analytics script from the HTML and instead introduce a React component, UmamiScriptLoader, which dynamically injects the script at runtime in production environments. The main application file is updated to include this new component, ensuring analytics are only loaded as needed.

Changes

File(s) Change Summary
src/renderer/index.html Removed the embedded Umami Analytics script and its related comments from the HTML.
src/renderer/src/App.tsx Added import and usage of UmamiScriptLoader component; reordered imports; minor JSX structure update.
src/renderer/src/components/analytics/umami.tsx Introduced new UmamiScriptLoader React component for dynamic analytics script injection in production only.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant App
    participant UmamiScriptLoader
    participant Document

    User->>App: Loads application
    App->>UmamiScriptLoader: Renders component
    UmamiScriptLoader->>Document: On mount, injects Umami script (if production)
Loading

Poem

A script once lived in HTML’s embrace,
Now React delivers it, just in case.
If production’s the scene, analytics appear—
Quietly loaded, then disappear.
The bunny hops, the code is clean,
Dynamic scripts, no trace unseen!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1d2b4a3 and 887a543.

📒 Files selected for processing (2)
  • src/renderer/src/App.tsx (2 hunks)
  • src/renderer/src/components/analytics/umami.tsx (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • src/renderer/src/App.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/renderer/src/components/analytics/umami.tsx
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: build (ubuntu-24.04-arm)
  • GitHub Check: build (macos-latest)
  • GitHub Check: build (windows-latest)
  • GitHub Check: build (macos-13)
  • GitHub Check: build (ubuntu-latest)
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
src/renderer/src/components/analytics/umami.tsx (2)

10-15: Consider error handling and configuration externalization.

The script creation and configuration logic is solid, but consider these improvements:

  1. Add error handling for script loading failures
  2. Consider externalizing the hardcoded configuration values to environment variables or a config file
 const script = document.createElement('script');
 script.src = '/umami.js';
 script.defer = true;
 script.setAttribute('data-host-url', 'https://umami.iamevan.dev');
 script.setAttribute('data-website-id', '846df382-cb68-4e59-a97e-76df33a73e90');
+
+script.onerror = () => {
+  console.warn('Failed to load Umami analytics script');
+};

7-21: ```bash
#!/bin/bash

Locate all usages of the UmamiScriptLoader component to check if it can mount multiple times

rg -n "UmamiScriptLoader" -g '*.tsx' -A3 -B3


</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

**Configuration used: CodeRabbit UI**
**Review profile: CHILL**
**Plan: Pro**


<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between c5cd6d8f9059b4d7c61bf08765b18340c387cbd2 and 20b4a75956357df0218245863f1319d4c18d73e2.

</details>

<details>
<summary>📒 Files selected for processing (3)</summary>

* `src/renderer/index.html` (0 hunks)
* `src/renderer/src/App.tsx` (2 hunks)
* `src/renderer/src/components/analytics/umami.tsx` (1 hunks)

</details>

<details>
<summary>💤 Files with no reviewable changes (1)</summary>

* src/renderer/index.html

</details>

<details>
<summary>🧰 Additional context used</summary>

<details>
<summary>🧬 Code Graph Analysis (1)</summary>

<details>
<summary>src/renderer/src/App.tsx (2)</summary><blockquote>

<details>
<summary>src/renderer/src/components/analytics/umami.tsx (1)</summary>

* `UmamiScriptLoader` (3-24)

</details>
<details>
<summary>src/renderer/src/components/main/platform.tsx (1)</summary>

* `PlatformProvider` (91-121)

</details>

</blockquote></details>

</details>

</details>

<details>
<summary>⏰ Context from checks skipped due to timeout of 90000ms (5)</summary>

* GitHub Check: build (macos-13)
* GitHub Check: build (windows-latest)
* GitHub Check: build (macos-latest)
* GitHub Check: build (ubuntu-24.04-arm)
* GitHub Check: build (ubuntu-latest)

</details>

<details>
<summary>🔇 Additional comments (4)</summary><blockquote>

<details>
<summary>src/renderer/src/components/analytics/umami.tsx (1)</summary>

`8-8`: **LGTM: Production environment check is correct.**

The condition properly restricts analytics loading to production environments only, which aligns with the PR objectives.

</details>
<details>
<summary>src/renderer/src/App.tsx (3)</summary>

`3-3`: **LGTM: Import statement is correct.**

The import path and naming convention follow the project structure properly.

---

`73-82`: **LGTM: Component integration is well-structured.**

The UmamiScriptLoader is correctly placed at the top level of the App component, ensuring early analytics initialization while maintaining the existing component hierarchy. The React fragment wrapper is appropriate for grouping multiple root elements.

---

`4-5`: **Import reordering looks good.**

The RouterProvider and Route imports have been reordered, which maintains consistency in the import organization.

</details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

Comment on lines 18 to 20
return () => {
document.body.removeChild(script);
};
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix potential error in script cleanup.

The cleanup function could throw an error if the script element doesn't exist in the DOM or was already removed.

Apply this diff to add safe cleanup:

 return () => {
-  document.body.removeChild(script);
+  if (script.parentNode) {
+    script.parentNode.removeChild(script);
+  }
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return () => {
document.body.removeChild(script);
};
return () => {
if (script.parentNode) {
script.parentNode.removeChild(script);
}
};
🤖 Prompt for AI Agents
In src/renderer/src/components/analytics/umami.tsx around lines 18 to 20, the
cleanup function removes the script element without checking if it exists in the
DOM, which can cause an error. Modify the cleanup function to first check if the
script element is still a child of document.body before attempting to remove it,
ensuring safe cleanup without errors.

script.setAttribute('data-host-url', 'https://umami.iamevan.dev');
script.setAttribute('data-website-id', '846df382-cb68-4e59-a97e-76df33a73e90');

document.body.appendChild(script);
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed? Can't you just do that below?

<script
  defer
  src="/umami.js"
  data-host-url="https://umami.iamevan.dev"
  data-website-id="846df382-cb68-4e59-a97e-76df33a73e90"
></script>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you enable it like this, it will also run during development, which isn't necessary and may capture random data, since developers often perform non-typical actions. This data won't help analyze how a 'normal' user behaves.

@MHase MHase force-pushed the chore/analytics-prod-only branch from 20b4a75 to 1d2b4a3 Compare June 23, 2025 18:41
@MHase MHase requested a review from iamEvanYT June 23, 2025 18:43
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/renderer/src/components/analytics/umami.tsx (2)

2-3: Add TypeScript return type annotation for better type safety.

Consider adding an explicit return type annotation to improve type safety and code documentation.

-export const UmamiScriptLoader = () => {
+export const UmamiScriptLoader = (): JSX.Element | null => {

8-13: Clarify script source and consider environment-based configuration.

The implementation is clean and uses the JSX approach effectively. However, consider these improvements:

  1. Script source clarity: The script loads from /umami.js (local) but connects to an external host. Ensure the Umami script file is available in your public folder.

  2. Environment-based configuration: The hardcoded values could be moved to environment variables for better flexibility across different environments.

Consider this approach for environment-based configuration:

  return <script
    defer
    src="/umami.js"
-   data-host-url="https://umami.iamevan.dev"
-   data-website-id="846df382-cb68-4e59-a97e-76df33a73e90"
+   data-host-url={process.env.VITE_UMAMI_HOST_URL || "https://umami.iamevan.dev"}
+   data-website-id={process.env.VITE_UMAMI_WEBSITE_ID || "846df382-cb68-4e59-a97e-76df33a73e90"}
  />
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 20b4a75 and 1d2b4a3.

📒 Files selected for processing (3)
  • src/renderer/index.html (0 hunks)
  • src/renderer/src/App.tsx (2 hunks)
  • src/renderer/src/components/analytics/umami.tsx (1 hunks)
💤 Files with no reviewable changes (1)
  • src/renderer/index.html
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/renderer/src/App.tsx
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: build (ubuntu-24.04-arm)
  • GitHub Check: build (macos-latest)
  • GitHub Check: build (windows-latest)
  • GitHub Check: build (macos-13)
  • GitHub Check: build (ubuntu-latest)
🔇 Additional comments (1)
src/renderer/src/components/analytics/umami.tsx (1)

5-7: Well-documented analytics choice.

The comments clearly explain the privacy-focused nature of Umami Analytics, which helps other developers understand the rationale behind this analytics solution.

Copy link
Member

@iamEvanYT iamEvanYT left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@iamEvanYT iamEvanYT merged commit 8d57b0c into MultiboxLabs:main Jun 23, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants