@getintel/agent 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 GetIntel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # @getintel/agent
2
+
3
+ Shows which AI bots — ChatGPT, Perplexity, Claude, Gemini — read your pages,
4
+ in GetIntel → Analytics → AI crawlers. Bots never run JavaScript, so this
5
+ runs on your server. It only reports requests from GetIntel's AI bot list
6
+ (refreshed hourly from GetIntel — no upgrades needed when bots change), sends
7
+ after the response, and never throws.
8
+
9
+ ```sh
10
+ npm install @getintel/agent
11
+ ```
12
+
13
+ Your site key is in GetIntel → Analytics → AI crawlers → Install our server script.
14
+
15
+ ## Next.js
16
+
17
+ On Next.js 16 the file is `proxy.ts` and the export is `proxy`; on 13–15 it's `middleware.ts` and `middleware`.
18
+
19
+ ```ts
20
+ // middleware.ts
21
+ import { getintel } from '@getintel/agent/next'
22
+
23
+ export const middleware = getintel({ siteKey: 'gi_live_…' })
24
+ ```
25
+
26
+ Already have a middleware? Use `createTracker`:
27
+
28
+ ```ts
29
+ import { createTracker } from '@getintel/agent/next'
30
+ const track = createTracker({ siteKey: 'gi_live_…' })
31
+
32
+ export function middleware(request, event) {
33
+ track(request, event)
34
+ // …your code
35
+ }
36
+ ```
37
+
38
+ ## Express
39
+
40
+ ```js
41
+ const { getintel } = require('@getintel/agent/express')
42
+ app.use(getintel({ siteKey: 'gi_live_…' }))
43
+ ```
44
+
45
+ ## Anything else
46
+
47
+ ```js
48
+ const { createAgent } = require('@getintel/agent')
49
+ const agent = createAgent({ siteKey: 'gi_live_…' })
50
+ agent.track({ ua, ip, path, status }) // returns a promise that always resolves
51
+ ```
52
+
53
+ Visitors from AI assistants are measured by the separate one-line visitor tag
54
+ (AI visitors page), not this package.
package/express.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { AgentOptions } from './index'
2
+
3
+ type Next = (err?: unknown) => void
4
+
5
+ /** Express / Connect middleware that reports AI bot visits after the response is sent. */
6
+ export declare function getintel(options: AgentOptions): (req: any, res: any, next: Next) => void
package/express.js ADDED
@@ -0,0 +1,22 @@
1
+ // @getintel/agent/express — Express / Connect middleware.
2
+ //
3
+ // const { getintel } = require('@getintel/agent/express')
4
+ // app.use(getintel({ siteKey: 'gi_live_…' }))
5
+ //
6
+ // Reports after the response is sent (with its status code), so it never
7
+ // slows a request down.
8
+
9
+ const { createAgent, clientIp } = require('./index')
10
+
11
+ function getintel(options) {
12
+ const agent = createAgent(options)
13
+ return function getintelMiddleware(req, res, next) {
14
+ const get = (k) => (typeof req.get === 'function' ? req.get(k) : req.headers[k])
15
+ res.on('finish', () => {
16
+ agent.track({ ua: get('user-agent'), ip: clientIp(get) || req.ip || '', path: (req.originalUrl || req.url || '/').split('?')[0], status: res.statusCode })
17
+ })
18
+ next()
19
+ }
20
+ }
21
+
22
+ module.exports = { getintel }
package/index.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ export interface AgentOptions {
2
+ /** Your site key from GetIntel → Analytics → AI crawlers (gi_live_…). */
3
+ siteKey: string
4
+ /** Where events are sent. Defaults to https://ingest.getintel.ai. */
5
+ endpoint?: string
6
+ }
7
+
8
+ export interface TrackInput {
9
+ ua?: string | null
10
+ ip?: string | null
11
+ path?: string | null
12
+ status?: number | null
13
+ }
14
+
15
+ export interface Agent {
16
+ /** Reports the request if it came from an AI bot. The promise always resolves. */
17
+ track(input?: TrackInput): Promise<void>
18
+ /** The matching bot user-agent token, or undefined. */
19
+ botFor(ua?: string | null): string | undefined
20
+ }
21
+
22
+ export declare const DEFAULT_ENDPOINT: string
23
+ export declare function createAgent(options: AgentOptions): Agent
24
+ export declare function clientIp(getHeader: (name: string) => string | null | undefined): string
package/index.js ADDED
@@ -0,0 +1,65 @@
1
+ // @getintel/agent — core. Reports requests from AI bots to GetIntel
2
+ // (Analytics → AI crawlers). Bots never run JavaScript, so this runs on your
3
+ // server. Only requests whose user agent matches GetIntel's bot list are
4
+ // sent (the list is downloaded from /v1/config and refreshed hourly), plus
5
+ // one "ping" when the process starts so GetIntel can show it's installed.
6
+ // Fire-and-forget: never throws, never delays your response.
7
+
8
+ const DEFAULT_ENDPOINT = 'https://ingest.getintel.ai'
9
+ const FALLBACK_BOTS = [
10
+ 'OAI-SearchBot', 'ChatGPT-User', 'GPTBot', 'PerplexityBot', 'Perplexity-User',
11
+ 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Google-Agent', 'Google-GeminiNotebook', 'Google-NotebookLM',
12
+ ]
13
+ const HOUR = 60 * 60 * 1000
14
+ const TIMEOUT_MS = 3000
15
+
16
+ // Abort a request that hangs, where the runtime supports it (Node 18+, Edge).
17
+ const withTimeout = () => (typeof AbortSignal !== 'undefined' && typeof AbortSignal.timeout === 'function' ? { signal: AbortSignal.timeout(TIMEOUT_MS) } : {})
18
+
19
+ function createAgent({ siteKey, endpoint = DEFAULT_ENDPOINT } = {}) {
20
+ if (!siteKey) throw new Error('@getintel/agent: siteKey is required')
21
+ const base = String(endpoint).replace(/\/+$/, '')
22
+ let bots = FALLBACK_BOTS
23
+ let fetchedAt = 0
24
+ let pinged = false
25
+
26
+ function refresh() {
27
+ if (Date.now() - fetchedAt < HOUR) return
28
+ fetchedAt = Date.now()
29
+ fetch(`${base}/v1/config`, withTimeout())
30
+ .then((r) => (r.ok ? r.json() : null))
31
+ .then((c) => { if (c && Array.isArray(c.bots)) bots = c.bots.map((b) => b.match).filter(Boolean) })
32
+ .catch(() => {})
33
+ }
34
+
35
+ function botFor(ua) {
36
+ const s = String(ua || '')
37
+ return bots.find((m) => s.includes(m))
38
+ }
39
+
40
+ // Returns a promise that always resolves — pass it to waitUntil where the
41
+ // runtime has one (Next.js middleware, Workers), ignore it elsewhere.
42
+ function track({ ua, ip, path, status } = {}) {
43
+ try {
44
+ refresh()
45
+ const events = []
46
+ if (!pinged) { pinged = true; events.push({ k: siteKey, t: 'ping' }) }
47
+ if (botFor(ua)) events.push({ k: siteKey, t: 'bot', ua: String(ua).slice(0, 400), ip, path, status })
48
+ if (!events.length) return Promise.resolve()
49
+ return fetch(`${base}/v1/hit`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(events), ...withTimeout() })
50
+ .then(() => {}, () => {})
51
+ } catch {
52
+ return Promise.resolve()
53
+ }
54
+ }
55
+
56
+ return { track, botFor }
57
+ }
58
+
59
+ // The real client IP behind common proxies / CDNs.
60
+ function clientIp(getHeader) {
61
+ const xff = getHeader('x-forwarded-for')
62
+ return getHeader('cf-connecting-ip') || (xff && xff.split(',')[0].trim()) || getHeader('x-real-ip') || ''
63
+ }
64
+
65
+ module.exports = { createAgent, clientIp, DEFAULT_ENDPOINT }
package/next.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import type { NextRequest, NextFetchEvent, NextResponse } from 'next/server'
2
+ import type { AgentOptions } from './index'
3
+
4
+ /** Middleware (Next.js 13–15 middleware.ts, or Next.js 16 proxy.ts) that reports AI bot visits. */
5
+ export declare function getintel(options: AgentOptions): (request: NextRequest, event?: NextFetchEvent) => NextResponse
6
+
7
+ /** For an existing middleware or proxy: call track(request, event) inside it. */
8
+ export declare function createTracker(options: AgentOptions): (request: NextRequest, event?: NextFetchEvent) => Promise<void>
package/next.js ADDED
@@ -0,0 +1,33 @@
1
+ // @getintel/agent/next — Next.js middleware (works on the Edge runtime).
2
+ //
3
+ // // middleware.ts
4
+ // import { getintel } from '@getintel/agent/next'
5
+ // export const middleware = getintel({ siteKey: 'gi_live_…' })
6
+ //
7
+ // Already have a middleware? Call track() inside it instead:
8
+ // import { createTracker } from '@getintel/agent/next'
9
+ // const track = createTracker({ siteKey: 'gi_live_…' })
10
+ // export function middleware(request, event) { track(request, event); /* …your code… */ }
11
+
12
+ const { NextResponse } = require('next/server')
13
+ const { createAgent, clientIp } = require('./index')
14
+
15
+ function createTracker(options) {
16
+ const agent = createAgent(options)
17
+ return function track(request, event) {
18
+ const h = request.headers
19
+ const p = agent.track({ ua: h.get('user-agent'), ip: clientIp((k) => h.get(k)), path: request.nextUrl.pathname })
20
+ if (event && typeof event.waitUntil === 'function') event.waitUntil(p)
21
+ return p
22
+ }
23
+ }
24
+
25
+ function getintel(options) {
26
+ const track = createTracker(options)
27
+ return function middleware(request, event) {
28
+ track(request, event)
29
+ return NextResponse.next()
30
+ }
31
+ }
32
+
33
+ module.exports = { getintel, createTracker }
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@getintel/agent",
3
+ "version": "0.1.0",
4
+ "description": "Report AI bot visits (ChatGPT, Perplexity, Claude, Gemini) from your server to GetIntel — Analytics → AI crawlers.",
5
+ "license": "MIT",
6
+ "homepage": "https://getintel.ai/docs/integrations/getintel-script/",
7
+ "main": "index.js",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "default": "./index.js"
12
+ },
13
+ "./next": {
14
+ "types": "./next.d.ts",
15
+ "default": "./next.js"
16
+ },
17
+ "./express": {
18
+ "types": "./express.d.ts",
19
+ "default": "./express.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "index.js",
24
+ "index.d.ts",
25
+ "next.js",
26
+ "next.d.ts",
27
+ "express.js",
28
+ "express.d.ts",
29
+ "README.md",
30
+ "LICENSE"
31
+ ],
32
+ "peerDependencies": {
33
+ "next": ">=13"
34
+ },
35
+ "peerDependenciesMeta": {
36
+ "next": {
37
+ "optional": true
38
+ }
39
+ },
40
+ "engines": {
41
+ "node": ">=18"
42
+ },
43
+ "keywords": [
44
+ "ai",
45
+ "crawlers",
46
+ "chatgpt",
47
+ "perplexity",
48
+ "claude",
49
+ "analytics",
50
+ "getintel"
51
+ ],
52
+ "types": "index.d.ts",
53
+ "author": "GetIntel (https://getintel.ai)"
54
+ }