@codaco/analytics 0.2.0-alpha → 0.3.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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @codaco/analytics@0.2.0-alpha build /Users/buckhalt/Code/complexdatacollective/error-analytics-microservice/packages/analytics
2
+ > @codaco/analytics@0.3.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
@@ -9,8 +9,8 @@ CLI Target: es2022
9
9
  CLI Cleaning output folder
10
10
  ESM Build start
11
11
  ESM dist/index.mjs 1.89 KB
12
- ESM dist/index.mjs.map 6.66 KB
13
- ESM ⚡️ Build success in 64ms
12
+ ESM dist/index.mjs.map 6.64 KB
13
+ ESM ⚡️ Build success in 48ms
14
14
  DTS Build start
15
- DTS ⚡️ Build success in 894ms
16
- DTS dist/index.d.mts 1.54 KB
15
+ DTS ⚡️ Build success in 735ms
16
+ DTS dist/index.d.mts 1.51 KB
@@ -1,5 +1,5 @@
1
1
 
2
- > @codaco/analytics@0.2.0-alpha lint /Users/buckhalt/Code/complexdatacollective/error-analytics-microservice/packages/analytics
2
+ > @codaco/analytics@0.3.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;
@@ -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 code: number;\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 = false;\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\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 this.installationId = installationId;\n }\n\n public enable() {\n if (!this.installationId) {\n console.error(\n \"🚫 An installation ID must be set before you can enable analytics\"\n );\n }\n\n console.info(\"📈 Analytics enabled\");\n this.enabled = true;\n this.dispatchQueue.resume();\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,S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B,IAAI,6CAC/C,EACA,MACF,CAEA,QAAQ,KACN,oBAAaA,EAA4B,IAAI,+CAC/C,CACF,OAASH,EAAO,CACd,QAAQ,MAAM,gDAAiDA,CAAK,EACpE,MACF,CACF,EAEO,WAAWI,EAAgC,CAChD,QAAQ,KAAK,mBAAYA,EAAQ,IAAI,yBAAyB,EAC9D,KAAK,cAAc,KAAKA,CAAO,CACjC,CAEO,kBAAkBC,EAAwB,CAC/C,KAAK,eAAiBA,CACxB,CAEO,QAAS,CACT,KAAK,gBACR,QAAQ,MACN,0EACF,EAGF,QAAQ,KAAK,6BAAsB,EACnC,KAAK,QAAU,GACf,KAAK,cAAc,OAAO,CAC5B,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"]}
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 = false;\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\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 this.installationId = installationId;\n }\n\n public enable() {\n if (!this.installationId) {\n console.error(\n \"🚫 An installation ID must be set before you can enable analytics\"\n );\n }\n\n console.info(\"📈 Analytics enabled\");\n this.enabled = true;\n this.dispatchQueue.resume();\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,EAEH,GAAIC,IAAO,MAET,OAAO,IAAI,SAAS,KAAM,CAAE,OAAQ,GAAI,CAAC,EAG3C,GAAI,CAACA,EACH,eAAQ,MAAM,wCAAwC,EAC/C,IAAI,SAAS,KAAM,CAAE,OAAQ,GAAI,CAAC,EAG3C,GAAI,CACF,IAAMC,EAAW,MAAMH,EAAc,QAAQE,CAAE,EAE/C,OAAO,IAAI,SAASC,GAAU,SAAS,QAAS,CAC9C,QAAS,CACP,eAAgB,kBAClB,CACF,CAAC,CACH,OAASC,EAAO,CAEd,eAAQ,MAAM,6BAA8BA,CAAK,EAC1C,IAAI,SAAS,KAAM,CAAE,OAAQ,GAAI,CAAC,CAC3C,CACF,EAEM,aAAe,MAAOC,GAAiC,CAM7D,GAAI,CACF,IAAMF,EAAW,MAAM,MAAM,yBAAyB,EACjDA,EAAS,IACZ,QAAQ,MAAM,4BAA4B,EAG5C,IAAMG,EAA6B,MAAMH,EAAS,KAAK,EAEjDI,EAA0D,CAC9D,GAAGF,EACH,eAAgB,KAAK,gBAAkB,GACvC,UAAW,IAAI,KACf,YAAa,CACX,YAAaC,GAAe,EAC9B,CACF,EAcA,GAAI,EARW,MAAM,MAAM,GAAG,KAAK,WAAW,aAAc,CAC1D,OAAQ,OACR,QAAS,CACP,eAAgB,kBAClB,EACA,KAAM,KAAK,UAAUC,CAA2B,CAClD,CAAC,GAEW,GAAI,CACd,QAAQ,KACN,oBAAaA,EAA4B,IAAI,6CAC/C,EACA,MACF,CAEA,QAAQ,KACN,oBAAaA,EAA4B,IAAI,+CAC/C,CACF,OAASH,EAAO,CACd,QAAQ,MAAM,gDAAiDA,CAAK,EACpE,MACF,CACF,EAEO,WAAWI,EAAgC,CAChD,QAAQ,KAAK,mBAAYA,EAAQ,IAAI,yBAAyB,EAC9D,KAAK,cAAc,KAAKA,CAAO,CACjC,CAEO,kBAAkBC,EAAwB,CAC/C,KAAK,eAAiBA,CACxB,CAEO,QAAS,CACT,KAAK,gBACR,QAAQ,MACN,0EACF,EAGF,QAAQ,KAAK,6BAAsB,EACnC,KAAK,QAAU,GACf,KAAK,cAAc,OAAO,CAC5B,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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codaco/analytics",
3
- "version": "0.2.0-alpha",
3
+ "version": "0.3.0-alpha",
4
4
  "module": "./dist/index.mjs",
5
5
  "types": "./dist/index.d.mts",
6
6
  "author": "Complex Data Collective <developers@coda.co>",
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;