Skip to content

refactor: convert events module to TS#1258

Open
jaissica12 wants to merge 5 commits into
developmentfrom
refactor/SDKE-1106-migrate-event-modules-to-TS
Open

refactor: convert events module to TS#1258
jaissica12 wants to merge 5 commits into
developmentfrom
refactor/SDKE-1106-migrate-event-modules-to-TS

Conversation

@jaissica12
Copy link
Copy Markdown
Contributor

@jaissica12 jaissica12 commented May 12, 2026

Background

  • As part of the ongoing effort to migrate the mParticle Web SDK from JavaScript to TypeScript, this PR converts the events module and its corresponding test file to TypeScript for improved type safety and developer experience.

What Has Changed

  • Converted src/events.js to src/events.ts with full type annotations on all method parameters, return types, and internal variables
  • Converted test/src/tests-event-logging.js to test/src/tests-event-logging.ts with proper global type declarations and type casts for intentionally invalid test inputs
  • Updated src/events.interfaces.ts to expand the logImpressionEvent signature to accept SDKImpression | SDKImpression[] | SDKProductImpression | SDKProductImpression[]
  • Minor supporting type adjustments in src/sdkRuntimeModels.ts, src/serverModel.ts, and src/store.ts

Screenshots/Video

Screenshot 2026-05-13 at 7 34 08 AM Screenshot 2026-05-13 at 7 34 37 AM Screenshot 2026-05-13 at 7 35 03 AM Screenshot 2026-05-13 at 7 35 23 AM

Checklist

  • I have performed a self-review of my own code.
  • I have made corresponding changes to the documentation.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have tested this locally.

Additional Notes

Reference Issue (For employees only. Ignore if you are an outside contributor)

@jaissica12 jaissica12 changed the base branch from master to development May 12, 2026 21:07
@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot
4.3% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@jaissica12 jaissica12 marked this pull request as ready for review May 13, 2026 11:51
@jaissica12 jaissica12 requested a review from a team as a code owner May 13, 2026 11:51
@cursor
Copy link
Copy Markdown

cursor Bot commented May 13, 2026

PR Summary

Medium Risk
Refactors core event/commerce logging code to TypeScript and adjusts several public method signatures, which could introduce subtle runtime regressions around DOM handlers, location tracking, and ecommerce event shaping despite largely type-only intent.

Overview
Migrates the Events module to TypeScript with explicit typings for event logging, commerce helpers, DOM event handlers, and location tracking, including stricter error handling and attribute casting.

Expands commerce APIs to accept arrays in more places (e.g., logPromotionEvent) and broadens logImpressionEvent to support SDKImpression and lists, with minor supporting type updates in runtime models, server DTO conversion, and store config (timeout).

Converts the event logging test suite to TypeScript by adding global type declarations and using casts/String(...) coercions for intentionally invalid test inputs and mocked request bodies.

Reviewed by Cursor Bugbot for commit 14dfc68. Bugbot is set up for automated code reviews on this repo. Configure here.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 14dfc68. Configure here.

Comment thread src/events.ts
this.logProductActionEvent = function(
productActionType,
product,
customAttrs,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Replacing self with this in regular functions risks broken context

Medium Severity

The original code used var self = this to lexically capture the Events instance, guaranteeing correct binding regardless of calling context. The refactored code replaces self with this inside regular (non-arrow) function expressions like logAST, logCheckoutEvent, logProductActionEvent, logPurchaseEvent, logRefundEvent, logPromotionEvent, and logImpressionEvent. Since these are not arrow functions, this is determined at call time. If any method is ever invoked without being called as a property of the Events object (e.g., stored in a variable, passed as a callback), this will be undefined in strict mode, causing a runtime crash. The project's tsconfig.json does not enable noImplicitThis, so the compiler won't catch this.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 14dfc68. Configure here.

Comment thread src/events.ts
ProductImpressionList: imp.Name,
ProductList: Array.isArray(imp.Product)
? imp.Product
: [imp.Product],
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unsafe cast silently mishandles SDKProductImpression input data

Medium Severity

The logImpressionEvent type signature now explicitly accepts SDKProductImpression | SDKProductImpression[], but the implementation unconditionally casts each item to SDKImpression via as SDKImpression and accesses .Name and .Product. SDKProductImpression has different properties (ProductImpressionList and ProductList), so passing one would silently produce impressions with undefined names and [undefined] product lists — silent data corruption with no error.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 14dfc68. Configure here.

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