@codaco/analytics 0.2.0-alpha → 0.4.0-alpha
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 +6 -6
- package/.turbo/turbo-lint.log +1 -1
- package/dist/index.d.mts +0 -2
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +9 -15
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @codaco/analytics@0.
|
|
2
|
+
> @codaco/analytics@0.4.0-alpha build /Users/buckhalt/Code/complexdatacollective/error-analytics-microservice/packages/analytics
|
|
3
3
|
> tsup src/index.ts --format esm --dts --minify --clean --sourcemap
|
|
4
4
|
|
|
5
5
|
CLI Building entry: src/index.ts
|
|
@@ -8,9 +8,9 @@ CLI tsup v7.2.0
|
|
|
8
8
|
CLI Target: es2022
|
|
9
9
|
CLI Cleaning output folder
|
|
10
10
|
ESM Build start
|
|
11
|
-
ESM dist/index.mjs 1.
|
|
12
|
-
ESM dist/index.mjs.map 6.
|
|
13
|
-
ESM ⚡️ Build success in
|
|
11
|
+
ESM dist/index.mjs 1.84 KB
|
|
12
|
+
ESM dist/index.mjs.map 6.61 KB
|
|
13
|
+
ESM ⚡️ Build success in 43ms
|
|
14
14
|
DTS Build start
|
|
15
|
-
DTS ⚡️ Build success in
|
|
16
|
-
DTS dist/index.d.mts 1.
|
|
15
|
+
DTS ⚡️ Build success in 738ms
|
|
16
|
+
DTS dist/index.d.mts 1.49 KB
|
package/.turbo/turbo-lint.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @codaco/analytics@0.
|
|
2
|
+
> @codaco/analytics@0.4.0-alpha lint /Users/buckhalt/Code/complexdatacollective/error-analytics-microservice/packages/analytics
|
|
3
3
|
> eslint .
|
|
4
4
|
|
|
5
5
|
Pages directory cannot be found at /Users/buckhalt/Code/complexdatacollective/error-analytics-microservice/packages/analytics/pages or /Users/buckhalt/Code/complexdatacollective/error-analytics-microservice/packages/analytics/src/pages. If using a custom path, please configure with the `no-html-link-for-pages` rule in your eslint config file.
|
package/dist/index.d.mts
CHANGED
|
@@ -14,7 +14,6 @@ type AnalyticsEvent = AnalyticsEventBase & {
|
|
|
14
14
|
type AnalyticsError = AnalyticsEventBase & {
|
|
15
15
|
type: "Error";
|
|
16
16
|
error: {
|
|
17
|
-
code: number;
|
|
18
17
|
message: string;
|
|
19
18
|
details: string;
|
|
20
19
|
stacktrace: string;
|
|
@@ -40,7 +39,6 @@ declare class AnalyticsClient {
|
|
|
40
39
|
private processEvent;
|
|
41
40
|
trackEvent(payload: AnalyticsEventOrError): void;
|
|
42
41
|
setInstallationId(installationId: string): void;
|
|
43
|
-
enable(): void;
|
|
44
42
|
disable(): void;
|
|
45
43
|
get isEnabled(): boolean;
|
|
46
44
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{queue as
|
|
1
|
+
import{queue as s}from"async";var o=class{platformUrl="https://analytics.networkcanvas.dev";installationId=null;dispatchQueue;enabled=!0;constructor(e){e.platformUrl&&(this.platformUrl=e.platformUrl),this.dispatchQueue=s(this.processEvent,1),this.dispatchQueue.pause()}createRouteHandler=e=>async t=>{let n=(t.headers.get("x-forwarded-for")??"127.0.0.1").split(",")[0];if(n==="::1")return new Response(null,{status:200});if(!n)return console.error("No IP address provided for geolocation"),new Response(null,{status:500});try{let r=await e.country(n);return new Response(r?.country?.isoCode,{headers:{"Content-Type":"application/json"}})}catch(r){return console.error("Error getting country code",r),new Response(null,{status:500})}};processEvent=async e=>{try{let t=await fetch("api/analytics/geolocate");t.ok||console.error("Geolocation request failed");let n=await t.text(),r={...e,installationId:this.installationId??"",timestamp:new Date,geolocation:{countryCode:n??""}};if(!(await fetch(`${this.platformUrl}/api/event`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)})).ok){console.info(`\u{1F6AB} Event "${r.type}" failed to send to analytics microservice.`);return}console.info(`\u{1F680} Event "${r.type}" successfully sent to analytics microservice`)}catch(t){console.error("Error sending event to analytics microservice",t);return}};trackEvent(e){console.info(`\u{1F560} Event ${e.type} queued for dispatch...`),this.dispatchQueue.push(e)}setInstallationId(e){if(this.enabled)try{this.installationId=e,this.dispatchQueue.resume(),console.info("\u{1F4C8} Analytics queue resumed")}catch(t){console.error("Error setting installation ID",t)}}disable(){console.info("\u{1F4C8} Analytics disabled"),this.enabled=!1,this.dispatchQueue.pause()}get isEnabled(){return this.enabled}};export{o as AnalyticsClient};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { WebServiceClient } from \"@maxmind/geoip2-node\";\nimport { QueueObject, queue } from \"async\";\nimport type { NextRequest } from \"next/server\";\n\ntype GeoLocation = {\n countryCode: string;\n};\n\nexport type AnalyticsEventBase = {\n type:\n | \"InterviewCompleted\"\n | \"InterviewStarted\"\n | \"ProtocolInstalled\"\n | \"AppSetup\"\n | \"Error\";\n};\n\nexport type AnalyticsEvent = AnalyticsEventBase & {\n type:\n | \"InterviewCompleted\"\n | \"InterviewStarted\"\n | \"ProtocolInstalled\"\n | \"AppSetup\";\n metadata?: Record<string, unknown>;\n};\n\nexport type AnalyticsError = AnalyticsEventBase & {\n type: \"Error\";\n error: {\n
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { WebServiceClient } from \"@maxmind/geoip2-node\";\nimport { QueueObject, queue } from \"async\";\nimport type { NextRequest } from \"next/server\";\n\ntype GeoLocation = {\n countryCode: string;\n};\n\nexport type AnalyticsEventBase = {\n type:\n | \"InterviewCompleted\"\n | \"InterviewStarted\"\n | \"ProtocolInstalled\"\n | \"AppSetup\"\n | \"Error\";\n};\n\nexport type AnalyticsEvent = AnalyticsEventBase & {\n type:\n | \"InterviewCompleted\"\n | \"InterviewStarted\"\n | \"ProtocolInstalled\"\n | \"AppSetup\";\n metadata?: Record<string, unknown>;\n};\n\nexport type AnalyticsError = AnalyticsEventBase & {\n type: \"Error\";\n error: {\n message: string;\n details: string;\n stacktrace: string;\n path: string;\n };\n};\n\nexport type AnalyticsEventOrError = AnalyticsEvent | AnalyticsError;\n\nexport type DispatchableAnalyticsEvent = AnalyticsEventOrError & {\n installationId: string;\n geolocation?: GeoLocation;\n timestamp: Date;\n};\n\ntype AnalyticsClientConfiguration = {\n platformUrl?: string;\n};\n\nexport class AnalyticsClient {\n private platformUrl?: string = \"https://analytics.networkcanvas.dev\";\n private installationId: string | null = null;\n\n private dispatchQueue: QueueObject<AnalyticsEventOrError>;\n\n private enabled: boolean = true;\n\n constructor(configuration: AnalyticsClientConfiguration) {\n if (configuration.platformUrl) {\n this.platformUrl = configuration.platformUrl;\n }\n\n this.dispatchQueue = queue(this.processEvent, 1);\n this.dispatchQueue.pause(); // Start the queue paused so we can set the installation ID\n }\n\n public createRouteHandler =\n (maxMindClient: WebServiceClient) =>\n async (req: NextRequest): Promise<Response> => {\n const ip = (req.headers.get(\"x-forwarded-for\") ?? \"127.0.0.1\").split(\n \",\"\n )[0];\n\n if (ip === \"::1\") {\n // This is a localhost request, so we can't geolocate it.\n return new Response(null, { status: 200 });\n }\n\n if (!ip) {\n console.error(\"No IP address provided for geolocation\");\n return new Response(null, { status: 500 });\n }\n\n try {\n const response = await maxMindClient.country(ip);\n\n return new Response(response?.country?.isoCode, {\n headers: {\n \"Content-Type\": \"application/json\",\n },\n });\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error(\"Error getting country code\", error);\n return new Response(null, { status: 500 });\n }\n };\n\n private processEvent = async (event: AnalyticsEventOrError) => {\n // Todo: we need to think about client vs server geolocation. If we want\n // client, does this get us that? If we want server, we can get it once,\n // and simply store it.\n // Todo: use fetchWithZod?\n try {\n const response = await fetch(\"api/analytics/geolocate\");\n if (!response.ok) {\n console.error(\"Geolocation request failed\");\n }\n\n const countryCode: string | null = await response.text();\n\n const eventWithRequiredProperties: DispatchableAnalyticsEvent = {\n ...event,\n installationId: this.installationId ?? \"\",\n timestamp: new Date(),\n geolocation: {\n countryCode: countryCode ?? \"\",\n },\n };\n\n // We could validate against a zod schema here.\n\n // Send event to microservice.\n\n const result = await fetch(`${this.platformUrl}/api/event`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(eventWithRequiredProperties),\n });\n\n if (!result.ok) {\n console.info(\n `🚫 Event \"${eventWithRequiredProperties.type}\" failed to send to analytics microservice.`\n );\n return;\n }\n\n console.info(\n `🚀 Event \"${eventWithRequiredProperties.type}\" successfully sent to analytics microservice`\n );\n } catch (error) {\n console.error(\"Error sending event to analytics microservice\", error);\n return;\n }\n };\n\n public trackEvent(payload: AnalyticsEventOrError) {\n console.info(`🕠 Event ${payload.type} queued for dispatch...`);\n this.dispatchQueue.push(payload);\n }\n\n public setInstallationId(installationId: string) {\n if (this.enabled) {\n try {\n this.installationId = installationId;\n this.dispatchQueue.resume();\n console.info(\"📈 Analytics queue resumed\");\n } catch (error) {\n console.error(\"Error setting installation ID\", error);\n }\n }\n }\n\n public disable() {\n console.info(\"📈 Analytics disabled\");\n this.enabled = false;\n this.dispatchQueue.pause();\n }\n\n get isEnabled() {\n return this.enabled;\n }\n}\n"],"mappings":"AACA,OAAsB,SAAAA,MAAa,QA+C5B,IAAMC,EAAN,KAAsB,CACnB,YAAuB,sCACvB,eAAgC,KAEhC,cAEA,QAAmB,GAE3B,YAAYC,EAA6C,CACnDA,EAAc,cAChB,KAAK,YAAcA,EAAc,aAGnC,KAAK,cAAgBF,EAAM,KAAK,aAAc,CAAC,EAC/C,KAAK,cAAc,MAAM,CAC3B,CAEO,mBACJG,GACD,MAAOC,GAAwC,CAC7C,IAAMC,GAAMD,EAAI,QAAQ,IAAI,iBAAiB,GAAK,aAAa,MAC7D,GACF,EAAE,CAAC,EAE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1B,QAAQ,KAAK,mCAA4B,CAC3C,OAASL,EAAO,CACd,QAAQ,MAAM,gCAAiCA,CAAK,CACtD,CAEJ,CAEO,SAAU,CACf,QAAQ,KAAK,8BAAuB,EACpC,KAAK,QAAU,GACf,KAAK,cAAc,MAAM,CAC3B,CAEA,IAAI,WAAY,CACd,OAAO,KAAK,OACd,CACF","names":["queue","AnalyticsClient","configuration","maxMindClient","req","ip","response","error","event","countryCode","eventWithRequiredProperties","payload","installationId"]}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -27,7 +27,6 @@ export type AnalyticsEvent = AnalyticsEventBase & {
|
|
|
27
27
|
export type AnalyticsError = AnalyticsEventBase & {
|
|
28
28
|
type: "Error";
|
|
29
29
|
error: {
|
|
30
|
-
code: number;
|
|
31
30
|
message: string;
|
|
32
31
|
details: string;
|
|
33
32
|
stacktrace: string;
|
|
@@ -53,7 +52,7 @@ export class AnalyticsClient {
|
|
|
53
52
|
|
|
54
53
|
private dispatchQueue: QueueObject<AnalyticsEventOrError>;
|
|
55
54
|
|
|
56
|
-
private enabled: boolean =
|
|
55
|
+
private enabled: boolean = true;
|
|
57
56
|
|
|
58
57
|
constructor(configuration: AnalyticsClientConfiguration) {
|
|
59
58
|
if (configuration.platformUrl) {
|
|
@@ -101,7 +100,6 @@ export class AnalyticsClient {
|
|
|
101
100
|
// client, does this get us that? If we want server, we can get it once,
|
|
102
101
|
// and simply store it.
|
|
103
102
|
// Todo: use fetchWithZod?
|
|
104
|
-
|
|
105
103
|
try {
|
|
106
104
|
const response = await fetch("api/analytics/geolocate");
|
|
107
105
|
if (!response.ok) {
|
|
@@ -153,19 +151,15 @@ export class AnalyticsClient {
|
|
|
153
151
|
}
|
|
154
152
|
|
|
155
153
|
public setInstallationId(installationId: string) {
|
|
156
|
-
this.
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
"
|
|
163
|
-
|
|
154
|
+
if (this.enabled) {
|
|
155
|
+
try {
|
|
156
|
+
this.installationId = installationId;
|
|
157
|
+
this.dispatchQueue.resume();
|
|
158
|
+
console.info("📈 Analytics queue resumed");
|
|
159
|
+
} catch (error) {
|
|
160
|
+
console.error("Error setting installation ID", error);
|
|
161
|
+
}
|
|
164
162
|
}
|
|
165
|
-
|
|
166
|
-
console.info("📈 Analytics enabled");
|
|
167
|
-
this.enabled = true;
|
|
168
|
-
this.dispatchQueue.resume();
|
|
169
163
|
}
|
|
170
164
|
|
|
171
165
|
public disable() {
|