Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve track flag #637

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions packages/clarity-js/src/data/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Time } from "@clarity-types/core";
import { BooleanFlag, Constant, Dimension, Metadata, MetadataCallback, MetadataCallbackOptions, Metric, Session, User, Setting } from "@clarity-types/data";
import { BooleanFlag, Constant, Dimension, Metadata, MetadataCallback, MetadataCallbackOptions, Metric, Session, User, Setting, Event } from "@clarity-types/data";
import * as clarity from "@src/clarity";
import * as core from "@src/core";
import config from "@src/core/config";
Expand All @@ -8,6 +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";

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

// Read cookies specified in configuration
for (let key of config.cookies) {
let value = getCookie(key);
if (value) { set(key, value); }
if (config.track) {
let sync = window['sync'];
if (sync) {
sync();
}
read();
}

// Track ids using a cookie if configuration allows it
Expand Down Expand Up @@ -120,6 +123,9 @@ export function consent(status: boolean = true): void {
if (core.active()) {
config.track = true;
track(user(), BooleanFlag.True);
save();
read();
encode(Event.Consent);
Copy link
Contributor

Choose a reason for hiding this comment

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

encode(Event.Consent);

can we please add the status to the consent event, and log in both positive cases

}
}

Expand All @@ -128,6 +134,14 @@ export function clear(): void {
setCookie(Constant.SessionKey, Constant.Empty, 0);
}

function read(): void {
// Read cookies specified in configuration
for (let key of config.cookies) {
let value = getCookie(key);
if (value) { set(key, value); }
}
}

function tab(): string {
let id = shortid();
if (config.track && supported(window, Constant.SessionStorage)) {
Expand All @@ -142,10 +156,14 @@ export function save(): void {
let ts = Math.round(Date.now());
let upload = config.upload && typeof config.upload === Constant.String ? (config.upload as string).replace(Constant.HTTPS, Constant.Empty) : Constant.Empty;
let upgrade = config.lean ? BooleanFlag.False : BooleanFlag.True;
processCallback(upgrade);
setCookie(Constant.SessionKey, [data.sessionId, ts, data.pageNum, upgrade, upload].join(Constant.Pipe), Setting.SessionExpire);
}

export function callback(): void {
let upgrade = config.lean ? BooleanFlag.False : BooleanFlag.True;
processCallback(upgrade);
}

function processCallback(upgrade: BooleanFlag) {
if (callbacks.length > 0) {
callbacks.forEach(x => {
Expand Down Expand Up @@ -251,11 +269,11 @@ function getCookie(key: string): string {
// * Cookie was previously not encoded by Clarity and browser encoded it once or more
// * Cookie was previously encoded by Clarity and browser did not encode it
let [isEncoded, decodedValue] = decodeCookieValue(pair[1]);

while (isEncoded) {
[isEncoded, decodedValue] = decodeCookieValue(decodedValue);
}

return decodedValue;
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/clarity-js/src/data/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function upgrade(key: string): void {
data = { key };

// Update metadata to track we have upgraded this session
metadata.callback();
metadata.save();

// Callback upgrade handler, if configured
Expand Down
18 changes: 13 additions & 5 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 @@ -125,7 +125,7 @@ async function upload(final: boolean = false): Promise<void> {
let a = `[${analysis.join()}]`;

let p = sendPlaybackBytes ? `[${playback.join()}]` : Constant.Empty;
let encoded: EncodedPayload = {e, a, p};
let encoded: EncodedPayload = { e, a, p };

// Get the payload ready for sending over the wire
// We also attempt to compress the payload if it is not the last payload and the browser supports it
Expand Down Expand Up @@ -241,7 +241,10 @@ 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) { metadata.save(); }
if (sequence === 1) {
metadata.callback();
metadata.save();
}
}

function delay(): number {
Expand All @@ -253,8 +256,7 @@ function delay(): number {

function response(payload: string): void {
let lines = payload && payload.length > 0 ? payload.split("\n") : [];
for (var line of lines)
{
for (var line of lines) {
let parts = line && line.length > 0 ? line.split(/ (.*)/) : [Constant.Empty];
switch (parts[0]) {
case Constant.End:
Expand All @@ -275,6 +277,12 @@ 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;
}
}
}
4 changes: 3 additions & 1 deletion packages/clarity-js/types/data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export const enum Event {
Snapshot = 43,
Animation = 44,
StyleSheetAdoption = 45,
StyleSheetUpdate = 46
StyleSheetUpdate = 46,
Consent = 47
}

export const enum Metric {
Expand Down Expand Up @@ -283,6 +284,7 @@ export const enum Constant {
Action = "ACTION",
Signal = "SIGNAL",
Extract = "EXTRACT",
Sync = "SYNC",
UserHint = "userHint",
UserType = "userType",
UserId = "userId",
Expand Down