Skip to content

Commit cdb5eea

Browse files
feat: add UTM tracking for agent hosting and IronClaw URLs (#351)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 17f4552 commit cdb5eea

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

src/api/constants.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { withUtm } from "@/lib/utm";
2+
13
export const APP_NAME = "NEAR AI Private Chat";
24
// TODO: remove DEPRECATED_API_BASE_URL from the project as there's no dependency on the legacy Private Chat any more
35
export const DEPRECATED_API_BASE_URL = (import.meta.env.VITE_DEPRECATED_API_URL || "https://private-chat-legacy.near.ai").replace(
@@ -40,5 +42,6 @@ export const NEAR_RPC_URL = import.meta.env.VITE_NEAR_RPC_URL || "https://free.r
4042
export const NEAR_AI_CLOUD_MODELS_URL = "https://cloud.near.ai/models";
4143

4244
export const AGENT_HOST = "agent.near.ai";
43-
export const AGENT_URL = `https://${AGENT_HOST}`;
44-
export const AGENT_BILLING_URL = `https://${AGENT_HOST}/billing`;
45+
46+
export const AGENT_URL = withUtm(`https://${AGENT_HOST}`);
47+
export const AGENT_BILLING_URL = withUtm(`https://${AGENT_HOST}/billing`);

src/lib/constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { withUtm } from "@/lib/utm";
2+
13
export const LOCAL_STORAGE_KEYS = {
24
TOKEN: "sessionToken",
35
SESSION: "sessionId",
@@ -58,4 +60,4 @@ export const SUPPORTED_TEXT_EXTENSIONS = [
5860
export const ACCEPTED_FILE_TYPES = SUPPORTED_TEXT_EXTENSIONS.join(",");
5961
// export const ACCEPTED_FILE_TYPES = [".pdf", ...SUPPORTED_TEXT_EXTENSIONS].join(",");
6062

61-
export const IRONCLAW_URL = "https://www.ironclaw.com";
63+
export const IRONCLAW_URL = withUtm("https://www.ironclaw.com/");

src/lib/utm.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export const UTM_SOURCE = "private_chat";
2+
export const UTM_MEDIUM = "web";
3+
4+
/**
5+
* Adds default UTM parameters to a URL string.
6+
*
7+
* - Sets `utm_source` and `utm_medium` only when the corresponding query
8+
* parameters are missing (e.g. `?utm_source=` is considered "present" and
9+
* will not be overwritten).
10+
* - If the value cannot be parsed as a URL, the original `url` string is
11+
* returned unchanged.
12+
*
13+
* @param url - A URL (absolute or relative) to augment.
14+
* @param base - Optional base used to resolve `url` when it's relative.
15+
* @returns The augmented URL as a string, or the original input on parse failure.
16+
*/
17+
export function withUtm(url: string, base?: string) {
18+
let u: URL;
19+
try {
20+
u = base ? new URL(url, base) : new URL(url);
21+
} catch (error) {
22+
console.error(
23+
`[withUtm] Failed to construct URL from value: "${url}". Returning original string.`,
24+
error
25+
);
26+
return url;
27+
}
28+
if (!u.searchParams.has("utm_source")) u.searchParams.set("utm_source", UTM_SOURCE);
29+
if (!u.searchParams.has("utm_medium")) u.searchParams.set("utm_medium", UTM_MEDIUM);
30+
return u.toString();
31+
}

0 commit comments

Comments
 (0)