Skip to content

Commit

Permalink
Event track improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MustafaLeithy committed Aug 5, 2024
1 parent c659fa8 commit a433e5f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
14 changes: 14 additions & 0 deletions packages/clarity-js/src/data/consent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Event, ConsentData } from "@clarity-types/data";
import * as core from "@src/core";
import encode from "@src/data/encode";

export let data: ConsentData = null;

export function consent(value: boolean): void {
if (core.active) {
data = { source: 1, value: value.toString() };

encode(Event.Consent);
}
}

6 changes: 6 additions & 0 deletions packages/clarity-js/src/data/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as summary from "@src/data/summary";
import * as upgrade from "@src/data/upgrade";
import * as variable from "@src/data/variable";
import * as extract from "@src/data/extract";
import * as consent from "@src/data/consent";
import { queue, track } from "./upload";

export default function(event: Event): void {
Expand Down Expand Up @@ -123,5 +124,10 @@ export default function(event: Event): void {

extract.reset();
queue(tokens, false);
case Event.Consent:
tokens.push(consent.data.source);
tokens.push(consent.data.value);
queue(tokens);
break;
}
}
16 changes: 5 additions & 11 deletions packages/clarity-js/src/data/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as scrub from "@src/core/scrub";
import * as dimension from "@src/data/dimension";
import * as metric from "@src/data/metric";
import { set } from "@src/data/variable";
import encode from "@src/data/encode";
import * as trackConsent from "@src/data/consent";

export let data: Metadata = null;
export let callbacks: MetadataCallbackOptions[] = [];
Expand Down Expand Up @@ -63,15 +63,7 @@ export function start(): void {
metric.max(Metric.ColorDepth, Math.round(screen.colorDepth));
}

if (config.track) {
let sync = window['sync'];
if (sync) {
sync();
}
read();
}

// Track ids using a cookie if configuration allows it
read();
track(u);
}

Expand Down Expand Up @@ -124,8 +116,10 @@ export function consent(status: boolean = true): void {
config.track = true;
track(user(), BooleanFlag.True);
save();

// Read cookie data again to mark as consented
read();
encode(Event.Consent);
trackConsent.consent(status);
}
}

Expand Down
6 changes: 0 additions & 6 deletions packages/clarity-js/src/data/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,6 @@ function response(payload: string): void {
case Constant.Signal:
if (parts.length > 1) { signalsEvent(parts[1]); }
break;
case Constant.Sync:
let sync = window['sync'];
if (sync) {
sync();
}
break;
}
}
}
6 changes: 5 additions & 1 deletion packages/clarity-js/types/data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ export const enum Constant {
Action = "ACTION",
Signal = "SIGNAL",
Extract = "EXTRACT",
Sync = "SYNC",
UserHint = "userHint",
UserType = "userType",
UserId = "userId",
Expand Down Expand Up @@ -452,6 +451,11 @@ export interface UploadData {
status: number;
}

export interface ConsentData {
source: number;
value: string;
}

export interface ClaritySignal {
type: string
value?: number
Expand Down

0 comments on commit a433e5f

Please sign in to comment.