usermaven / usermaven-js

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

fix: cross domain tracking cases #125

Closed seeratawan01 closed 2 weeks ago

seeratawan01 commented 2 weeks ago

PR Type

Enhancement, Bug fix


Description


Changes walkthrough ๐Ÿ“

Relevant files
Enhancement
cookie.ts
Enhance cookie management and domain extraction logic       

packages/javascript-sdk/src/utils/cookie.ts
  • Improved cookie retrieval by decoding the cookie value.
  • Enhanced domain extraction logic with new helper methods.
  • Added optional path parameter to cookie deletion.
  • Refactored code to handle IP addresses and domain extraction more
    robustly.
  • +56/-24 

    ๐Ÿ’ก 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: 85
    ๐Ÿงช No relevant tests
    ๐Ÿ”’ No security concerns identified
    โšก Recommended focus areas for review

    Possible Bug
    The method `extractTopLevelDomain` uses a regex that might not correctly handle all valid TLDs, especially newer or less common ones. Performance Issue
    The `extractHostname` method could be optimized by using URL parsing instead of manual string operations, which might be error-prone and less efficient.
    Code feedback:
    relevant filepackages/javascript-sdk/src/utils/cookie.ts
    suggestion       Consider using the URL API for parsing and handling URLs in the `extractHostname` method to improve reliability and readability. [important]
    relevant linelet hostname: string;

    relevant filepackages/javascript-sdk/src/utils/cookie.ts
    suggestion       Refactor the `extractTopLevelDomain` method to use a more comprehensive regex or a dedicated library for TLD extraction to ensure accuracy. [important]
    relevant lineconst DOMAIN_MATCH_REGEX = /[a-z0-9][a-z0-9-]+\.[a-z.]{2,6}$/i;

    github-actions[bot] commented 2 weeks ago

    PR Code Suggestions โœจ

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Improve the reliability and accuracy of hostname extraction by utilizing URL parsing ___ **Optimize the extractHostname method by using URL parsing instead of manual string
    operations to improve reliability and handle edge cases.** [packages/javascript-sdk/src/utils/cookie.ts [68-74]](https://github.com/usermaven/usermaven-js/pull/125/files#diff-304a2517b554eaf957917f59494746ca14cd003a21f6076e2db28255dcafe5ccR68-R74) ```diff -if (url.indexOf("//") > -1) { - hostname = url.split('/')[2]; -} else { - hostname = url.split('/')[0]; -} -hostname = hostname.split(':')[0]; -hostname = hostname.split('?')[0]; +const parsedUrl = new URL(url); +return parsedUrl.hostname; ```
    Suggestion importance[1-10]: 9 Why: This suggestion significantly enhances the reliability of hostname extraction by using the URL API, which is more robust and handles edge cases better than manual string operations.
    9
    Improve error handling by catching exceptions that occur when attempting to set cookies ___ **Add error handling for the case where document.cookie is not accessible or
    modifiable, such as in some restricted environments or when cookies are blocked by
    the browser.** [packages/javascript-sdk/src/utils/cookie.ts [16]](https://github.com/usermaven/usermaven-js/pull/125/files#diff-304a2517b554eaf957917f59494746ca14cd003a21f6076e2db28255dcafe5ccR16-R16) ```diff -document.cookie = `${name}=${value};${expires};path=/;domain=${this.cookieDomain}${secureFlag}${httpOnlyFlag}`; +try { + document.cookie = `${name}=${value};${expires};path=/;domain=${this.cookieDomain}${secureFlag}${httpOnlyFlag}`; +} catch (e) { + console.error('Failed to set cookie:', e); +} ```
    Suggestion importance[1-10]: 7 Why: Adding error handling for setting cookies is beneficial, especially in environments where cookies might be restricted. This enhances the code's robustness by preventing unhandled exceptions.
    7
    Possible issue
    Enhance the robustness of the cookie deletion process by handling potential undefined or empty domain values ___ **Ensure that the delete method correctly handles the case where this.cookieDomain
    might be undefined or an empty string, which could lead to an invalid cookie
    deletion command.** [packages/javascript-sdk/src/utils/cookie.ts [32]](https://github.com/usermaven/usermaven-js/pull/125/files#diff-304a2517b554eaf957917f59494746ca14cd003a21f6076e2db28255dcafe5ccR32-R32) ```diff -document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${path};domain=${this.cookieDomain}`; +document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${path};domain=${this.cookieDomain || 'defaultDomain.com'}`; ```
    Suggestion importance[1-10]: 8 Why: This suggestion addresses a potential issue where the cookie deletion might fail if `this.cookieDomain` is undefined or empty. By providing a default domain, it ensures that the deletion command remains valid, enhancing robustness.
    8
    Best practice
    Enhance the accuracy and maintainability of IP address validation ___ **Refactor the isIpAddress method to use a regular expression for IP address
    validation to ensure accuracy and improve maintainability.** [packages/javascript-sdk/src/utils/cookie.ts [62-63]](https://github.com/usermaven/usermaven-js/pull/125/files#diff-304a2517b554eaf957917f59494746ca14cd003a21f6076e2db28255dcafe5ccR62-R63) ```diff -const parts = url.split('.'); -return parts.length === 4 && parts.every(part => !isNaN(Number(part))); +return /^(\d{1,3}\.){3}\d{1,3}$/.test(url); ```
    Suggestion importance[1-10]: 6 Why: Using a regular expression for IP address validation improves accuracy and maintainability, as it simplifies the logic and reduces potential errors in manual parsing.
    6