@codaco/analytics 5.0.0 → 6.0.0-alpha.1
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/.turbo/turbo-build.log +2 -7
- package/dist/index.d.ts +28 -25
- package/dist/index.js +33 -20
- package/dist/index.js.map +1 -1
- package/package.json +19 -9
- package/src/index.ts +74 -52
- package/src/utils.ts +12 -7
- package/tsconfig.json +8 -10
- package/.eslintrc.js +0 -4
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
> @codaco/analytics@5.
|
|
3
|
+
> @codaco/analytics@5.1.0 build /Users/jmh629/Projects/network-canvas/packages/analytics
|
|
4
4
|
> tsup src/index.ts --format esm --dts --clean --sourcemap
|
|
5
5
|
|
|
6
6
|
[34mCLI[39m Building entry: src/index.ts
|
|
7
7
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
8
|
-
[34mCLI[39m tsup
|
|
8
|
+
[34mCLI[39m tsup v8.0.2
|
|
9
9
|
[34mCLI[39m Target: es2022
|
|
10
10
|
[34mCLI[39m Cleaning output folder
|
|
11
11
|
[34mESM[39m Build start
|
|
12
|
-
[32mESM[39m [1mdist/index.js [22m[32m5.77 KB[39m
|
|
13
|
-
[32mESM[39m [1mdist/index.js.map [22m[32m11.87 KB[39m
|
|
14
|
-
[32mESM[39m ⚡️ Build success in 18ms
|
|
15
12
|
[34mDTS[39m Build start
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in 907ms
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[32m5.64 KB[39m
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { NextRequest } from 'next/server';
|
|
2
|
-
import { WebServiceClient } from '@maxmind/geoip2-node';
|
|
3
2
|
import z from 'zod';
|
|
4
3
|
|
|
5
4
|
declare const eventTypes: readonly ["AppSetup", "ProtocolInstalled", "InterviewStarted", "InterviewCompleted", "DataExported"];
|
|
@@ -8,68 +7,72 @@ declare const eventTypes: readonly ["AppSetup", "ProtocolInstalled", "InterviewS
|
|
|
8
7
|
* events or errors. We discriminate on the `type` property to determine which
|
|
9
8
|
* schema to use, and then merge the shared properties.
|
|
10
9
|
*/
|
|
11
|
-
declare const RawEventSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
10
|
+
declare const RawEventSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<z.objectUtil.extendShape<{
|
|
12
11
|
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
12
|
+
}, {
|
|
13
13
|
type: z.ZodEnum<["AppSetup", "ProtocolInstalled", "InterviewStarted", "InterviewCompleted", "DataExported"]>;
|
|
14
|
-
}
|
|
14
|
+
}>, "strip", z.ZodTypeAny, {
|
|
15
15
|
type: "AppSetup" | "ProtocolInstalled" | "InterviewStarted" | "InterviewCompleted" | "DataExported";
|
|
16
16
|
metadata?: Record<string, unknown> | undefined;
|
|
17
17
|
}, {
|
|
18
18
|
type: "AppSetup" | "ProtocolInstalled" | "InterviewStarted" | "InterviewCompleted" | "DataExported";
|
|
19
19
|
metadata?: Record<string, unknown> | undefined;
|
|
20
|
-
}>, z.ZodObject<{
|
|
20
|
+
}>, z.ZodObject<z.objectUtil.extendShape<{
|
|
21
21
|
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
22
|
+
}, {
|
|
22
23
|
type: z.ZodLiteral<"Error">;
|
|
23
24
|
message: z.ZodString;
|
|
24
25
|
name: z.ZodString;
|
|
25
26
|
stack: z.ZodOptional<z.ZodString>;
|
|
26
27
|
cause: z.ZodOptional<z.ZodString>;
|
|
27
|
-
}
|
|
28
|
+
}>, "strip", z.ZodTypeAny, {
|
|
28
29
|
type: "Error";
|
|
29
30
|
message: string;
|
|
30
31
|
name: string;
|
|
31
|
-
metadata?: Record<string, unknown> | undefined;
|
|
32
32
|
stack?: string | undefined;
|
|
33
33
|
cause?: string | undefined;
|
|
34
|
+
metadata?: Record<string, unknown> | undefined;
|
|
34
35
|
}, {
|
|
35
36
|
type: "Error";
|
|
36
37
|
message: string;
|
|
37
38
|
name: string;
|
|
38
|
-
metadata?: Record<string, unknown> | undefined;
|
|
39
39
|
stack?: string | undefined;
|
|
40
40
|
cause?: string | undefined;
|
|
41
|
+
metadata?: Record<string, unknown> | undefined;
|
|
41
42
|
}>]>;
|
|
42
43
|
type RawEvent = z.infer<typeof RawEventSchema>;
|
|
43
|
-
declare const TrackableEventSchema: z.ZodIntersection<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
44
|
+
declare const TrackableEventSchema: z.ZodIntersection<z.ZodDiscriminatedUnion<"type", [z.ZodObject<z.objectUtil.extendShape<{
|
|
44
45
|
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
46
|
+
}, {
|
|
45
47
|
type: z.ZodEnum<["AppSetup", "ProtocolInstalled", "InterviewStarted", "InterviewCompleted", "DataExported"]>;
|
|
46
|
-
}
|
|
48
|
+
}>, "strip", z.ZodTypeAny, {
|
|
47
49
|
type: "AppSetup" | "ProtocolInstalled" | "InterviewStarted" | "InterviewCompleted" | "DataExported";
|
|
48
50
|
metadata?: Record<string, unknown> | undefined;
|
|
49
51
|
}, {
|
|
50
52
|
type: "AppSetup" | "ProtocolInstalled" | "InterviewStarted" | "InterviewCompleted" | "DataExported";
|
|
51
53
|
metadata?: Record<string, unknown> | undefined;
|
|
52
|
-
}>, z.ZodObject<{
|
|
54
|
+
}>, z.ZodObject<z.objectUtil.extendShape<{
|
|
53
55
|
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
56
|
+
}, {
|
|
54
57
|
type: z.ZodLiteral<"Error">;
|
|
55
58
|
message: z.ZodString;
|
|
56
59
|
name: z.ZodString;
|
|
57
60
|
stack: z.ZodOptional<z.ZodString>;
|
|
58
61
|
cause: z.ZodOptional<z.ZodString>;
|
|
59
|
-
}
|
|
62
|
+
}>, "strip", z.ZodTypeAny, {
|
|
60
63
|
type: "Error";
|
|
61
64
|
message: string;
|
|
62
65
|
name: string;
|
|
63
|
-
metadata?: Record<string, unknown> | undefined;
|
|
64
66
|
stack?: string | undefined;
|
|
65
67
|
cause?: string | undefined;
|
|
68
|
+
metadata?: Record<string, unknown> | undefined;
|
|
66
69
|
}, {
|
|
67
70
|
type: "Error";
|
|
68
71
|
message: string;
|
|
69
72
|
name: string;
|
|
70
|
-
metadata?: Record<string, unknown> | undefined;
|
|
71
73
|
stack?: string | undefined;
|
|
72
74
|
cause?: string | undefined;
|
|
75
|
+
metadata?: Record<string, unknown> | undefined;
|
|
73
76
|
}>]>, z.ZodObject<{
|
|
74
77
|
timestamp: z.ZodString;
|
|
75
78
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -83,36 +86,38 @@ type TrackableEvent = z.infer<typeof TrackableEventSchema>;
|
|
|
83
86
|
* validate the event before it is inserted into the database. It is the
|
|
84
87
|
* intersection of the trackable event and the dispatchable properties.
|
|
85
88
|
*/
|
|
86
|
-
declare const AnalyticsEventSchema: z.ZodIntersection<z.ZodIntersection<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
89
|
+
declare const AnalyticsEventSchema: z.ZodIntersection<z.ZodIntersection<z.ZodDiscriminatedUnion<"type", [z.ZodObject<z.objectUtil.extendShape<{
|
|
87
90
|
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
91
|
+
}, {
|
|
88
92
|
type: z.ZodEnum<["AppSetup", "ProtocolInstalled", "InterviewStarted", "InterviewCompleted", "DataExported"]>;
|
|
89
|
-
}
|
|
93
|
+
}>, "strip", z.ZodTypeAny, {
|
|
90
94
|
type: "AppSetup" | "ProtocolInstalled" | "InterviewStarted" | "InterviewCompleted" | "DataExported";
|
|
91
95
|
metadata?: Record<string, unknown> | undefined;
|
|
92
96
|
}, {
|
|
93
97
|
type: "AppSetup" | "ProtocolInstalled" | "InterviewStarted" | "InterviewCompleted" | "DataExported";
|
|
94
98
|
metadata?: Record<string, unknown> | undefined;
|
|
95
|
-
}>, z.ZodObject<{
|
|
99
|
+
}>, z.ZodObject<z.objectUtil.extendShape<{
|
|
96
100
|
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
101
|
+
}, {
|
|
97
102
|
type: z.ZodLiteral<"Error">;
|
|
98
103
|
message: z.ZodString;
|
|
99
104
|
name: z.ZodString;
|
|
100
105
|
stack: z.ZodOptional<z.ZodString>;
|
|
101
106
|
cause: z.ZodOptional<z.ZodString>;
|
|
102
|
-
}
|
|
107
|
+
}>, "strip", z.ZodTypeAny, {
|
|
103
108
|
type: "Error";
|
|
104
109
|
message: string;
|
|
105
110
|
name: string;
|
|
106
|
-
metadata?: Record<string, unknown> | undefined;
|
|
107
111
|
stack?: string | undefined;
|
|
108
112
|
cause?: string | undefined;
|
|
113
|
+
metadata?: Record<string, unknown> | undefined;
|
|
109
114
|
}, {
|
|
110
115
|
type: "Error";
|
|
111
116
|
message: string;
|
|
112
117
|
name: string;
|
|
113
|
-
metadata?: Record<string, unknown> | undefined;
|
|
114
118
|
stack?: string | undefined;
|
|
115
119
|
cause?: string | undefined;
|
|
120
|
+
metadata?: Record<string, unknown> | undefined;
|
|
116
121
|
}>]>, z.ZodObject<{
|
|
117
122
|
timestamp: z.ZodString;
|
|
118
123
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -130,14 +135,12 @@ declare const AnalyticsEventSchema: z.ZodIntersection<z.ZodIntersection<z.ZodDis
|
|
|
130
135
|
countryISOCode: string;
|
|
131
136
|
}>>;
|
|
132
137
|
type analyticsEvent = z.infer<typeof AnalyticsEventSchema>;
|
|
133
|
-
declare const createRouteHandler: ({ platformUrl, installationId,
|
|
134
|
-
platformUrl?: string
|
|
138
|
+
declare const createRouteHandler: ({ platformUrl, installationId, }: {
|
|
139
|
+
platformUrl?: string;
|
|
135
140
|
installationId: string;
|
|
136
|
-
maxMindClient: WebServiceClient;
|
|
137
141
|
}) => (request: NextRequest) => Promise<Response>;
|
|
138
|
-
declare const makeEventTracker: ({
|
|
139
|
-
|
|
140
|
-
endpoint?: string | undefined;
|
|
142
|
+
declare const makeEventTracker: ({ endpoint }: {
|
|
143
|
+
endpoint?: string;
|
|
141
144
|
}) => (event: RawEvent) => Promise<{
|
|
142
145
|
error: string | null;
|
|
143
146
|
success: boolean;
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { NextResponse } from "next/server";
|
|
3
|
+
|
|
1
4
|
// src/utils.ts
|
|
2
5
|
function ensureError(value) {
|
|
3
6
|
if (!value)
|
|
4
7
|
return new Error("No value was thrown");
|
|
5
8
|
if (value instanceof Error)
|
|
6
9
|
return value;
|
|
7
|
-
if (
|
|
10
|
+
if (Object.prototype.isPrototypeOf.call(value, Error))
|
|
8
11
|
return value;
|
|
9
12
|
let stringified = "[Unable to stringify the thrown value]";
|
|
10
13
|
try {
|
|
11
14
|
stringified = JSON.stringify(value);
|
|
12
|
-
} catch {
|
|
15
|
+
} catch (e) {
|
|
16
|
+
console.error(e);
|
|
13
17
|
}
|
|
14
18
|
const error = new Error(
|
|
15
19
|
`This value was thrown as is, not through an Error: ${stringified}`
|
|
@@ -69,21 +73,31 @@ var AnalyticsEventSchema = z.intersection(
|
|
|
69
73
|
);
|
|
70
74
|
var createRouteHandler = ({
|
|
71
75
|
platformUrl = "https://analytics.networkcanvas.com",
|
|
72
|
-
installationId
|
|
73
|
-
maxMindClient
|
|
76
|
+
installationId
|
|
74
77
|
}) => {
|
|
75
78
|
return async (request) => {
|
|
76
79
|
try {
|
|
77
80
|
const incomingEvent = await request.json();
|
|
81
|
+
if (process.env.DISABLE_ANALYTICS) {
|
|
82
|
+
console.info("\u{1F6D1} Analytics disabled. Payload not sent.");
|
|
83
|
+
try {
|
|
84
|
+
console.info(
|
|
85
|
+
"Payload:",
|
|
86
|
+
"\n",
|
|
87
|
+
JSON.stringify(incomingEvent, null, 2)
|
|
88
|
+
);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
console.error("Error stringifying payload:", e);
|
|
91
|
+
}
|
|
92
|
+
return NextResponse.json(
|
|
93
|
+
{ message: "Analytics disabled" },
|
|
94
|
+
{ status: 200 }
|
|
95
|
+
);
|
|
96
|
+
}
|
|
78
97
|
const trackableEvent = TrackableEventSchema.safeParse(incomingEvent);
|
|
79
98
|
if (!trackableEvent.success) {
|
|
80
99
|
console.error("Invalid event:", trackableEvent.error);
|
|
81
|
-
return
|
|
82
|
-
status: 400,
|
|
83
|
-
headers: {
|
|
84
|
-
"Content-Type": "application/json"
|
|
85
|
-
}
|
|
86
|
-
});
|
|
100
|
+
return NextResponse.json({ error: "Invalid event" }, { status: 400 });
|
|
87
101
|
}
|
|
88
102
|
let countryISOCode = "Unknown";
|
|
89
103
|
try {
|
|
@@ -93,8 +107,14 @@ var createRouteHandler = ({
|
|
|
93
107
|
if (!ip) {
|
|
94
108
|
throw new Error("Could not fetch IP address");
|
|
95
109
|
}
|
|
96
|
-
const
|
|
97
|
-
|
|
110
|
+
const geoData = await fetch(`http://ip-api.com/json/${ip}`).then(
|
|
111
|
+
(res) => res.json()
|
|
112
|
+
);
|
|
113
|
+
if (geoData.status === "success") {
|
|
114
|
+
countryISOCode = geoData.countryCode;
|
|
115
|
+
} else {
|
|
116
|
+
throw new Error(geoData.message);
|
|
117
|
+
}
|
|
98
118
|
} catch (e) {
|
|
99
119
|
console.error("Geolocation failed:", e);
|
|
100
120
|
}
|
|
@@ -142,14 +162,7 @@ var createRouteHandler = ({
|
|
|
142
162
|
}
|
|
143
163
|
};
|
|
144
164
|
};
|
|
145
|
-
var makeEventTracker = ({
|
|
146
|
-
enabled = false,
|
|
147
|
-
endpoint = "/api/analytics"
|
|
148
|
-
}) => async (event) => {
|
|
149
|
-
if (!enabled) {
|
|
150
|
-
console.log("Analytics disabled - event not sent.");
|
|
151
|
-
return { error: null, success: true };
|
|
152
|
-
}
|
|
165
|
+
var makeEventTracker = ({ endpoint = "/api/analytics" }) => async (event) => {
|
|
153
166
|
const endpointWithHost = getBaseUrl() + endpoint;
|
|
154
167
|
const eventWithTimeStamp = {
|
|
155
168
|
...event,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["// Helper function that ensures that a value is an Error\nexport function ensureError(value: unknown): Error {\n if (!value) return new Error(\"No value was thrown\");\n\n if (value instanceof Error) return value;\n\n // Test if value inherits from Error\n if (value.isPrototypeOf(Error)) return value as Error & typeof value;\n\n let stringified = \"[Unable to stringify the thrown value]\";\n try {\n stringified = JSON.stringify(value);\n } catch {}\n\n const error = new Error(\n `This value was thrown as is, not through an Error: ${stringified}`\n );\n return error;\n}\n\nexport function getBaseUrl() {\n if (typeof window !== \"undefined\")\n // browser should use relative path\n return \"\";\n\n if (process.env.VERCEL_URL)\n // reference for vercel.com\n return `https://${process.env.VERCEL_URL}`;\n\n if (process.env.NEXT_PUBLIC_URL)\n // Manually set deployment URL from env\n return process.env.NEXT_PUBLIC_URL;\n\n // assume localhost\n return `http://127.0.0.1:3000`;\n}\n","import { type NextRequest } from \"next/server\";\nimport { WebServiceClient } from \"@maxmind/geoip2-node\";\nimport { ensureError, getBaseUrl } from \"./utils\";\nimport z from \"zod\";\n\n// Todo: it would be great to work out a way to support arbitrary types here.\nexport const eventTypes = [\n \"AppSetup\",\n \"ProtocolInstalled\",\n \"InterviewStarted\",\n \"InterviewCompleted\",\n \"DataExported\",\n] as const;\n\nconst EventSchema = z.object({\n type: z.enum(eventTypes),\n});\n\nconst ErrorSchema = z.object({\n type: z.literal(\"Error\"),\n message: z.string(),\n name: z.string(),\n stack: z.string().optional(),\n cause: z.string().optional(),\n});\n\nconst SharedEventAndErrorSchema = z.object({\n metadata: z.record(z.unknown()).optional(),\n});\n\n/**\n * Raw events are the events that are sent trackEvent. They are either general\n * events or errors. We discriminate on the `type` property to determine which\n * schema to use, and then merge the shared properties.\n */\nexport const RawEventSchema = z.discriminatedUnion(\"type\", [\n SharedEventAndErrorSchema.merge(EventSchema),\n SharedEventAndErrorSchema.merge(ErrorSchema),\n]);\nexport type RawEvent = z.infer<typeof RawEventSchema>;\n\n/**\n * Trackable events are the events that are sent to the route handler. The\n * `trackEvent` function adds the timestamp to ensure it is not inaccurate\n * due to network latency or processing time.\n */\nconst TrackablePropertiesSchema = z.object({\n timestamp: z.string(),\n});\n\nexport const TrackableEventSchema = z.intersection(\n RawEventSchema,\n TrackablePropertiesSchema\n);\nexport type TrackableEvent = z.infer<typeof TrackableEventSchema>;\n\n/**\n * Dispatchable events are the events that are sent to the platform. The route\n * handler injects the installationId and countryISOCode properties.\n */\nconst DispatchablePropertiesSchema = z.object({\n installationId: z.string(),\n countryISOCode: z.string(),\n});\n\n/**\n * The final schema for an analytics event. This is the schema that is used to\n * validate the event before it is inserted into the database. It is the\n * intersection of the trackable event and the dispatchable properties.\n */\nexport const AnalyticsEventSchema = z.intersection(\n TrackableEventSchema,\n DispatchablePropertiesSchema\n);\nexport type analyticsEvent = z.infer<typeof AnalyticsEventSchema>;\n\nexport const createRouteHandler = ({\n platformUrl = \"https://analytics.networkcanvas.com\",\n installationId,\n maxMindClient,\n}: {\n platformUrl?: string;\n installationId: string;\n maxMindClient: WebServiceClient;\n}) => {\n return async (request: NextRequest) => {\n try {\n const incomingEvent = (await request.json()) as unknown;\n\n // Validate the event\n const trackableEvent = TrackableEventSchema.safeParse(incomingEvent);\n\n if (!trackableEvent.success) {\n console.error(\"Invalid event:\", trackableEvent.error);\n return new Response(JSON.stringify({ error: \"Invalid event\" }), {\n status: 400,\n headers: {\n \"Content-Type\": \"application/json\",\n },\n });\n }\n\n // We don't want failures in third party services to prevent us from\n // tracking analytics events, so we'll catch any errors and log them\n // and continue with an 'Unknown' country code.\n let countryISOCode = \"Unknown\";\n try {\n const ip = await fetch(\"https://api64.ipify.org\").then((res) =>\n res.text()\n );\n\n if (!ip) {\n throw new Error(\"Could not fetch IP address\");\n }\n\n const { country } = await maxMindClient.country(ip);\n countryISOCode = country?.isoCode ?? \"Unknown\";\n } catch (e) {\n console.error(\"Geolocation failed:\", e);\n }\n\n const analyticsEvent: analyticsEvent = {\n ...trackableEvent.data,\n installationId,\n countryISOCode,\n };\n\n // Forward to backend\n const response = await fetch(`${platformUrl}/api/event`, {\n keepalive: true,\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(analyticsEvent),\n });\n\n if (!response.ok) {\n let error = `Analytics platform returned an unexpected error: ${response.statusText}`;\n\n if (response.status === 400) {\n error = `Analytics platform rejected the event as invalid. Please check the event schema`;\n }\n\n if (response.status === 404) {\n error = `Analytics platform could not be reached. Please specify a valid platform URL, or check that the platform is online.`;\n }\n\n if (response.status === 500) {\n error = `Analytics platform returned an internal server error. Please check the platform logs.`;\n }\n\n console.info(`⚠️ Analytics platform rejected event: ${error}`);\n return Response.json(\n {\n error,\n },\n { status: 500 }\n );\n }\n console.info(\"🚀 Analytics event sent to platform!\");\n return Response.json({ message: \"Event forwarded successfully\" });\n } catch (e) {\n const error = ensureError(e);\n console.info(\"🚫 Internal error with sending analytics event.\");\n\n return Response.json(\n { error: `Error in analytics route handler: ${error.message}` },\n { status: 500 }\n );\n }\n };\n};\n\nexport const makeEventTracker =\n ({\n enabled = false,\n endpoint = \"/api/analytics\",\n }: {\n enabled?: boolean;\n endpoint?: string;\n }) =>\n async (\n event: RawEvent\n ): Promise<{\n error: string | null;\n success: boolean;\n }> => {\n if (!enabled) {\n console.log(\"Analytics disabled - event not sent.\");\n return { error: null, success: true };\n }\n\n const endpointWithHost = getBaseUrl() + endpoint;\n\n const eventWithTimeStamp: TrackableEvent = {\n ...event,\n timestamp: new Date().toJSON(),\n };\n\n try {\n const response = await fetch(endpointWithHost, {\n method: \"POST\",\n keepalive: true,\n body: JSON.stringify(eventWithTimeStamp),\n headers: {\n \"Content-Type\": \"application/json\",\n },\n });\n\n if (!response.ok) {\n if (response.status === 404) {\n return {\n error: `Analytics endpoint not found, did you forget to add the route?`,\n success: false,\n };\n }\n\n // createRouteHandler will return a 400 if the event failed schema validation.\n if (response.status === 400) {\n return {\n error: `Invalid event sent to analytics endpoint: ${response.statusText}`,\n success: false,\n };\n }\n\n // createRouteHandler will return a 500 for all error states\n return {\n error: `Internal server error when sending analytics event: ${response.statusText}. Check the route handler implementation.`,\n success: false,\n };\n }\n\n return { error: null, success: true };\n } catch (e) {\n const error = ensureError(e);\n return {\n error: `Internal error when sending analytics event: ${error.message}`,\n success: false,\n };\n }\n };\n"],"mappings":";AACO,SAAS,YAAY,OAAuB;AACjD,MAAI,CAAC;AAAO,WAAO,IAAI,MAAM,qBAAqB;AAElD,MAAI,iBAAiB;AAAO,WAAO;AAGnC,MAAI,MAAM,cAAc,KAAK;AAAG,WAAO;AAEvC,MAAI,cAAc;AAClB,MAAI;AACF,kBAAc,KAAK,UAAU,KAAK;AAAA,EACpC,QAAQ;AAAA,EAAC;AAET,QAAM,QAAQ,IAAI;AAAA,IAChB,sDAAsD,WAAW;AAAA,EACnE;AACA,SAAO;AACT;AAEO,SAAS,aAAa;AAC3B,MAAI,OAAO,WAAW;AAEpB,WAAO;AAET,MAAI,QAAQ,IAAI;AAEd,WAAO,WAAW,QAAQ,IAAI,UAAU;AAE1C,MAAI,QAAQ,IAAI;AAEd,WAAO,QAAQ,IAAI;AAGrB,SAAO;AACT;;;AChCA,OAAO,OAAO;AAGP,IAAM,aAAa;AAAA,EACxB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,IAAM,cAAc,EAAE,OAAO;AAAA,EAC3B,MAAM,EAAE,KAAK,UAAU;AACzB,CAAC;AAED,IAAM,cAAc,EAAE,OAAO;AAAA,EAC3B,MAAM,EAAE,QAAQ,OAAO;AAAA,EACvB,SAAS,EAAE,OAAO;AAAA,EAClB,MAAM,E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,YAAQ,IAAI,sCAAsC;AAClD,WAAO,EAAE,OAAO,MAAM,SAAS,KAAK;AAAA,EACtC;AAEA,QAAM,mBAAmB,WAAW,IAAI;AAExC,QAAM,qBAAqC;AAAA,IACzC,GAAG;AAAA,IACH,YAAW,oBAAI,KAAK,GAAE,OAAO;AAAA,EAC/B;AAEA,MAAI;AACF,UAAM,WAAW,MAAM,MAAM,kBAAkB;AAAA,MAC7C,QAAQ;AAAA,MACR,WAAW;AAAA,MACX,MAAM,KAAK,UAAU,kBAAkB;AAAA,MACvC,SAAS;AAAA,QACP,gBAAgB;AAAA,MAClB;AAAA,IACF,CAAC;AAED,QAAI,CAAC,SAAS,IAAI;AAChB,UAAI,SAAS,WAAW,KAAK;AAC3B,eAAO;AAAA,UACL,OAAO;AAAA,UACP,SAAS;AAAA,QACX;AAAA,MACF;AAGA,UAAI,SAAS,WAAW,KAAK;AAC3B,eAAO;AAAA,UACL,OAAO,6CAA6C,SAAS,UAAU;AAAA,UACvE,SAAS;AAAA,QACX;AAAA,MACF;AAGA,aAAO;AAAA,QACL,OAAO,uDAAuD,SAAS,UAAU;AAAA,QACjF,SAAS;AAAA,MACX;AAAA,IACF;AAEA,WAAO,EAAE,OAAO,MAAM,SAAS,KAAK;AAAA,EACtC,SAAS,GAAG;AACV,UAAM,QAAQ,YAAY,CAAC;AAC3B,WAAO;AAAA,MACL,OAAO,gDAAgD,MAAM,OAAO;AAAA,MACpE,SAAS;AAAA,IACX;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils.ts"],"sourcesContent":["import { NextResponse, type NextRequest } from 'next/server';\nimport { ensureError, getBaseUrl } from './utils';\nimport z from 'zod';\n\n// Todo: it would be great to work out a way to support arbitrary types here.\nexport const eventTypes = [\n 'AppSetup',\n 'ProtocolInstalled',\n 'InterviewStarted',\n 'InterviewCompleted',\n 'DataExported',\n] as const;\n\nconst EventSchema = z.object({\n type: z.enum(eventTypes),\n});\n\nconst ErrorSchema = z.object({\n type: z.literal('Error'),\n message: z.string(),\n name: z.string(),\n stack: z.string().optional(),\n cause: z.string().optional(),\n});\n\nconst SharedEventAndErrorSchema = z.object({\n metadata: z.record(z.unknown()).optional(),\n});\n\n/**\n * Raw events are the events that are sent trackEvent. They are either general\n * events or errors. We discriminate on the `type` property to determine which\n * schema to use, and then merge the shared properties.\n */\nexport const RawEventSchema = z.discriminatedUnion('type', [\n SharedEventAndErrorSchema.merge(EventSchema),\n SharedEventAndErrorSchema.merge(ErrorSchema),\n]);\nexport type RawEvent = z.infer<typeof RawEventSchema>;\n\n/**\n * Trackable events are the events that are sent to the route handler. The\n * `trackEvent` function adds the timestamp to ensure it is not inaccurate\n * due to network latency or processing time.\n */\nconst TrackablePropertiesSchema = z.object({\n timestamp: z.string(),\n});\n\nexport const TrackableEventSchema = z.intersection(\n RawEventSchema,\n TrackablePropertiesSchema,\n);\nexport type TrackableEvent = z.infer<typeof TrackableEventSchema>;\n\n/**\n * Dispatchable events are the events that are sent to the platform. The route\n * handler injects the installationId and countryISOCode properties.\n */\nconst DispatchablePropertiesSchema = z.object({\n installationId: z.string(),\n countryISOCode: z.string(),\n});\n\n/**\n * The final schema for an analytics event. This is the schema that is used to\n * validate the event before it is inserted into the database. It is the\n * intersection of the trackable event and the dispatchable properties.\n */\nexport const AnalyticsEventSchema = z.intersection(\n TrackableEventSchema,\n DispatchablePropertiesSchema,\n);\nexport type analyticsEvent = z.infer<typeof AnalyticsEventSchema>;\n\ntype GeoData = {\n status: 'success' | 'fail';\n countryCode: string;\n message: string;\n};\n\nexport const createRouteHandler = ({\n platformUrl = 'https://analytics.networkcanvas.com',\n installationId,\n}: {\n platformUrl?: string;\n installationId: string;\n}) => {\n return async (request: NextRequest) => {\n try {\n const incomingEvent = (await request.json()) as unknown;\n\n // Check if analytics is disabled\n // eslint-disable-next-line no-process-env, turbo/no-undeclared-env-vars\n if (process.env.DISABLE_ANALYTICS) {\n // eslint-disable-next-line no-console\n console.info('🛑 Analytics disabled. Payload not sent.');\n try {\n // eslint-disable-next-line no-console\n console.info(\n 'Payload:',\n '\\n',\n JSON.stringify(incomingEvent, null, 2),\n );\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error('Error stringifying payload:', e);\n }\n\n return NextResponse.json(\n { message: 'Analytics disabled' },\n { status: 200 },\n );\n }\n\n // Validate the event\n const trackableEvent = TrackableEventSchema.safeParse(incomingEvent);\n\n if (!trackableEvent.success) {\n // eslint-disable-next-line no-console\n console.error('Invalid event:', trackableEvent.error);\n return NextResponse.json({ error: 'Invalid event' }, { status: 400 });\n }\n\n // We don't want failures in third party services to prevent us from\n // tracking analytics events, so we'll catch any errors and log them\n // and continue with an 'Unknown' country code.\n let countryISOCode = 'Unknown';\n try {\n const ip = await fetch('https://api64.ipify.org').then((res) =>\n res.text(),\n );\n\n if (!ip) {\n throw new Error('Could not fetch IP address');\n }\n\n const geoData = (await fetch(`http://ip-api.com/json/${ip}`).then(\n (res) => res.json(),\n )) as GeoData;\n\n if (geoData.status === 'success') {\n countryISOCode = geoData.countryCode;\n } else {\n throw new Error(geoData.message);\n }\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error('Geolocation failed:', e);\n }\n\n const analyticsEvent: analyticsEvent = {\n ...trackableEvent.data,\n installationId,\n countryISOCode,\n };\n\n // Forward to backend\n const response = await fetch(`${platformUrl}/api/event`, {\n keepalive: true,\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(analyticsEvent),\n });\n\n if (!response.ok) {\n let error = `Analytics platform returned an unexpected error: ${response.statusText}`;\n\n if (response.status === 400) {\n error = `Analytics platform rejected the event as invalid. Please check the event schema`;\n }\n\n if (response.status === 404) {\n error = `Analytics platform could not be reached. Please specify a valid platform URL, or check that the platform is online.`;\n }\n\n if (response.status === 500) {\n error = `Analytics platform returned an internal server error. Please check the platform logs.`;\n }\n\n // eslint-disable-next-line no-console\n console.info(`⚠️ Analytics platform rejected event: ${error}`);\n return Response.json(\n {\n error,\n },\n { status: 500 },\n );\n }\n // eslint-disable-next-line no-console\n console.info('🚀 Analytics event sent to platform!');\n return Response.json({ message: 'Event forwarded successfully' });\n } catch (e) {\n const error = ensureError(e);\n // eslint-disable-next-line no-console\n console.info('🚫 Internal error with sending analytics event.');\n\n return Response.json(\n { error: `Error in analytics route handler: ${error.message}` },\n { status: 500 },\n );\n }\n };\n};\n\nexport const makeEventTracker =\n ({ endpoint = '/api/analytics' }: { endpoint?: string }) =>\n async (\n event: RawEvent,\n ): Promise<{\n error: string | null;\n success: boolean;\n }> => {\n const endpointWithHost = getBaseUrl() + endpoint;\n\n const eventWithTimeStamp: TrackableEvent = {\n ...event,\n timestamp: new Date().toJSON(),\n };\n\n try {\n const response = await fetch(endpointWithHost, {\n method: 'POST',\n keepalive: true,\n body: JSON.stringify(eventWithTimeStamp),\n headers: {\n 'Content-Type': 'application/json',\n },\n });\n\n if (!response.ok) {\n if (response.status === 404) {\n return {\n error: `Analytics endpoint not found, did you forget to add the route?`,\n success: false,\n };\n }\n\n // createRouteHandler will return a 400 if the event failed schema validation.\n if (response.status === 400) {\n return {\n error: `Invalid event sent to analytics endpoint: ${response.statusText}`,\n success: false,\n };\n }\n\n // createRouteHandler will return a 500 for all error states\n return {\n error: `Internal server error when sending analytics event: ${response.statusText}. Check the route handler implementation.`,\n success: false,\n };\n }\n\n return { error: null, success: true };\n } catch (e) {\n const error = ensureError(e);\n return {\n error: `Internal error when sending analytics event: ${error.message}`,\n success: false,\n };\n }\n };\n","/* eslint-disable no-process-env */\n// Helper function that ensures that a value is an Error\nexport function ensureError(value: unknown): Error {\n if (!value) return new Error('No value was thrown');\n\n if (value instanceof Error) return value;\n\n // Test if value inherits from Error\n if (Object.prototype.isPrototypeOf.call(value, Error))\n return value as Error & typeof value;\n\n let stringified = '[Unable to stringify the thrown value]';\n try {\n stringified = JSON.stringify(value);\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error(e);\n }\n\n const error = new Error(\n `This value was thrown as is, not through an Error: ${stringified}`,\n );\n return error;\n}\n\nexport function getBaseUrl() {\n if (typeof window !== 'undefined')\n // browser should use relative path\n return '';\n\n if (process.env.VERCEL_URL)\n // reference for vercel.com\n return `https://${process.env.VERCEL_URL}`;\n\n if (process.env.NEXT_PUBLIC_URL)\n // Manually set deployment URL from env\n return process.env.NEXT_PUBLIC_URL;\n\n // assume localhost\n return `http://127.0.0.1:3000`;\n}\n"],"mappings":";AAAA,SAAS,oBAAsC;;;ACExC,SAAS,YAAY,OAAuB;AACjD,MAAI,CAAC;AAAO,WAAO,IAAI,MAAM,qBAAqB;AAElD,MAAI,iBAAiB;AAAO,WAAO;AAGnC,MAAI,OAAO,UAAU,cAAc,KAAK,OAAO,KAAK;AAClD,WAAO;AAET,MAAI,cAAc;AAClB,MAAI;AACF,kBAAc,KAAK,UAAU,KAAK;AAAA,EACpC,SAAS,GAAG;AAEV,YAAQ,MAAM,CAAC;AAAA,EACjB;AAEA,QAAM,QAAQ,IAAI;AAAA,IAChB,sDAAsD,WAAW;AAAA,EACnE;AACA,SAAO;AACT;AAEO,SAAS,aAAa;AAC3B,MAAI,OAAO,WAAW;AAEpB,WAAO;AAET,MAAI,QAAQ,IAAI;AAEd,WAAO,WAAW,QAAQ,IAAI,UAAU;AAE1C,MAAI,QAAQ,IAAI;AAEd,WAAO,QAAQ,IAAI;AAGrB,SAAO;AACT;;;ADtCA,OAAO,OAAO;AAGP,IAAM,aAAa;AAAA,EACxB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,IAAM,cAAc,EAAE,OAAO;AAAA,EAC3B,MAAM,EAAE,KAAK,UAAU;AACzB,CAAC;AAED,IAAM,cAAc,EAAE,OAAO;AAAA,EAC3B,MAAM,EAAE,QAAQ,OAAO;AAAA,EACvB,SAAS,EAAE,OAAO;AAAA,EAClB,MAAM,EAAE,OAAO;AAAA,EACf,OAAO,EAAE,OAAO,EAAE,SAAS;AAAA,EAC3B,OAAO,EAAE,OAAO,EAAE,SA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6.0.0-alpha.1",
|
|
4
|
+
"packageManager": "pnpm@8.15.5+sha256.4b4efa12490e5055d59b9b9fc9438b7d581a6b7af3b5675eb5c5f447cee1a589",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"main": "./dist/index.js",
|
|
6
7
|
"types": "./dist/index.d.ts",
|
|
7
|
-
"author": "Complex Data Collective <
|
|
8
|
+
"author": "Complex Data Collective <hello@complexdatacollective.org>",
|
|
8
9
|
"description": "Utilities for tracking analytics and error reporting in Fresco",
|
|
9
10
|
"scripts": {
|
|
10
11
|
"build": "tsup src/index.ts --format esm --dts --clean --sourcemap",
|
|
@@ -12,16 +13,25 @@
|
|
|
12
13
|
"dev": "npm run build -- --watch"
|
|
13
14
|
},
|
|
14
15
|
"peerDependencies": {
|
|
15
|
-
"
|
|
16
|
-
"next": "13 || 14"
|
|
16
|
+
"next": "13 || 14 || 15"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"eslint-config
|
|
20
|
-
"tsconfig": "workspace:*",
|
|
21
|
-
"
|
|
22
|
-
"
|
|
19
|
+
"@codaco/eslint-config": "workspace:*",
|
|
20
|
+
"@codaco/tsconfig": "workspace:*",
|
|
21
|
+
"eslint": "^8.57.0",
|
|
22
|
+
"prettier": "^3.2.5",
|
|
23
|
+
"tsup": "^8.0.2",
|
|
24
|
+
"typescript": "^5.4.5"
|
|
23
25
|
},
|
|
24
26
|
"dependencies": {
|
|
25
|
-
"zod": "^3.
|
|
27
|
+
"zod": "^3.23.8"
|
|
28
|
+
},
|
|
29
|
+
"eslintConfig": {
|
|
30
|
+
"root": true,
|
|
31
|
+
"extends": [
|
|
32
|
+
"@codaco/eslint-config/base",
|
|
33
|
+
"@codaco/eslint-config/nextjs",
|
|
34
|
+
"@codaco/eslint-config/react"
|
|
35
|
+
]
|
|
26
36
|
}
|
|
27
37
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import { type NextRequest } from
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import z from "zod";
|
|
1
|
+
import { NextResponse, type NextRequest } from 'next/server';
|
|
2
|
+
import { ensureError, getBaseUrl } from './utils';
|
|
3
|
+
import z from 'zod';
|
|
5
4
|
|
|
6
5
|
// Todo: it would be great to work out a way to support arbitrary types here.
|
|
7
6
|
export const eventTypes = [
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
'AppSetup',
|
|
8
|
+
'ProtocolInstalled',
|
|
9
|
+
'InterviewStarted',
|
|
10
|
+
'InterviewCompleted',
|
|
11
|
+
'DataExported',
|
|
13
12
|
] as const;
|
|
14
13
|
|
|
15
14
|
const EventSchema = z.object({
|
|
@@ -17,7 +16,7 @@ const EventSchema = z.object({
|
|
|
17
16
|
});
|
|
18
17
|
|
|
19
18
|
const ErrorSchema = z.object({
|
|
20
|
-
type: z.literal(
|
|
19
|
+
type: z.literal('Error'),
|
|
21
20
|
message: z.string(),
|
|
22
21
|
name: z.string(),
|
|
23
22
|
stack: z.string().optional(),
|
|
@@ -33,7 +32,7 @@ const SharedEventAndErrorSchema = z.object({
|
|
|
33
32
|
* events or errors. We discriminate on the `type` property to determine which
|
|
34
33
|
* schema to use, and then merge the shared properties.
|
|
35
34
|
*/
|
|
36
|
-
export const RawEventSchema = z.discriminatedUnion(
|
|
35
|
+
export const RawEventSchema = z.discriminatedUnion('type', [
|
|
37
36
|
SharedEventAndErrorSchema.merge(EventSchema),
|
|
38
37
|
SharedEventAndErrorSchema.merge(ErrorSchema),
|
|
39
38
|
]);
|
|
@@ -50,7 +49,7 @@ const TrackablePropertiesSchema = z.object({
|
|
|
50
49
|
|
|
51
50
|
export const TrackableEventSchema = z.intersection(
|
|
52
51
|
RawEventSchema,
|
|
53
|
-
TrackablePropertiesSchema
|
|
52
|
+
TrackablePropertiesSchema,
|
|
54
53
|
);
|
|
55
54
|
export type TrackableEvent = z.infer<typeof TrackableEventSchema>;
|
|
56
55
|
|
|
@@ -70,53 +69,84 @@ const DispatchablePropertiesSchema = z.object({
|
|
|
70
69
|
*/
|
|
71
70
|
export const AnalyticsEventSchema = z.intersection(
|
|
72
71
|
TrackableEventSchema,
|
|
73
|
-
DispatchablePropertiesSchema
|
|
72
|
+
DispatchablePropertiesSchema,
|
|
74
73
|
);
|
|
75
74
|
export type analyticsEvent = z.infer<typeof AnalyticsEventSchema>;
|
|
76
75
|
|
|
76
|
+
type GeoData = {
|
|
77
|
+
status: 'success' | 'fail';
|
|
78
|
+
countryCode: string;
|
|
79
|
+
message: string;
|
|
80
|
+
};
|
|
81
|
+
|
|
77
82
|
export const createRouteHandler = ({
|
|
78
|
-
platformUrl =
|
|
83
|
+
platformUrl = 'https://analytics.networkcanvas.com',
|
|
79
84
|
installationId,
|
|
80
|
-
maxMindClient,
|
|
81
85
|
}: {
|
|
82
86
|
platformUrl?: string;
|
|
83
87
|
installationId: string;
|
|
84
|
-
maxMindClient: WebServiceClient;
|
|
85
88
|
}) => {
|
|
86
89
|
return async (request: NextRequest) => {
|
|
87
90
|
try {
|
|
88
91
|
const incomingEvent = (await request.json()) as unknown;
|
|
89
92
|
|
|
93
|
+
// Check if analytics is disabled
|
|
94
|
+
// eslint-disable-next-line no-process-env, turbo/no-undeclared-env-vars
|
|
95
|
+
if (process.env.DISABLE_ANALYTICS) {
|
|
96
|
+
// eslint-disable-next-line no-console
|
|
97
|
+
console.info('🛑 Analytics disabled. Payload not sent.');
|
|
98
|
+
try {
|
|
99
|
+
// eslint-disable-next-line no-console
|
|
100
|
+
console.info(
|
|
101
|
+
'Payload:',
|
|
102
|
+
'\n',
|
|
103
|
+
JSON.stringify(incomingEvent, null, 2),
|
|
104
|
+
);
|
|
105
|
+
} catch (e) {
|
|
106
|
+
// eslint-disable-next-line no-console
|
|
107
|
+
console.error('Error stringifying payload:', e);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return NextResponse.json(
|
|
111
|
+
{ message: 'Analytics disabled' },
|
|
112
|
+
{ status: 200 },
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
90
116
|
// Validate the event
|
|
91
117
|
const trackableEvent = TrackableEventSchema.safeParse(incomingEvent);
|
|
92
118
|
|
|
93
119
|
if (!trackableEvent.success) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
headers: {
|
|
98
|
-
"Content-Type": "application/json",
|
|
99
|
-
},
|
|
100
|
-
});
|
|
120
|
+
// eslint-disable-next-line no-console
|
|
121
|
+
console.error('Invalid event:', trackableEvent.error);
|
|
122
|
+
return NextResponse.json({ error: 'Invalid event' }, { status: 400 });
|
|
101
123
|
}
|
|
102
124
|
|
|
103
125
|
// We don't want failures in third party services to prevent us from
|
|
104
126
|
// tracking analytics events, so we'll catch any errors and log them
|
|
105
127
|
// and continue with an 'Unknown' country code.
|
|
106
|
-
let countryISOCode =
|
|
128
|
+
let countryISOCode = 'Unknown';
|
|
107
129
|
try {
|
|
108
|
-
const ip = await fetch(
|
|
109
|
-
res.text()
|
|
130
|
+
const ip = await fetch('https://api64.ipify.org').then((res) =>
|
|
131
|
+
res.text(),
|
|
110
132
|
);
|
|
111
133
|
|
|
112
134
|
if (!ip) {
|
|
113
|
-
throw new Error(
|
|
135
|
+
throw new Error('Could not fetch IP address');
|
|
114
136
|
}
|
|
115
137
|
|
|
116
|
-
const
|
|
117
|
-
|
|
138
|
+
const geoData = (await fetch(`http://ip-api.com/json/${ip}`).then(
|
|
139
|
+
(res) => res.json(),
|
|
140
|
+
)) as GeoData;
|
|
141
|
+
|
|
142
|
+
if (geoData.status === 'success') {
|
|
143
|
+
countryISOCode = geoData.countryCode;
|
|
144
|
+
} else {
|
|
145
|
+
throw new Error(geoData.message);
|
|
146
|
+
}
|
|
118
147
|
} catch (e) {
|
|
119
|
-
|
|
148
|
+
// eslint-disable-next-line no-console
|
|
149
|
+
console.error('Geolocation failed:', e);
|
|
120
150
|
}
|
|
121
151
|
|
|
122
152
|
const analyticsEvent: analyticsEvent = {
|
|
@@ -128,9 +158,9 @@ export const createRouteHandler = ({
|
|
|
128
158
|
// Forward to backend
|
|
129
159
|
const response = await fetch(`${platformUrl}/api/event`, {
|
|
130
160
|
keepalive: true,
|
|
131
|
-
method:
|
|
161
|
+
method: 'POST',
|
|
132
162
|
headers: {
|
|
133
|
-
|
|
163
|
+
'Content-Type': 'application/json',
|
|
134
164
|
},
|
|
135
165
|
body: JSON.stringify(analyticsEvent),
|
|
136
166
|
});
|
|
@@ -150,47 +180,39 @@ export const createRouteHandler = ({
|
|
|
150
180
|
error = `Analytics platform returned an internal server error. Please check the platform logs.`;
|
|
151
181
|
}
|
|
152
182
|
|
|
183
|
+
// eslint-disable-next-line no-console
|
|
153
184
|
console.info(`⚠️ Analytics platform rejected event: ${error}`);
|
|
154
185
|
return Response.json(
|
|
155
186
|
{
|
|
156
187
|
error,
|
|
157
188
|
},
|
|
158
|
-
{ status: 500 }
|
|
189
|
+
{ status: 500 },
|
|
159
190
|
);
|
|
160
191
|
}
|
|
161
|
-
|
|
162
|
-
|
|
192
|
+
// eslint-disable-next-line no-console
|
|
193
|
+
console.info('🚀 Analytics event sent to platform!');
|
|
194
|
+
return Response.json({ message: 'Event forwarded successfully' });
|
|
163
195
|
} catch (e) {
|
|
164
196
|
const error = ensureError(e);
|
|
165
|
-
|
|
197
|
+
// eslint-disable-next-line no-console
|
|
198
|
+
console.info('🚫 Internal error with sending analytics event.');
|
|
166
199
|
|
|
167
200
|
return Response.json(
|
|
168
201
|
{ error: `Error in analytics route handler: ${error.message}` },
|
|
169
|
-
{ status: 500 }
|
|
202
|
+
{ status: 500 },
|
|
170
203
|
);
|
|
171
204
|
}
|
|
172
205
|
};
|
|
173
206
|
};
|
|
174
207
|
|
|
175
208
|
export const makeEventTracker =
|
|
176
|
-
({
|
|
177
|
-
enabled = false,
|
|
178
|
-
endpoint = "/api/analytics",
|
|
179
|
-
}: {
|
|
180
|
-
enabled?: boolean;
|
|
181
|
-
endpoint?: string;
|
|
182
|
-
}) =>
|
|
209
|
+
({ endpoint = '/api/analytics' }: { endpoint?: string }) =>
|
|
183
210
|
async (
|
|
184
|
-
event: RawEvent
|
|
211
|
+
event: RawEvent,
|
|
185
212
|
): Promise<{
|
|
186
213
|
error: string | null;
|
|
187
214
|
success: boolean;
|
|
188
215
|
}> => {
|
|
189
|
-
if (!enabled) {
|
|
190
|
-
console.log("Analytics disabled - event not sent.");
|
|
191
|
-
return { error: null, success: true };
|
|
192
|
-
}
|
|
193
|
-
|
|
194
216
|
const endpointWithHost = getBaseUrl() + endpoint;
|
|
195
217
|
|
|
196
218
|
const eventWithTimeStamp: TrackableEvent = {
|
|
@@ -200,11 +222,11 @@ export const makeEventTracker =
|
|
|
200
222
|
|
|
201
223
|
try {
|
|
202
224
|
const response = await fetch(endpointWithHost, {
|
|
203
|
-
method:
|
|
225
|
+
method: 'POST',
|
|
204
226
|
keepalive: true,
|
|
205
227
|
body: JSON.stringify(eventWithTimeStamp),
|
|
206
228
|
headers: {
|
|
207
|
-
|
|
229
|
+
'Content-Type': 'application/json',
|
|
208
230
|
},
|
|
209
231
|
});
|
|
210
232
|
|
package/src/utils.ts
CHANGED
|
@@ -1,27 +1,32 @@
|
|
|
1
|
+
/* eslint-disable no-process-env */
|
|
1
2
|
// Helper function that ensures that a value is an Error
|
|
2
3
|
export function ensureError(value: unknown): Error {
|
|
3
|
-
if (!value) return new Error(
|
|
4
|
+
if (!value) return new Error('No value was thrown');
|
|
4
5
|
|
|
5
6
|
if (value instanceof Error) return value;
|
|
6
7
|
|
|
7
8
|
// Test if value inherits from Error
|
|
8
|
-
if (
|
|
9
|
+
if (Object.prototype.isPrototypeOf.call(value, Error))
|
|
10
|
+
return value as Error & typeof value;
|
|
9
11
|
|
|
10
|
-
let stringified =
|
|
12
|
+
let stringified = '[Unable to stringify the thrown value]';
|
|
11
13
|
try {
|
|
12
14
|
stringified = JSON.stringify(value);
|
|
13
|
-
} catch {
|
|
15
|
+
} catch (e) {
|
|
16
|
+
// eslint-disable-next-line no-console
|
|
17
|
+
console.error(e);
|
|
18
|
+
}
|
|
14
19
|
|
|
15
20
|
const error = new Error(
|
|
16
|
-
`This value was thrown as is, not through an Error: ${stringified}
|
|
21
|
+
`This value was thrown as is, not through an Error: ${stringified}`,
|
|
17
22
|
);
|
|
18
23
|
return error;
|
|
19
24
|
}
|
|
20
25
|
|
|
21
26
|
export function getBaseUrl() {
|
|
22
|
-
if (typeof window !==
|
|
27
|
+
if (typeof window !== 'undefined')
|
|
23
28
|
// browser should use relative path
|
|
24
|
-
return
|
|
29
|
+
return '';
|
|
25
30
|
|
|
26
31
|
if (process.env.VERCEL_URL)
|
|
27
32
|
// reference for vercel.com
|
package/tsconfig.json
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
{
|
|
2
|
-
"extends": "tsconfig/
|
|
3
|
-
"
|
|
4
|
-
"."
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
]
|
|
11
|
-
}
|
|
2
|
+
"extends": "@codaco/tsconfig/base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": ".",
|
|
5
|
+
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
|
|
6
|
+
},
|
|
7
|
+
"include": ["."],
|
|
8
|
+
"exclude": ["dist", "build", "node_modules"]
|
|
9
|
+
}
|
package/.eslintrc.js
DELETED