@knownagents/sdk 1.6.0 → 1.7.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/README.md CHANGED
@@ -29,7 +29,13 @@ Get realtime insight into the hidden ecosystem of [crawlers, scrapers, AI agents
29
29
  To collect this data, call `trackVisit` for each incoming request in the endpoints where you serve your pages.
30
30
 
31
31
  ```ts
32
- knownAgents.trackVisit(incomingRequest)
32
+ knownAgents.trackVisit(request)
33
+ ```
34
+
35
+ For richer analytics, include the response and duration:
36
+
37
+ ```ts
38
+ knownAgents.trackVisit(request, response, responseDurationInMilliseconds)
33
39
  ```
34
40
 
35
41
  ### Use Middleware if Possible
@@ -46,7 +52,13 @@ const app = express()
46
52
  const knownAgents = new KnownAgents("YOUR_ACCESS_TOKEN")
47
53
 
48
54
  app.use((req, res, next) => {
49
- knownAgents.trackVisit(req)
55
+ const start = Date.now()
56
+
57
+ res.on('finish', () => {
58
+ const duration = Date.now() - start
59
+ knownAgents.trackVisit(req, res, duration)
60
+ })
61
+
50
62
  next()
51
63
  })
52
64
 
@@ -82,7 +94,7 @@ const robotsTxt = await knownAgents.generateRobotsTxt([
82
94
 
83
95
  ```
84
96
 
85
- The return value is a plain text robots.txt string. Generate a `robotsTXT` periodically (e.g. once per day), then cache and serve it from your website's `/robots.txt` endpoint.
97
+ The return value is a plain text robots.txt string. Generate a `robotsTxt` periodically (e.g. once per day), then cache and serve it from your website's `/robots.txt` endpoint.
86
98
 
87
99
  ## Requirements
88
100
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IncomingMessage } from "http";
1
+ import { IncomingMessage, ServerResponse } from "http";
2
2
  export interface Request {
3
3
  /** The path of the request's URL (e.g. "/about") */
4
4
  path: string;
@@ -7,6 +7,10 @@ export interface Request {
7
7
  /** The request's HTTP headers */
8
8
  headers: Record<string, string | string[] | undefined>;
9
9
  }
10
+ export interface Response {
11
+ /** The HTTP response status code */
12
+ statusCode: number;
13
+ }
10
14
  /**
11
15
  * Agent types that can be controlled with robots.txt rules.
12
16
  */
@@ -38,9 +42,11 @@ export declare class KnownAgents {
38
42
  * Tracks an agent visit in Known Agents agent analytics.
39
43
  *
40
44
  * @param visitRequest - The incoming visit request.
45
+ * @param visitResponse - The outgoing visit response (optional).
46
+ * @param responseDurationInMilliseconds - The response duration in milliseconds (optional).
41
47
  */
42
- trackVisit(request: Request): void;
43
- trackVisit(request: IncomingMessage): void;
48
+ trackVisit(request: Request, response?: Response, responseDurationInMilliseconds?: number): void;
49
+ trackVisit(request: IncomingMessage, response?: ServerResponse, responseDurationInMilliseconds?: number): void;
44
50
  /**
45
51
  * Generates a `robots.txt` file that disallows the specified agent types.
46
52
  * Cache and serve this string at your website’s `/robots.txt` endpoint.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,MAAM,CAAA;AAItC,MAAM,WAAW,OAAO;IACpB,oDAAoD;IACpD,IAAI,EAAE,MAAM,CAAA;IACZ,qDAAqD;IACrD,MAAM,EAAE,MAAM,CAAA;IACd,iCAAiC;IACjC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAA;CACzD;AAED;;GAEG;AACH,oBAAY,SAAS;IACjB,OAAO,aAAa;IACpB,WAAW,iBAAiB;IAC5B,aAAa,oBAAoB;IACjC,eAAe,sBAAsB;IACrC,QAAQ,aAAa;IACrB,eAAe,qBAAqB;IACpC,OAAO,YAAY;IACnB,aAAa,mBAAmB;IAChC,oBAAoB,0BAA0B;IAC9C,OAAO,YAAY;IACnB,UAAU,gBAAgB;IAC1B,mBAAmB,0BAA0B;IAC7C,eAAe,qBAAqB;IACpC,mBAAmB,0BAA0B;CAChD;AAED,qBAAa,WAAW;IACpB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAQ;IAEpC;;;;OA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Q,EAAE,8BAA8B,CAAC,EAAE,MAAM,GAAG,IAAI;IAChG,UAAU,CAAC,OAAO,EAAE,eAAe,EAAE,QAAQ,CAAC,EAAE,cAAc,EAAE,8BAA8B,CAAC,EAAE,MAAM,GAAG,IAAI;IA4B9G;;;;;;;;OAQG;IACG,iBAAiB,CAAC,UAAU,EAAE,SAAS,EAAE,EAAE,QAAQ,GAAE,MAAY,GAAG,OAAO,CAAC,MAAM,CAAC;IAuBzF,OAAO,CAAC,aAAa;CAcxB"}
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.KnownAgents = exports.AgentType = void 0;
4
- const NODE_PACKAGE_VERSION = "1.3.0";
4
+ const NODE_PACKAGE_VERSION = "1.7.0";
5
5
  /**
6
6
  * Agent types that can be controlled with robots.txt rules.
7
7
  */
@@ -31,7 +31,7 @@ class KnownAgents {
31
31
  constructor(accessToken) {
32
32
  this.accessToken = accessToken;
33
33
  }
34
- trackVisit(visitRequest) {
34
+ trackVisit(visitRequest, visitResponse, responseDurationInMilliseconds) {
35
35
  const request = "path" in visitRequest ? visitRequest : {
36
36
  path: visitRequest.url ? new URL(visitRequest.url, "https://example.org/").pathname : "/",
37
37
  method: visitRequest.method ?? "GET",
@@ -47,7 +47,10 @@ class KnownAgents {
47
47
  request_path: request.path,
48
48
  request_method: request.method,
49
49
  request_headers: this.filterHeaders(request.headers),
50
- node_package_version: NODE_PACKAGE_VERSION
50
+ response_status_code: visitResponse?.statusCode,
51
+ response_duration_in_milliseconds: responseDurationInMilliseconds,
52
+ node_package_version: NODE_PACKAGE_VERSION,
53
+ created: new Date().toISOString()
51
54
  })
52
55
  }).catch(error => {
53
56
  console.error(`Known Agents failed to track visit: ${error.message}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knownagents/sdk",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "The official Node.js SDK for Known Agents: AI agent analytics (bot traffic), LLM referral tracking, automatic robots.txt",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { IncomingMessage } from "http"
1
+ import { IncomingMessage, ServerResponse } from "http"
2
2
 
3
- const NODE_PACKAGE_VERSION = "1.3.0"
3
+ const NODE_PACKAGE_VERSION = "1.7.0"
4
4
 
5
5
  export interface Request {
6
6
  /** The path of the request's URL (e.g. "/about") */
@@ -11,6 +11,11 @@ export interface Request {
11
11
  headers: Record<string, string | string[] | undefined>
12
12
  }
13
13
 
14
+ export interface Response {
15
+ /** The HTTP response status code */
16
+ statusCode: number
17
+ }
18
+
14
19
  /**
15
20
  * Agent types that can be controlled with robots.txt rules.
16
21
  */
@@ -47,10 +52,12 @@ export class KnownAgents {
47
52
  * Tracks an agent visit in Known Agents agent analytics.
48
53
  *
49
54
  * @param visitRequest - The incoming visit request.
55
+ * @param visitResponse - The outgoing visit response (optional).
56
+ * @param responseDurationInMilliseconds - The response duration in milliseconds (optional).
50
57
  */
51
- trackVisit(request: Request): void
52
- trackVisit(request: IncomingMessage): void
53
- trackVisit(visitRequest: Request | IncomingMessage): void {
58
+ trackVisit(request: Request, response?: Response, responseDurationInMilliseconds?: number): void
59
+ trackVisit(request: IncomingMessage, response?: ServerResponse, responseDurationInMilliseconds?: number): void
60
+ trackVisit(visitRequest: Request | IncomingMessage, visitResponse?: Response | ServerResponse, responseDurationInMilliseconds?: number): void {
54
61
  const request: Request = "path" in visitRequest ? visitRequest : {
55
62
  path: visitRequest.url ? new URL(visitRequest.url, "https://example.org/").pathname : "/",
56
63
  method: visitRequest.method ?? "GET",
@@ -67,7 +74,10 @@ export class KnownAgents {
67
74
  request_path: request.path,
68
75
  request_method: request.method,
69
76
  request_headers: this.filterHeaders(request.headers),
70
- node_package_version: NODE_PACKAGE_VERSION
77
+ response_status_code: visitResponse?.statusCode,
78
+ response_duration_in_milliseconds: responseDurationInMilliseconds,
79
+ node_package_version: NODE_PACKAGE_VERSION,
80
+ created: new Date().toISOString()
71
81
  })
72
82
  }).catch(error => {
73
83
  console.error(`Known Agents failed to track visit: ${error.message}`)