Skip to content

Commit

Permalink
Merge pull request microsoft#468 from microsoft/samart/allowEventShor…
Browse files Browse the repository at this point in the history
…thand

allowing event to be called with only value
  • Loading branch information
ender336 committed Oct 4, 2023
2 parents 1c1f354 + 8dac26d commit 1c48b8e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
23 changes: 14 additions & 9 deletions packages/clarity-js/src/data/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import encode from "./encode";

export let data: CustomData = null;

export function event(key: string, value: string): void {
// custom events allow 2 parameters or 1 parameter to be passed. If 2 are passed we
// consider it a key value pair. If only 1 is passed we only consider the event to have a value.
export function event(a: string, b: string): void {
if (core.active() &&
key &&
value &&
typeof key === Constant.String &&
typeof value === Constant.String &&
key.length < 255 &&
value.length < 255) {
data = { key, value};
encode(Event.Custom);
a &&
typeof a === Constant.String &&
a.length < 255
) {
if (b && typeof b === Constant.String && b.length < 255) {
data = { key: a, value: b};
} else {
data = { value: a }
}
encode(Event.Custom);

}
}
3 changes: 2 additions & 1 deletion packages/clarity-js/src/data/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export default function(event: Event): void {
queue(tokens, false);
break;
case Event.Custom:
tokens.push(custom.data.key);
// not all custom events have a key - if it wasn't passed server handles just value
custom.data.key && tokens.push(custom.data.key);
tokens.push(custom.data.value);
queue(tokens);
break;
Expand Down
2 changes: 1 addition & 1 deletion packages/clarity-js/types/data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ export interface VariableData {
// The way it's different from variable is that Custom Event has a notion of time
// Whereas variables have no timing element and eventually will turn into custom dimensions
export interface CustomData {
key: string;
key?: string;
value: string;
}

Expand Down

0 comments on commit 1c48b8e

Please sign in to comment.