usermaven / usermaven-js

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

Next #120

Closed seeratawan01 closed 2 weeks ago

seeratawan01 commented 2 weeks ago

PR Type

enhancement, tests


Description


Changes walkthrough ๐Ÿ“

Relevant files
Enhancement
20 files
client.ts
Implement UsermavenClient for event tracking and user identification

packages/javascript-sdk/src/core/client.ts
  • Implemented UsermavenClient class with various tracking methods.
  • Added initialization logic for browser features and transport methods.
  • Introduced methods for user identification and event tracking.
  • +580/-0 
    autocapture.ts
    Add AutoCapture class for DOM event tracking                         

    packages/javascript-sdk/src/tracking/autocapture.ts
  • Created AutoCapture class for DOM event tracking.
  • Added methods to handle DOM events and capture element properties.
  • Implemented logic to sanitize and process captured data.
  • +318/-0 
    form-tracking.ts
    Enhance FormTracking with field change tracking                   

    packages/javascript-sdk/src/tracking/form-tracking.ts
  • Enhanced FormTracking class with options for tracking form
    submissions.
  • Added methods to handle form field changes and capture attributes.
  • Introduced singleton pattern for FormTracking instance.
  • +91/-39 
    index.ts
    Export UsermavenClient and implement script initialization

    packages/javascript-sdk/src/index.ts
  • Exported UsermavenClient and related types.
  • Implemented initialization logic from script tags.
  • Added function to initialize client with configuration.
  • +166/-0 
    scroll-depth.ts
    Implement ScrollDepth class for scroll event tracking       

    packages/javascript-sdk/src/extensions/scroll-depth.ts
  • Implemented ScrollDepth class for tracking scroll events.
  • Added methods to calculate and send scroll depth data.
  • Introduced debouncing for scroll event handling.
  • +64/-101
    common.ts
    Add utility functions for common operations                           

    packages/javascript-sdk/src/utils/common.ts
  • Added utility functions for common operations.
  • Implemented functions for deep copying and event registration.
  • Introduced functions for window availability checks.
  • +55/-9   
    cookie.ts
    Implement CookieManager for cookie operations                       

    packages/javascript-sdk/src/utils/cookie.ts
  • Implemented CookieManager class for cookie operations.
  • Added methods for setting, getting, and deleting cookies.
  • Introduced logic for domain extraction and cookie management.
  • +107/-0 
    queue.ts
    Implement RetryQueue for event retry management                   

    packages/javascript-sdk/src/utils/queue.ts
  • Implemented RetryQueue class for managing event retries.
  • Added logic for batch processing and network status handling.
  • Introduced persistence for offline queue management.
  • +123/-0 
    autocapture-utils.ts
    Add utility functions for autocapture operations                 

    packages/javascript-sdk/src/utils/autocapture-utils.ts
  • Added utility functions for autocapture operations.
  • Implemented functions to determine element capture eligibility.
  • Introduced logic for handling sensitive elements.
  • +29/-19 
    usePageView.ts
    Implement usePageView hook for page view tracking               

    packages/react/src/usePageView.ts
  • Implemented usePageView hook for tracking page views.
  • Added logic to handle URL changes and trigger tracking.
  • Introduced callback options for additional tracking parameters.
  • +72/-18 
    usePageView.ts
    Implement usePageView hook for Next.js applications           

    packages/nextjs/src/usePageView.ts
  • Implemented usePageView hook for Next.js applications.
  • Added logic to track page views on URL changes.
  • Introduced options for customizing tracking behavior.
  • +66/-21 
    useUsermaven.ts
    Implement useUsermaven hook for Usermaven client access   

    packages/nextjs/src/useUsermaven.ts
  • Implemented useUsermaven hook for accessing Usermaven client.
  • Added methods for tracking events and managing user properties.
  • Introduced error handling for uninitialized client context.
  • +33/-12 
    page.tsx
    Create basic Next.js page component                                           

    examples/next-app/src/app/page.tsx
  • Created a basic Next.js page component.
  • Added layout and styling for the home page.
  • Included links and images for navigation and branding.
  • +94/-0   
    rage-click.ts
    Implement RageClick class for detecting rage clicks           

    packages/javascript-sdk/src/extensions/rage-click.ts
  • Implemented RageClick class for detecting rage clicks.
  • Added logic to track rapid clicks within a time window.
  • Introduced event dispatching for detected rage clicks.
  • +74/-0   
    helpers.ts
    Add helper functions for common tasks                                       

    packages/javascript-sdk/src/utils/helpers.ts
  • Added helper functions for common tasks.
  • Implemented functions for ID generation and email validation.
  • Introduced debouncing utility for function calls.
  • +70/-0   
    xhr.ts
    Implement XhrTransport for sending events via XHR               

    packages/javascript-sdk/src/transport/xhr.ts
  • Implemented XhrTransport class for sending events via XHR.
  • Added methods for constructing URLs and handling responses.
  • Introduced logic for setting custom headers.
  • +71/-0   
    https.ts
    Implement HttpsTransport for server-side event sending     

    packages/javascript-sdk/src/transport/https.ts
  • Implemented HttpsTransport class for server-side event sending.
  • Added methods for constructing HTTPS requests and handling responses.
  • Introduced support for custom headers in requests.
  • +71/-0   
    config.ts
    Define Config interface and default settings                         

    packages/javascript-sdk/src/core/config.ts
  • Defined Config interface for client configuration.
  • Added default configuration values for various settings.
  • Introduced policies for privacy and cookie handling.
  • +67/-0   
    fetch.ts
    Implement FetchTransport for sending events via Fetch API

    packages/javascript-sdk/src/transport/fetch.ts
  • Implemented FetchTransport class for sending events via Fetch API.
  • Added methods for constructing request URLs and handling responses.
  • Introduced logic for setting custom headers.
  • +64/-0   
    beacon.ts
    Implement BeaconTransport for sending events via Beacon API

    packages/javascript-sdk/src/transport/beacon.ts
  • Implemented BeaconTransport class for sending events via Beacon API.
  • Added methods for constructing URLs and handling payloads.
  • Introduced limitations due to Beacon API constraints.
  • +42/-0   
    Tests
    3 files
    event-tracking.test.ts
    Add unit tests for UsermavenClient event tracking               

    packages/javascript-sdk/test/unit/core/event-tracking.test.ts
  • Added unit tests for UsermavenClient event tracking methods.
  • Tested various event types and payload handling.
  • Included tests for user and group identification.
  • +238/-0 
    client.test.ts
    Add unit tests for UsermavenClient methods                             

    packages/javascript-sdk/test/unit/core/client.test.ts
  • Added unit tests for UsermavenClient methods.
  • Tested event tracking, user identification, and reset functionality.
  • Included error handling tests for invalid inputs.
  • +165/-0 
    server-side-client.test.ts
    Add unit tests for server-side UsermavenClient                     

    packages/javascript-sdk/test/unit/core/server-side-client.test.ts
  • Added unit tests for server-side UsermavenClient.
  • Tested initialization and event tracking without browser features.
  • Included tests for user and group identification on server-side.
  • +106/-0 
    Configuration changes
    1 files
    vite.config.ts
    Configure Vite for React application                                         

    examples/react-app/vite.config.ts
  • Configured Vite for React application.
  • Added React plugin for Vite.
  • Set up basic Vite configuration.
  • +7/-0     
    Additional files (token-limit)
    62 files
    pageviews.ts
    ...                                                                                                           

    packages/javascript-sdk/src/tracking/pageviews.ts ...
    +57/-0   
    vite.config.ts
    ...                                                                                                           

    packages/javascript-sdk/vite.config.ts ...
    +52/-0   
    local-storage.ts
    ...                                                                                                           

    packages/javascript-sdk/src/persistence/local-storage.ts ...
    +58/-0   
    App.tsx
    ...                                                                                                           

    examples/react-app/src/App.tsx ...
    +44/-0   
    types.ts
    ...                                                                                                           

    packages/javascript-sdk/src/core/types.ts ...
    +48/-0   
    UsermavenProvider.tsx
    ...                                                                                                           

    packages/react/src/UsermavenProvider.tsx ...
    +14/-10 
    test.ts
    ...                                                                                                           

    packages/javascript-sdk/examples/test.ts ...
    +30/-0   
    logger.ts
    ...                                                                                                           

    packages/javascript-sdk/src/utils/logger.ts ...
    +38/-0   
    layout.tsx
    ...                                                                                                           

    examples/next-app/src/app/layout.tsx ...
    +37/-0   
    main.tsx
    ...                                                                                                           

    examples/react-app/src/main.tsx ...
    +28/-0   
    LayoutWrapper.tsx
    ...                                                                                                           

    examples/next-app/src/component/client/LayoutWrapper.tsx ...
    +24/-0   
    setup.ts
    ...                                                                                                           

    packages/javascript-sdk/test/setup.ts ...
    +27/-0   
    UsermavenProvider.tsx
    ...                                                                                                           

    packages/nextjs/src/UsermavenProvider.tsx ...
    +3/-3     
    vitest.config.ts
    ...                                                                                                           

    packages/javascript-sdk/vitest.config.ts ...
    +20/-0   
    middlewareEnv.ts
    ...                                                                                                           

    packages/nextjs/src/middlewareEnv.ts ...
    +2/-2     
    memory.ts
    ...                                                                                                           

    packages/javascript-sdk/src/persistence/memory.ts ...
    +25/-0   
    vitest.d.ts
    ...                                                                                                           

    packages/javascript-sdk/test/vitest.d.ts ...
    +20/-0   
    global.d.ts
    ...                                                                                                           

    packages/javascript-sdk/src/global.d.ts ...
    +7/-0     
    transport.ts
    ...                                                                                                           

    packages/javascript-sdk/src/transport/transport.ts ...
    +3/-0     
    vite-env.d.ts
    ...                                                                                                           

    examples/react-app/src/vite-env.d.ts ...
    +1/-0     
    form-tracking.html
    ...                                                                                                           

    packages/javascript-sdk/examples/form-tracking.html ...
    +110/-0 
    index.html
    ...                                                                                                           

    packages/javascript-sdk/examples/index.html ...
    +45/-0   
    index.html
    ...                                                                                                           

    examples/react-app/index.html ...
    +13/-0   
    mergeFiles.js
    ...                                                                                                           

    mergeFiles.js ...
    +156/-0 
    mockServer.js
    ...                                                                                                           

    packages/javascript-sdk/server/mockServer.js ...
    +119/-0 
    copyContents.js
    ...                                                                                                           

    scripts/copyContents.js ...
    +93/-0   
    eslint.config.js
    ...                                                                                                           

    examples/react-app/eslint.config.js ...
    +28/-0   
    page.module.css
    ...                                                                                                           

    examples/next-app/src/app/page.module.css ...
    +165/-0 
    index.css
    ...                                                                                                           

    examples/react-app/src/index.css ...
    +63/-8   
    App.css
    ...                                                                                                           

    examples/react-app/src/App.css ...
    +42/-0   
    globals.css
    ...                                                                                                           

    examples/next-app/src/app/globals.css ...
    +42/-0   
    pnpm-lock.yaml
    ...                                                                                                           

    pnpm-lock.yaml ...
    +5173/-1
    README.md
    ...                                                                                                           

    packages/javascript-sdk/README.md ...
    +149/-36
    README.md
    ...                                                                                                           

    examples/react-app/README.md ...
    +50/-70 
    cd-master.yml
    ...                                                                                                           

    .github/workflows/cd-master.yml ...
    +141/-0 
    package.json
    ...                                                                                                           

    packages/javascript-sdk/package.json ...
    +45/-47 
    README.md
    ...                                                                                                           

    README.md ...
    +163/-3 
    cd-develop.yml
    ...                                                                                                           

    .github/workflows/cd-develop.yml ...
    +122/-0 
    package.json
    ...                                                                                                           

    examples/react-app/package.json ...
    +23/-34 
    ci.yml
    ...                                                                                                           

    .github/workflows/ci.yml ...
    +62/-0   
    tsconfig.json
    ...                                                                                                           

    packages/javascript-sdk/tsconfig.json ...
    +35/-12 
    README.md
    ...                                                                                                           

    examples/next-app/README.md ...
    +36/-0   
    package.json
    ...                                                                                                           

    package.json ...
    +12/-11 
    README.md
    ...                                                                                                           

    packages/nextjs/README.md ...
    +4/-4     
    Dockerfile
    ...                                                                                                           

    packages/javascript-sdk/docker/Dockerfile ...
    +14/-6   
    tsconfig.json
    ...                                                                                                           

    examples/next-app/tsconfig.json ...
    +26/-0   
    tsconfig.app.json
    ...                                                                                                           

    examples/react-app/tsconfig.app.json ...
    +24/-0   
    package.json
    ...                                                                                                           

    examples/next-app/package.json ...
    +23/-0   
    tsconfig.node.json
    ...                                                                                                           

    examples/react-app/tsconfig.node.json ...
    +22/-0   
    README.md
    ...                                                                                                           

    packages/react/README.md ...
    +2/-2     
    master-release.yml
    ...                                                                                                           

    .github/workflows/master-release.yml ...
    +26/-0   
    package.json
    ...                                                                                                           

    packages/react/package.json ...
    +2/-2     
    package.json
    ...                                                                                                           

    packages/nextjs/package.json ...
    +2/-2     
    .eslintrc.json
    ...                                                                                                           

    packages/javascript-sdk/.eslintrc.json ...
    +19/-0   
    docker-compose.yaml
    ...                                                                                                           

    docker-compose.yaml ...
    +1/-1     
    .prettierrc
    ...                                                                                                           

    packages/javascript-sdk/.prettierrc ...
    +7/-0     
    tsconfig.json
    ...                                                                                                           

    examples/react-app/tsconfig.json ...
    +7/-0     
    next.config.mjs
    ...                                                                                                           

    examples/next-app/next.config.mjs ...
    +4/-0     
    .dockerignore
    ...                                                                                                           

    packages/javascript-sdk/.dockerignore ...
    +1/-1     
    pnpm-workspace.yaml
    ...                                                                                                           

    pnpm-workspace.yaml ...
    +3/-0     
    sonar-project.properties
    ...                                                                                                           

    sonar-project.properties ...
    +3/-0     
    .dockerignore
    ...                                                                                                           

    .dockerignore ...
    +2/-0     

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

    netlify[bot] commented 2 weeks ago

    Deploy Preview for usermaven-tracker failed.

    Name Link
    Latest commit c26f550a12ec245c9836aaae7cdea4908416cf28
    Latest deploy log https://app.netlify.com/sites/usermaven-tracker/deploys/670fb3be6cdc0f0008f1ab8b
    github-actions[bot] commented 2 weeks ago

    PR Reviewer Guide ๐Ÿ”

    Here are some key observations to aid the review process:

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

    Performance Issue
    The `UsermavenClient` class initializes multiple features and components, which might not be necessary for all use cases. This could lead to unnecessary memory usage and slower initialization times, especially in environments where resources are limited or these features are not required. Code Smell
    The `mergeConfig` method uses recursion to merge configurations, which can lead to performance issues and stack overflow errors if the configuration object is deeply nested. Possible Bug
    The `track` method in `UsermavenClient` class directly modifies the state of `retryQueue` without ensuring thread safety, which might lead to race conditions in a multi-threaded environment like Node.js.
    Code feedback:
    relevant filepackages/javascript-sdk/src/core/client.ts
    suggestion       Consider implementing feature flags or a configuration option to selectively initialize components based on the environment or user preferences. This can help in optimizing resource usage and improving the initialization time of the `UsermavenClient` class. [important]
    relevant lineexport class UsermavenClient {

    relevant filepackages/javascript-sdk/src/core/client.ts
    suggestion       Refactor the `mergeConfig` method to use iterative approaches instead of recursion to avoid performance overhead and potential stack overflow issues. Utilizing iterative methods to merge configurations can be more efficient and less prone to errors in cases of deeply nested objects. [important]
    relevant linemergeConfig(config: Partial, defaultConfig: Partial): Config {

    relevant filepackages/javascript-sdk/src/core/client.ts
    suggestion       To avoid potential race conditions in the `track` method, consider using locks or other synchronization mechanisms when accessing and modifying shared resources like `retryQueue`. This will ensure that the state of `retryQueue` remains consistent even in a multi-threaded environment. [important]
    relevant linepublic track(typeName: string, payload?: EventPayload, directSend: boolean = false): void {

    github-actions[bot] commented 2 weeks ago

    PR Code Suggestions โœจ

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Ensure all used functions are properly imported or defined to avoid runtime errors ___ **Ensure that the isElementNode function is imported or defined in the current or a
    globally accessible scope, as it is used to check if el is an element node but is
    not defined or imported in the visible code.** [packages/javascript-sdk/src/utils/autocapture-utils.ts [171-172]](https://github.com/usermaven/usermaven-js/pull/120/files#diff-3421105f040b57611a61997529e3c66ca2a0ee572e2ff04008bd6bea93aa71a3R171-R172) ```diff +import { isElementNode } from './dom-utils'; +... if (!el || !isElementNode(el)) { return false; } ```
    Suggestion importance[1-10]: 9 Why: The suggestion addresses a critical issue by ensuring that the `isElementNode` function is imported, preventing potential runtime errors due to undefined function usage.
    9
    Add error handling for JSON parsing to enhance robustness ___ **Add error handling for JSON parsing to prevent runtime errors from malformed JSON
    strings.** [packages/javascript-sdk/src/index.ts [10]](https://github.com/usermaven/usermaven-js/pull/120/files#diff-02046ed2ea7c73d60e8f714d301b8e29ef6141c1b53d9f8504cd73f187a55050R10-R10) ```diff -const cleanConfig = JSON.parse(JSON.stringify(config)); +let cleanConfig; +try { + cleanConfig = JSON.parse(JSON.stringify(config)); +} catch (error) { + console.error('Failed to parse config:', error); + cleanConfig = {}; +} ```
    Suggestion importance[1-10]: 7 Why: Adding error handling for JSON parsing prevents runtime errors from malformed JSON strings, improving the robustness and reliability of the code.
    7
    Enhancement
    Ensure robust integer parsing for configuration timeouts and attempts ___ **Use a more robust method for parsing integers to ensure that minSendTimeout,
    maxSendTimeout, and maxSendAttempts are always valid integers or undefined.** [packages/javascript-sdk/src/index.ts [44-46]](https://github.com/usermaven/usermaven-js/pull/120/files#diff-02046ed2ea7c73d60e8f714d301b8e29ef6141c1b53d9f8504cd73f187a55050R44-R46) ```diff -minSendTimeout: parseInt(script.getAttribute('data-min-send-timeout') || '', 10) || undefined, -maxSendTimeout: parseInt(script.getAttribute('data-max-send-timeout') || '', 10) || undefined, -maxSendAttempts: parseInt(script.getAttribute('data-max-send-attempts') || '', 10) || undefined, +minSendTimeout: Number.isInteger(parseInt(script.getAttribute('data-min-send-timeout'), 10)) ? parseInt(script.getAttribute('data-min-send-timeout'), 10) : undefined, +maxSendTimeout: Number.isInteger(parseInt(script.getAttribute('data-max-send-timeout'), 10)) ? parseInt(script.getAttribute('data-max-send-timeout'), 10) : undefined, +maxSendAttempts: Number.isInteger(parseInt(script.getAttribute('data-max-send-attempts'), 10)) ? parseInt(script.getAttribute('data-max-send-attempts'), 10) : undefined, ```
    Suggestion importance[1-10]: 8 Why: The suggestion improves the reliability of parsing integer values by ensuring that only valid integers are assigned, reducing potential bugs from invalid configurations.
    8
    Improve the cloning process to handle deep object structures safely ___ **Ensure that the config object is deeply cloned to avoid unintended side effects when
    modifying nested properties.** [packages/javascript-sdk/src/index.ts [10]](https://github.com/usermaven/usermaven-js/pull/120/files#diff-02046ed2ea7c73d60e8f714d301b8e29ef6141c1b53d9f8504cd73f187a55050R10-R10) ```diff -const cleanConfig = JSON.parse(JSON.stringify(config)); +const cleanConfig = _.cloneDeep(config); ```
    Suggestion importance[1-10]: 7 Why: Using a deep clone method like `_.cloneDeep` ensures that nested objects are cloned properly, preventing unintended side effects. This enhances the robustness of the code when dealing with complex configurations.
    7
    Simplify attribute checks by removing unnecessary type checks ___ **Replace the typeof el.hasAttribute === 'function' check with a direct invocation
    since el is already confirmed to be an Element, which should inherently have the
    hasAttribute method.** [packages/javascript-sdk/src/utils/autocapture-utils.ts [176-182]](https://github.com/usermaven/usermaven-js/pull/120/files#diff-3421105f040b57611a61997529e3c66ca2a0ee572e2ff04008bd6bea93aa71a3R176-R182) ```diff -if (typeof el.hasAttribute === 'function') { - if (el.hasAttribute(AutoCapture.FORCE_CAPTURE_ATTR)) { - return true; - } - if (el.hasAttribute(AutoCapture.PREVENT_CAPTURE_ATTR)) { - return false; - } +if (el.hasAttribute(AutoCapture.FORCE_CAPTURE_ATTR)) { + return true; +} +if (el.hasAttribute(AutoCapture.PREVENT_CAPTURE_ATTR)) { + return false; } ```
    Suggestion importance[1-10]: 7 Why: The suggestion improves code readability and efficiency by removing redundant type checks, assuming `el` is confirmed to be an Element.
    7
    Improve React list rendering by adding unique keys to children ___ **Add a key attribute to each child in the list to improve React rendering performance
    and avoid potential issues in DOM reconciliation.** [examples/next-app/src/app/layout.tsx [31-33]](https://github.com/usermaven/usermaven-js/pull/120/files#diff-11f17983113855f424000e9b0d58f859395eb340ff1ab567a144e3a3b17b7c6cR31-R33) ```diff - {children} + {React.Children.map(children, (child, index) => React.cloneElement(child, { key: index }))} ```
    Suggestion importance[1-10]: 6 Why: Adding keys to children in React helps with efficient DOM reconciliation and rendering. Although the current code may not exhibit issues, this enhancement aligns with React best practices for handling lists of elements.
    6
    Performance
    Optimize class checks using classList.contains for better performance and readability ___ **Consider using classList.contains for checking class names instead of splitting
    className into an array, which can be more performant and concise.** [packages/javascript-sdk/src/utils/autocapture-utils.ts [185-191]](https://github.com/usermaven/usermaven-js/pull/120/files#diff-3421105f040b57611a61997529e3c66ca2a0ee572e2ff04008bd6bea93aa71a3R185-R191) ```diff let curEl: Element | null = el; while (curEl && curEl.parentElement && !isTag(curEl, 'body')) { - const classes = getClassName(curEl).split(' '); - if (_includes(classes, 'ph-sensitive') || _includes(classes, 'ph-no-capture')) { + if (curEl.classList.contains('ph-sensitive') || curEl.classList.contains('ph-no-capture')) { return false; } curEl = curEl.parentElement; } ```
    Suggestion importance[1-10]: 8 Why: This suggestion enhances performance and readability by using `classList.contains`, which is more efficient than splitting class names into an array.
    8
    Possible issue
    Ensure font files are correctly served by adjusting the path to be absolute ___ **Ensure that the font files are correctly served from the public directory to avoid
    loading issues.** [examples/next-app/src/app/layout.tsx [7-11]](https://github.com/usermaven/usermaven-js/pull/120/files#diff-11f17983113855f424000e9b0d58f859395eb340ff1ab567a144e3a3b17b7c6cR7-R11) ```diff const geistSans = localFont({ - src: "./fonts/GeistVF.woff", + src: "/fonts/GeistVF.woff", variable: "--font-geist-sans", weight: "100 900", }); ```
    Suggestion importance[1-10]: 7 Why: Changing the font path to an absolute path ensures that the font files are correctly served from the public directory, which can prevent loading issues. This is a practical suggestion that addresses a potential problem with resource loading.
    7
    Maintainability
    Refactor repetitive attribute checks into a loop for cleaner and more maintainable code ___ **Replace the manual attribute checks with a loop to simplify the code and improve
    maintainability.** [packages/javascript-sdk/src/index.ts [31-39]](https://github.com/usermaven/usermaven-js/pull/120/files#diff-02046ed2ea7c73d60e8f714d301b8e29ef6141c1b53d9f8504cd73f187a55050R31-R39) ```diff -autocapture: script.getAttribute('data-autocapture') === 'true', -formTracking: script.getAttribute('data-form-tracking') === 'true' ? 'all' : false, -autoPageview: script.getAttribute('data-auto-pageview') === 'true', -useBeaconApi: script.getAttribute('data-use-beacon-api') === 'true', -forceUseFetch: script.getAttribute('data-force-use-fetch') === 'true', -gaHook: script.getAttribute('data-ga-hook') === 'true', -segmentHook: script.getAttribute('data-segment-hook') === 'true', -randomizeUrl: script.getAttribute('data-randomize-url') === 'true', -capture3rdPartyCookies: script.getAttribute('data-capture-3rd-party-cookies') === 'false' ? false : undefined, +const booleanAttributes = { + autocapture: 'data-autocapture', + formTracking: 'data-form-tracking', + autoPageview: 'data-auto-pageview', + useBeaconApi: 'data-use-beacon-api', + forceUseFetch: 'data-force-use-fetch', + gaHook: 'data-ga-hook', + segmentHook: 'data-segment-hook', + randomizeUrl: 'data-randomize-url', + capture3rdPartyCookies: 'data-capture-3rd-party-cookies' +}; +Object.keys(booleanAttributes).forEach(key => { + const attr = script.getAttribute(booleanAttributes[key]); + config[key] = attr === 'true' || (key === 'formTracking' && attr === 'true' ? 'all' : false); +}); ```
    Suggestion importance[1-10]: 6 Why: This refactoring enhances code maintainability by reducing repetition and making the code easier to update and understand, though it doesn't directly affect functionality.
    6
    Improve code organization and reusability by extracting regex checks into a utility function ___ **Refactor the sensitive name regex check to a separate utility function for better
    modularity and reusability.** [packages/javascript-sdk/src/utils/autocapture-utils.ts [209-213]](https://github.com/usermaven/usermaven-js/pull/120/files#diff-3421105f040b57611a61997529e3c66ca2a0ee572e2ff04008bd6bea93aa71a3R209-R213) ```diff const name = (el as HTMLInputElement).name || el.id || ''; -if (typeof name === 'string') { +if (isSensitiveName(name)) { + return false; +} +... +function isSensitiveName(name: string): boolean { const sensitiveNameRegex = /^cc|cardnum|ccnum|creditcard|csc|cvc|cvv|exp|pass|pwd|routing|seccode|securitycode|securitynum|socialsec|socsec|ssn/i; - if (sensitiveNameRegex.test(name.replace(/[^a-zA-Z0-9]/g, ''))) { - return false; - } + return sensitiveNameRegex.test(name.replace(/[^a-zA-Z0-9]/g, '')); } ```
    Suggestion importance[1-10]: 6 Why: The suggestion promotes better code organization and reusability by refactoring the regex check into a separate utility function, although it does not address a critical issue.
    6
    Best practice
    Prevent potential memory leaks by adding cleanup logic to useEffect ___ **Use a cleanup function in useEffect to avoid potential memory leaks or unintended
    behavior when the component unmounts.** [examples/react-app/src/App.tsx [11-13]](https://github.com/usermaven/usermaven-js/pull/120/files#diff-cd61ef28fd5f5a4c0f4337eada1961a24ff1187484b001c36e07575aeef0e234R11-R13) ```diff useEffect(() => { console.log('Count changed:', count); + return () => { + // Cleanup logic here + }; }, [count]); ```
    Suggestion importance[1-10]: 5 Why: While the current `useEffect` does not require cleanup, adding a cleanup function is a good practice to prevent potential memory leaks or unintended behavior in more complex scenarios. This suggestion is a proactive measure for better code maintenance.
    5