Skip to content

Use HubSpot postMessage API for form submission tracking#17

Merged
joalves merged 4 commits into
mainfrom
feat/hubspot-message-api
Apr 14, 2026
Merged

Use HubSpot postMessage API for form submission tracking#17
joalves merged 4 commits into
mainfrom
feat/hubspot-message-api

Conversation

@joalves
Copy link
Copy Markdown
Collaborator

@joalves joalves commented Apr 14, 2026

Summary

  • Listen for hsFormCallback messages instead of submit events
  • Extract formGuid and conversionId from the message data
  • Derive form ID from data-form-id attr, hs_form_id hidden input, or element id
  • Key form state by form ID string instead of DOM element reference
  • Include form_id in all emitted events
  • Clean up message listener on destroy

Test plan

  • CI passes (lint, tests, coverage)

Summary by CodeRabbit

Chores

  • Configured automated code format validation to run before each code push

Tests

  • Significantly expanded HubSpot form tracker test coverage with comprehensive robustness tests for message handling, form lifecycle events, and various error scenarios

Refactor

  • Enhanced HubSpot form tracker with improved form identification accuracy, more robust event emission and handling, and refined form submission tracking

- Listen for hsFormCallback messages instead of submit events, which
  HubSpot forms fire via postMessage after successful submission
- Extract formGuid and conversionId from the message data
- Derive form ID from data-form-id attr, hs_form_id hidden input, or
  form element id
- Key form state by form ID string instead of DOM element reference
- Include form_id in all emitted events
- Clean up message listener on destroy
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 14, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 05267650-ba2c-4cf1-bbed-4e2a3fda64c9

📥 Commits

Reviewing files that changed from the base of the PR and between cba1625 and a7a11fd.

📒 Files selected for processing (4)
  • .githooks/pre-push
  • package.json
  • src/__tests__/presets/hubspot.test.ts
  • src/presets/hubspot.ts

Walkthrough

The pull request introduces git hook infrastructure and substantially refactors the HubSpot form tracking implementation. A new pre-push hook script runs format checks, configured via a prepare npm script. The HubSpot tracker implementation is redesigned to identify forms by ID string rather than DOM elements, introduces a global window message listener to handle HubSpot postMessage events, and augments emitted events with form_id fields. Test coverage is significantly expanded with comprehensive validation of form lifecycle events, message handler edge cases, abandonment timeout clearing, and payload robustness.

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Poem

🐰 Hooks are hopping, tests expand wide,
Forms now tracked by ID with pride!
Messages flow through the window's stream,
Format checks fulfil quality dreams! ✨

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/hubspot-message-api

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ast-grep (0.42.1)
src/__tests__/presets/hubspot.test.ts

[
{
"text": "window.postMessage(\n { type: "hsFormCallback", id: formId, eventName, data: data || {} },\n "",\n )",
"range": {
"byteOffset": {
"start": 958,
"end": 1063
},
"start": {
"line": 31,
"column": 2
},
"end": {
"line": 34,
"column": 3
}
},
"file": "src/tests/presets/hubspot.test.ts",
"lines": " window.postMessage(\n { type: "hsFormCallback", id: formId, eventName, data: data || {} },\n "
",\n );",
"charCount": {
"leading": 2,
"trailing": 1
},
"language": "TypeScript",
"ruleId": "message-origin-validation",
"severity": "warning",
"note": "[CWE-923] Improper Restriction of Communication Channel to Intended Endpoints [REFERENCES]\n - https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage\n - https://cwe.mitre.org/data/definitions/923.html",
"message": "Detected usage of wildcard '*' as origin in postMessage. Always specify an exact target origi

... [truncated 43018 characters] ...

      "end": 10836
    },
    "start": {
      "line": 346,
      "column": 6
    },
    "end": {
      "line": 346,
      "column": 9
    }
  },
  "style": "secondary"
},
{
  "text": "(\n      {\n        type: \"hsFormCallback\",\n        id: \"no-data-form\",\n        eventName: \"onFormSubmitted\",\n      },\n      \"*\",\n    )",
  "range": {
    "byteOffset": {
      "start": 10710,
      "end": 10843
    },
    "start": {
      "line": 340,
      "column": 22
    },
    "end": {
      "line": 347,
      "column": 5
    }
  },
  "style": "secondary"
}

]
}
]
Error: 6 error(s) found in code.
Help: Scan succeeded and found error level diagnostics in the codebase.


Comment @coderabbitai help to get the list of available commands and usage tips.

@joalves joalves merged commit 7bca800 into main Apr 14, 2026
1 check 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.

1 participant