Amplitude-TypeScript icon indicating copy to clipboard operation
Amplitude-TypeScript copied to clipboard

fix(uuid): update uuid.ts based on JS implementation

Open visiodeibc opened this issue 1 year ago • 1 comments

Summary

Hi, this is Jake from the Technical Support team.

There was an issue of user properties being mapped incorrectly due to colliding UUIDs set by the Amplitude TS SDK[due to math.random function].

There was a fix in JS SDK but not carried over to TS SDK

  • https://github.com/amplitude/Amplitude-JavaScript/blob/v8.x/src/uuid.js

The issue has been stale, so I am making PR for the issue!

Checklist

  • [v] Does your PR title have the correct title format?
  • Does your PR have a breaking change?: no

visiodeibc avatar Aug 14 '24 01:08 visiodeibc

Hi @visiodeibc, TS’s analytics-core package is shared by both browser (analytics-browser package) and node (analytics-node package) environments in which crypto should be imported differently. It's predefined in browser environment but it requires to be imported in node environment.

Mercy811 avatar Aug 15 '24 23:08 Mercy811

I've committed a change to this.

NodeJS and Web both export "crypto" to their global scope (global.crypto and window.crypto, respectively) so it doesn't need to imported in NodeJS, it can just be executed from the global scope.

Screenshot 2025-04-09 at 5 12 28 PM

I added a change that if "global.crypto.getRandomValues" isn't there, then fall back to the legacy UUID.

daniel-graham-amplitude avatar Apr 10 '25 00:04 daniel-graham-amplitude

@Mercy811 turns out you don't need to import "crypto" in a NodeJS environment. Crypto is exposed on the global scope (global.crypto or globalThis.crypto). Similar to how setTimeout, setInterval, etc... don't need to be imported as an NPM module.

Screenshot 2025-04-09 at 5 12 28 PM

daniel-graham-amplitude avatar Apr 10 '25 17:04 daniel-graham-amplitude

@Mercy811 @daniel-graham-amplitude thank you so much for the fix! is this available in the latest SDK version?!

visiodeibc avatar May 06 '25 06:05 visiodeibc

@visiodeibc it is yeah, it's been out for a few weeks. Thanks for the contribution!

daniel-graham-amplitude avatar May 06 '25 15:05 daniel-graham-amplitude