@garagem-ai/site-sdk 0.13.5 → 0.13.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +14 -9
- package/dist/index.d.ts +14 -9
- package/dist/index.js +86 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +84 -1
- package/dist/index.mjs.map +1 -1
- package/dist/react.js +46 -0
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +46 -0
- package/dist/react.mjs.map +1 -1
- package/package.json +1 -1
- package/src/analytics-document.ts +90 -0
- package/src/analytics.ts +13 -0
- package/src/index.ts +4 -0
- package/src/studio-contract.ts +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@garagem-ai/site-sdk",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.6",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Typed, runtime-agnostic SDK for building custom real estate sites against the Garagem platform. Drop into any Next.js (or other JS) project to fetch listings, sites and CMS data.",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A visit is a location, a listing, or a static page. Filter paths and
|
|
3
|
+
* `page` greater than 1 are the crawl space Studio's catch-all answers.
|
|
4
|
+
* `analyticsDocumentGuardSource()` is the same check pasted into the
|
|
5
|
+
* generated client, which cannot import this helper from the pinned tarball.
|
|
6
|
+
* Prompts must not name `isAnalyticsDocumentUrl`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const FILTER_SEGMENT_SOURCE =
|
|
10
|
+
"(?:^|-)(?:quartos|banheiros|suites|suite|vagas)$|(?:^|-)(?:reais|m2)$|^mapa$";
|
|
11
|
+
|
|
12
|
+
const CLICK_ID_SOURCE =
|
|
13
|
+
"^(?:fbclid|gclid|gbraid|wbraid|msclkid|ttclid|igshid|yclid|twclid|mc_eid|mc_cid|gad_source|_gl)$";
|
|
14
|
+
|
|
15
|
+
const DOCUMENT_QUERY_KEYS = ["q", "ordenar", "page", "pagina"] as const;
|
|
16
|
+
|
|
17
|
+
const FILTER_SEGMENT = new RegExp(FILTER_SEGMENT_SOURCE, "i");
|
|
18
|
+
const CLICK_ID = new RegExp(CLICK_ID_SOURCE);
|
|
19
|
+
|
|
20
|
+
/** `true` when this URL is a page a person opened, not a crawl of filters. */
|
|
21
|
+
export function isAnalyticsDocumentUrl(href: string): boolean {
|
|
22
|
+
let url: URL;
|
|
23
|
+
try {
|
|
24
|
+
url = new URL(href, "https://site.invalid");
|
|
25
|
+
} catch {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
const pageRaw = param(url, "page") ?? param(url, "pagina");
|
|
29
|
+
if (pageRaw != null && pageRaw !== "") {
|
|
30
|
+
const page = Number(pageRaw);
|
|
31
|
+
if (!Number.isInteger(page) || page !== 1) return false;
|
|
32
|
+
}
|
|
33
|
+
const path = url.pathname.replace(/\/+$/, "").toLowerCase() || "/";
|
|
34
|
+
if (path !== "/imoveis" && !path.startsWith("/imoveis/")) return true;
|
|
35
|
+
for (const key of url.searchParams.keys()) {
|
|
36
|
+
if (ignoredQuery(key.toLowerCase())) continue;
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
const segments = path.split("/").filter(Boolean).slice(1);
|
|
40
|
+
return segments.every((segment) => !FILTER_SEGMENT.test(segment));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function param(url: URL, name: string): string | null {
|
|
44
|
+
for (const [key, value] of url.searchParams) {
|
|
45
|
+
if (key.toLowerCase() === name) return value;
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function ignoredQuery(key: string): boolean {
|
|
51
|
+
return (
|
|
52
|
+
(DOCUMENT_QUERY_KEYS as readonly string[]).includes(key) ||
|
|
53
|
+
key.startsWith("utm_") ||
|
|
54
|
+
CLICK_ID.test(key)
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Source paste of `isAnalyticsDocumentUrl` for the generated Studio client. */
|
|
59
|
+
export function analyticsDocumentGuardSource(): string {
|
|
60
|
+
return `const FILTER_SEGMENT = /${FILTER_SEGMENT_SOURCE}/i;
|
|
61
|
+
const CLICK_ID = /${CLICK_ID_SOURCE}/;
|
|
62
|
+
const DOCUMENT_QUERY = ["q", "ordenar", "page", "pagina"];
|
|
63
|
+
|
|
64
|
+
function documentUrl(href) {
|
|
65
|
+
let url;
|
|
66
|
+
try {
|
|
67
|
+
url = new URL(href, "https://site.invalid");
|
|
68
|
+
} catch {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
let pageRaw = null;
|
|
72
|
+
for (const [key, value] of url.searchParams) {
|
|
73
|
+
const name = key.toLowerCase();
|
|
74
|
+
if (pageRaw == null && (name === "page" || name === "pagina")) pageRaw = value;
|
|
75
|
+
}
|
|
76
|
+
if (pageRaw != null && pageRaw !== "") {
|
|
77
|
+
const page = Number(pageRaw);
|
|
78
|
+
if (!Number.isInteger(page) || page !== 1) return false;
|
|
79
|
+
}
|
|
80
|
+
const path = url.pathname.replace(/\\/+$/, "").toLowerCase() || "/";
|
|
81
|
+
if (path !== "/imoveis" && !path.startsWith("/imoveis/")) return true;
|
|
82
|
+
for (const key of url.searchParams.keys()) {
|
|
83
|
+
const name = key.toLowerCase();
|
|
84
|
+
if (DOCUMENT_QUERY.includes(name) || name.startsWith("utm_") || CLICK_ID.test(name)) continue;
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
const segments = path.split("/").filter(Boolean).slice(1);
|
|
88
|
+
return segments.every((segment) => !FILTER_SEGMENT.test(segment));
|
|
89
|
+
}`;
|
|
90
|
+
}
|
package/src/analytics.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* consent, the visitor cookie and the site id, and posts it to `/api/t`.
|
|
5
5
|
* Without that listener (server render, other hosts) nothing is sent.
|
|
6
6
|
*/
|
|
7
|
+
import { isAnalyticsDocumentUrl } from "./analytics-document";
|
|
7
8
|
import { changedPathFilters, type LocationNameTables } from "./search-engine";
|
|
8
9
|
import type { AppliedSearchQuery, Listing, SearchPathFilters } from "./types";
|
|
9
10
|
|
|
@@ -16,11 +17,23 @@ type SiteEventName =
|
|
|
16
17
|
| "listings_impressed"
|
|
17
18
|
| "lead_generated";
|
|
18
19
|
|
|
20
|
+
function browserPageHref(): string | null {
|
|
21
|
+
if (typeof window === "undefined") return null;
|
|
22
|
+
const location = (window as { location?: { href?: unknown } }).location;
|
|
23
|
+
return typeof location?.href === "string" ? location.href : null;
|
|
24
|
+
}
|
|
25
|
+
|
|
19
26
|
function emitSiteEvent(
|
|
20
27
|
event: SiteEventName,
|
|
21
28
|
properties: Record<string, unknown>
|
|
22
29
|
): void {
|
|
23
30
|
if (typeof window === "undefined") return;
|
|
31
|
+
const href = browserPageHref();
|
|
32
|
+
// A lead on a filter URL is still a conversion. Everything else on a
|
|
33
|
+
// crawl URL is not a visit — the installed client drops the pageview too.
|
|
34
|
+
if (href && event !== "lead_generated" && !isAnalyticsDocumentUrl(href)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
24
37
|
window.dispatchEvent(
|
|
25
38
|
new CustomEvent(SITE_ANALYTICS_EVENT, { detail: { event, properties } })
|
|
26
39
|
);
|
package/src/index.ts
CHANGED
|
@@ -85,6 +85,10 @@ export {
|
|
|
85
85
|
export { getShareLink, type GetShareLinkOptions } from "./share";
|
|
86
86
|
|
|
87
87
|
export { SITE_ANALYTICS_EVENT, trackListingView } from "./analytics";
|
|
88
|
+
export {
|
|
89
|
+
analyticsDocumentGuardSource,
|
|
90
|
+
isAnalyticsDocumentUrl,
|
|
91
|
+
} from "./analytics-document";
|
|
88
92
|
|
|
89
93
|
export type {
|
|
90
94
|
ShareEntityType,
|
package/src/studio-contract.ts
CHANGED
|
@@ -158,6 +158,9 @@ export const STUDIO_SDK_CONTRACT = {
|
|
|
158
158
|
// `unknownSearchLocation` is in git. Prompts inline the same check with
|
|
159
159
|
// `parseSearchPublicPath` + `locationsPath` until this name is on the pin.
|
|
160
160
|
"unknownSearchLocation",
|
|
161
|
+
// In git. The installed client inlines the check. Naming it in a prompt
|
|
162
|
+
// makes v0 import it from the pinned tarball, which does not export it.
|
|
163
|
+
"isAnalyticsDocumentUrl",
|
|
161
164
|
] as const,
|
|
162
165
|
/**
|
|
163
166
|
* maps.fndn.ai helpers (static map, street view, boundaries, travel
|