Skip to content

Commit

Permalink
Merge pull request #729 from microsoft/almega/send-url-all-payloads
Browse files Browse the repository at this point in the history
Send url with all payloads
  • Loading branch information
alaa-megahed authored Jan 9, 2025
2 parents 35654d2 + 20e4c61 commit 03859c3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
28 changes: 18 additions & 10 deletions packages/clarity-js/src/core/scrub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Layout from "@clarity-types/layout";
import config from "@src/core/config";

const catchallRegex = /\S/gi;
const maxUrlLength = 255;
let unicodeRegex = true;
let digitRegex = null;
let letterRegex = null;
Expand Down Expand Up @@ -87,17 +88,24 @@ export function text(value: string, hint: string, privacy: Privacy, mangle: bool
return value;
}

export function url(input: string, electron: boolean = false): string {
export function url(input: string, electron: boolean = false, truncate: boolean = false): string {
let result = input;
// Replace the URL for Electron apps so we don't send back file:/// URL
if (electron) { return `${Data.Constant.HTTPS}${Data.Constant.Electron}`; }
if (electron) {
result = `${Data.Constant.HTTPS}${Data.Constant.Electron}`;
} else {
let drop = config.drop;
if (drop && drop.length > 0 && input && input.indexOf("?") > 0) {
let [path, query] = input.split("?");
let swap = Data.Constant.Dropped;
result = path + "?" + query.split("&").map(p => drop.some(x => p.indexOf(`${x}=`) === 0) ? `${p.split("=")[0]}=${swap}` : p).join("&");
}
}

let drop = config.drop;
if (drop && drop.length > 0 && input && input.indexOf("?") > 0) {
let [path, query] = input.split("?");
let swap = Data.Constant.Dropped;
return path + "?" + query.split("&").map(p => drop.some(x => p.indexOf(`${x}=`) === 0) ? `${p.split("=")[0]}=${swap}` : p).join("&");
if (truncate) {
result = result.substring(0, maxUrlLength);
}
return input;
return result;
}

function mangleText(value: string): string {
Expand All @@ -111,7 +119,7 @@ function mangleText(value: string): string {
}
return value;
}

function mask(value: string): string {
return value.replace(catchallRegex, Data.Constant.Mask);
}
Expand Down Expand Up @@ -149,7 +157,7 @@ function redact(value: string): string {
let hasEmail = false;
let hasWhitespace = false;
let array = null;

regex(); // Initialize regular expressions

for (let i = 0; i < value.length; i++) {
Expand Down
8 changes: 6 additions & 2 deletions packages/clarity-js/src/data/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BooleanFlag, Token, Upload, Envelope } from "@clarity-types/data";
import { time } from "@src/core/time";
import version from "@src/core/version";
import * as metadata from "@src/data/metadata";
import * as scrub from "@src/core/scrub";

export let data: Envelope = null;

Expand All @@ -17,7 +18,8 @@ export function start(): void {
sessionId: m.sessionId,
pageNum: m.pageNum,
upload: Upload.Async,
end: BooleanFlag.False
end: BooleanFlag.False,
url: ''
};
}

Expand All @@ -31,6 +33,7 @@ export function envelope(last: boolean): Token[] {
data.sequence++;
data.upload = last && "sendBeacon" in navigator ? Upload.Beacon : Upload.Async;
data.end = last ? BooleanFlag.True : BooleanFlag.False;
data.url = scrub.url(location.href, false, true);
return [
data.version,
data.sequence,
Expand All @@ -41,6 +44,7 @@ export function envelope(last: boolean): Token[] {
data.sessionId,
data.pageNum,
data.upload,
data.end
data.end,
data.url
];
}
4 changes: 2 additions & 2 deletions packages/clarity-js/src/data/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function queue(tokens: Token[], transmit: boolean = true): void {
case Event.Discover:
discoverBytes += event.length;
case Event.Box:
case Event.Mutation:
case Event.Mutation:
case Event.Snapshot:
case Event.StyleSheetAdoption:
case Event.StyleSheetUpdate:
Expand Down Expand Up @@ -241,7 +241,7 @@ function check(xhr: XMLHttpRequest, sequence: number): void {

function done(sequence: number): void {
// If we everything went successfully, and it is the first sequence, save this session for future reference
if (sequence === 1) {
if (sequence === 1) {
metadata.save();
metadata.callback();
}
Expand Down
3 changes: 2 additions & 1 deletion packages/clarity-js/types/data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ export interface Envelope extends Metadata {
version: string;
upload: Upload;
end: BooleanFlag;
url: string;
}

export interface Transit {
Expand Down Expand Up @@ -486,4 +487,4 @@ export interface PerformanceEventTiming extends PerformanceEntry {
export interface Interaction {
id: number;
latency: number;
}
}

0 comments on commit 03859c3

Please sign in to comment.