generated from PostHog/posthog-plugin-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.ts
27 lines (22 loc) · 743 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { PluginEvent, PluginInput, PluginMeta } from "@posthog/plugin-scaffold";
function normalizeUrl(url: string): string {
try {
const parsedUrl = new URL(url.toLocaleLowerCase());
parsedUrl.pathname = parsedUrl.pathname.replace(/\/$/, "");
return parsedUrl.toString();
} catch (err) {
throw `Unable to normalize invalid URL: "${url}"`;
}
}
export function processEvent(
event: PluginEvent,
meta: PluginMeta<PluginInput>
) {
const $current_url = event?.properties?.$current_url;
if (event?.properties && $current_url) {
const normalized_url = normalizeUrl($current_url);
event.properties.$current_url = normalized_url;
console.debug(`normalized_url: ${normalized_url}`);
}
return event;
}