Skip to content

Commit

Permalink
update for v2.0.0 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jthrilly committed Jan 10, 2024
1 parent 1fd9ee8 commit 78c6b4d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
26 changes: 9 additions & 17 deletions analytics/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 12 additions & 4 deletions app/api/analytics/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
4 changes: 2 additions & 2 deletions env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 5 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 78c6b4d

Please sign in to comment.