@ooky/sdk 0.1.0 → 0.6.0

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.
@@ -0,0 +1,73 @@
1
+ /**
2
+ * AI referrer detection — identifies humans arriving from AI platforms by
3
+ * Referer header or utm_source. Mirrors worker/src/referrals.js so the SDK
4
+ * and Worker tiers attribute the same visits; when you add a platform to
5
+ * one, add it to the other.
6
+ */
7
+
8
+ export const AI_REFERRERS = [
9
+ { pattern: "chatgpt.com", source: "chatgpt" },
10
+ { pattern: "chat.openai.com", source: "chatgpt" },
11
+ { pattern: "perplexity.ai", source: "perplexity" },
12
+ { pattern: "gemini.google.com", source: "gemini" },
13
+ { pattern: "bard.google.com", source: "gemini" },
14
+ { pattern: "copilot.microsoft.com", source: "copilot" },
15
+ { pattern: "bing.com/chat", source: "copilot" },
16
+ { pattern: "claude.ai", source: "claude" },
17
+ { pattern: "meta.ai", source: "meta_ai" },
18
+ { pattern: "grok.x.ai", source: "grok" },
19
+ { pattern: "you.com", source: "you" },
20
+ { pattern: "phind.com", source: "phind" },
21
+ { pattern: "deepseek.com", source: "deepseek" },
22
+ ];
23
+
24
+ export const UTM_SOURCES = [
25
+ { value: "chatgpt", source: "chatgpt" },
26
+ { value: "openai", source: "chatgpt" },
27
+ { value: "perplexity", source: "perplexity" },
28
+ { value: "gemini", source: "gemini" },
29
+ { value: "copilot", source: "copilot" },
30
+ { value: "claude", source: "claude" },
31
+ { value: "meta_ai", source: "meta_ai" },
32
+ { value: "grok", source: "grok" },
33
+ { value: "you", source: "you" },
34
+ { value: "phind", source: "phind" },
35
+ { value: "deepseek", source: "deepseek" },
36
+ ];
37
+
38
+ /**
39
+ * Detect an AI-platform referral from a Referer header and/or utm_source.
40
+ *
41
+ * @param {string|null|undefined} referer The request's Referer header.
42
+ * @param {string|null|undefined} utmSource The utm_source query param value.
43
+ * @returns {{ source: string, referrerUrl: string|null, method: string } | null}
44
+ */
45
+ export function detectAIReferral(referer, utmSource) {
46
+ if (referer && typeof referer === "string") {
47
+ const refererLower = referer.toLowerCase();
48
+ for (const entry of AI_REFERRERS) {
49
+ if (refererLower.includes(entry.pattern)) {
50
+ return {
51
+ source: entry.source,
52
+ referrerUrl: referer,
53
+ method: "referer_header",
54
+ };
55
+ }
56
+ }
57
+ }
58
+
59
+ if (utmSource && typeof utmSource === "string") {
60
+ const value = utmSource.toLowerCase();
61
+ for (const entry of UTM_SOURCES) {
62
+ if (value === entry.value) {
63
+ return {
64
+ source: entry.source,
65
+ referrerUrl: null,
66
+ method: "utm_param",
67
+ };
68
+ }
69
+ }
70
+ }
71
+
72
+ return null;
73
+ }