usermaven / usermaven-js

Usermaven provides instant actionable analytics to grow your SaaS business.
MIT License
1 stars 2 forks source link

fix: auto capture issue #121

Closed seeratawan01 closed 2 weeks ago

seeratawan01 commented 2 weeks ago

PR Type

Bug fix, Enhancement


Description


Changes walkthrough ๐Ÿ“

Relevant files
Enhancement
autocapture.ts
Improve URL handling and add comments in AutoCapture         

packages/javascript-sdk/src/tracking/autocapture.ts
  • Added comments for clarity on capturing href attributes.
  • Improved URL sanitization by handling empty URLs and using a base URL
    for parsing.
  • Enhanced error handling by returning the original URL with basic
    sanitization if parsing fails.
  • +7/-9     

    ๐Ÿ’ก PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    github-actions[bot] commented 2 weeks ago

    PR Reviewer Guide ๐Ÿ”

    Here are some key observations to aid the review process:

    ๐Ÿ… Score: 82
    ๐Ÿงช No relevant tests
    ๐Ÿ”’ No security concerns identified
    โšก Recommended focus areas for review

    Error Handling
    The error handling in sanitizeUrl might not be robust enough as it returns an empty string for non-http/https URLs, which could lead to data loss in analytics. Code Redundancy
    The check for 'a' tags and capturing href attributes could be optimized to avoid redundancy and improve performance.
    Code feedback:
    relevant filepackages/javascript-sdk/src/tracking/autocapture.ts
    suggestion       Consider using a more descriptive variable name than 'href' for storing the URL, such as 'elementHref' or 'currentHref', to improve code readability and maintainability. [medium]
    relevant lineif (href !== null) {

    relevant filepackages/javascript-sdk/src/tracking/autocapture.ts
    suggestion       Instead of returning an empty string when the URL protocol is not http or https, consider logging this event or handling it in a way that does not potentially lose important tracking information. [important]
    relevant lineif (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {

    github-actions[bot] commented 2 weeks ago

    PR Code Suggestions โœจ

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Reset the href variable at the start of each iteration to avoid data leakage between iterations ___ **Ensure that the href variable is reset to null at the start of each iteration in
    _each to prevent incorrect data being carried over from previous iterations.** [packages/javascript-sdk/src/tracking/autocapture.ts [165-170]](https://github.com/usermaven/usermaven-js/pull/121/files#diff-0cd940fd1ac22f0b70699cc61d155377da671515a538fe04bbfdda9693448b5bR165-R170) ```diff _each(targetElementList, (el) => { + href = null; // Reset href to null at the start of each iteration // Check for 'a' tag and capture href if (isTag(el, 'a')) { const hrefAttr = el.getAttribute('href'); if (hrefAttr !== null && shouldCaptureElement(el) && shouldCaptureValue(hrefAttr)) { href = hrefAttr; ```
    Suggestion importance[1-10]: 9 Why: This suggestion addresses a potential bug where the `href` variable could retain values from previous iterations, leading to incorrect data capture. Resetting `href` at the start of each iteration ensures data integrity.
    9
    Check that elementsJson[0] exists before property assignment to prevent runtime errors ___ **Add a check to ensure elementsJson[0] exists before attempting to assign properties
    to avoid potential runtime errors.** [packages/javascript-sdk/src/tracking/autocapture.ts [188-189]](https://github.com/usermaven/usermaven-js/pull/121/files#diff-0cd940fd1ac22f0b70699cc61d155377da671515a538fe04bbfdda9693448b5bR188-R189) ```diff -if (href !== null) { +if (href !== null && elementsJson.length > 0) { elementsJson[0]['attr__href'] = this.sanitizeUrl(href); ```
    Suggestion importance[1-10]: 8 Why: This suggestion prevents potential runtime errors by ensuring that `elementsJson[0]` exists before assigning properties to it, which is crucial for robust error handling.
    8
    Enhancement
    Enhance the check for an empty URL to handle strings that contain only whitespace ___ **Use a more specific condition to check for an empty url string in sanitizeUrl to
    handle cases where url might be only whitespace.** [packages/javascript-sdk/src/tracking/autocapture.ts [269]](https://github.com/usermaven/usermaven-js/pull/121/files#diff-0cd940fd1ac22f0b70699cc61d155377da671515a538fe04bbfdda9693448b5bR269-R269) ```diff -if (!url) return ''; +if (!url.trim()) return ''; ```
    Suggestion importance[1-10]: 7 Why: The suggestion improves the robustness of the `sanitizeUrl` function by ensuring that URLs containing only whitespace are treated as empty, which enhances input validation.
    7
    Best practice
    Log errors when URL parsing fails to aid in debugging and error tracking ___ **Modify the catch block in sanitizeUrl to log the error for better debugging and
    error tracking.** [packages/javascript-sdk/src/tracking/autocapture.ts [276-279]](https://github.com/usermaven/usermaven-js/pull/121/files#diff-0cd940fd1ac22f0b70699cc61d155377da671515a538fe04bbfdda9693448b5bR276-R279) ```diff catch (e) { + console.error('Failed to parse URL:', e); // If URL parsing fails, return the original url after basic sanitization return this.encodeHtml(url); ```
    Suggestion importance[1-10]: 6 Why: Logging errors when URL parsing fails provides valuable information for debugging and error tracking, although it does not directly affect functionality.
    6