diff --git a/analytics/utils.ts b/analytics/utils.ts index 65de462f3..76887bcfc 100644 --- a/analytics/utils.ts +++ b/analytics/utils.ts @@ -1,24 +1,16 @@ +'use server'; + import { makeEventTracker } from '@codaco/analytics'; import { cache } from 'react'; -import { api } from '~/trpc/server'; -import { getBaseUrl } from '~/trpc/shared'; +import { env } from '~/env.mjs'; +import { getAppSettings } from '~/server/routers/appSettings'; export const getInstallationId = cache(async () => { - const installationId = await api.appSettings.getInstallationId.query(); - - if (installationId) { - return installationId; - } + const appSettings = await getAppSettings(); - return 'Unknown'; + return appSettings?.installationId ?? 'Unknown'; }); -// eslint-disable-next-line no-process-env -const globalAnalyticsEnabled = process.env.NEXT_PUBLIC_ANALYTICS_ENABLED; - -export const trackEvent = - globalAnalyticsEnabled !== 'false' - ? makeEventTracker({ - endpoint: getBaseUrl() + '/api/analytics', - }) - : () => {}; +export const trackEvent = !env.DISABLE_ANALYTICS + ? makeEventTracker() + : () => null; diff --git a/app/api/analytics/route.ts b/app/api/analytics/route.ts index a39bc92e8..07e45d283 100644 --- a/app/api/analytics/route.ts +++ b/app/api/analytics/route.ts @@ -3,12 +3,20 @@ import { env } from '~/env.mjs'; import { createRouteHandler } from '@codaco/analytics'; import { WebServiceClient } from '@maxmind/geoip2-node'; +const maxMindClient = new WebServiceClient( + env.MAXMIND_ACCOUNT_ID, + env.MAXMIND_LICENSE_KEY, + { + host: 'geolite.info', + }, +); + +const installationId = await getInstallationId(); + const routeHandler = createRouteHandler({ - maxMindAccountId: env.MAXMIND_ACCOUNT_ID, - maxMindLicenseKey: env.MAXMIND_LICENSE_KEY, - getInstallationId, + installationId, platformUrl: 'https://frescoanalytics.networkcanvas.dev', - WebServiceClient, + maxMindClient, }); export { routeHandler as POST }; diff --git a/env.mjs b/env.mjs index 6a271a4f0..f7bb8dfbf 100644 --- a/env.mjs +++ b/env.mjs @@ -25,7 +25,7 @@ export const env = createEnv({ .default('development'), MAXMIND_ACCOUNT_ID: z.string(), MAXMIND_LICENSE_KEY: z.string(), - NEXT_PUBLIC_DISABLE_ANALYTICS: z.string().optional(), + DISABLE_ANALYTICS: z.string().optional(), }, /** * You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g. @@ -38,7 +38,7 @@ export const env = createEnv({ VERCEL_URL: process.env.VERCEL_URL, MAXMIND_ACCOUNT_ID: process.env.MAXMIND_ACCOUNT_ID, MAXMIND_LICENSE_KEY: process.env.MAXMIND_LICENSE_KEY, - NEXT_PUBLIC_DISABLE_ANALYTICS: process.env.NEXT_PUBLIC_DISABLE_ANALYTICS, + DISABLE_ANALYTICS: process.env.DISABLE_ANALYTICS, }, /** * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially diff --git a/package.json b/package.json index fc388bc42..33e17db99 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "seed": "tsx prisma/seed.ts" }, "dependencies": { - "@codaco/analytics": "1.0.1-alpha-1", + "@codaco/analytics": "^2.0.0", "@codaco/protocol-validation": "3.0.0-alpha.4", "@codaco/shared-consts": "^0.0.2", "@headlessui/react": "^1.7.17", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6a88a0d5e..bef9032a3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6,8 +6,8 @@ settings: dependencies: '@codaco/analytics': - specifier: 1.0.1-alpha-1 - version: 1.0.1-alpha-1(next@14.0.0) + specifier: ^2.0.0 + version: 2.0.0(@maxmind/geoip2-node@5.0.0)(next@14.0.0) '@codaco/protocol-validation': specifier: 3.0.0-alpha.4 version: 3.0.0-alpha.4(@types/eslint@8.44.6)(eslint-config-prettier@9.0.0)(eslint@8.52.0) @@ -1788,9 +1788,10 @@ packages: resolution: {integrity: sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==} dev: true - /@codaco/analytics@1.0.1-alpha-1(next@14.0.0): - resolution: {integrity: sha512-opMtf/+44TjRAPNZtWuS/nj7Nza2gHsA4PE5DM6bZ2USGpu2+vx6euJFUin4uwlZGXqitgLPZfmHGxPchzbnHQ==} + /@codaco/analytics@2.0.0(@maxmind/geoip2-node@5.0.0)(next@14.0.0): + resolution: {integrity: sha512-HR9tlHVu+g2/Lwg11N9GmGXRNkai04uvRplLQpSJ92Ypszx2sc4xs19qEK10Sy5wJSotyCCNkTtvag1HQ6sFEQ==} peerDependencies: + '@maxmind/geoip2-node': ^5.0.0 next: 13 || 14 dependencies: '@maxmind/geoip2-node': 5.0.0