Skip to content

Commit

Permalink
Merge branch 'main' into fix/promptid-selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
jthrilly authored Jan 11, 2024
2 parents 4258004 + a0e4720 commit e501abd
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 32 deletions.
8 changes: 7 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@ MAXMIND_ACCOUNT_ID=xxxxxxxx

# optional global override for analytics
# can be used to diable analytics in development
#NEXT_PUBLIC_DISABLE_ANALYTICS=true
#DISABLE_ANALYTICS=true

# optional manual specification for installation ID. Useful in scenarios such as
# CI/CD where the installation ID cannot be automatically determined because
# there is no database. Also useful for ensuring consistent ID between DB
# resets.
#INSTALLATION_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

5 changes: 5 additions & 0 deletions .github/workflows/lint.yml → .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ jobs:
build:
runs-on: ubuntu-latest

env:
SKIP_ENV_VALIDATION: true
DISABLE_ANALYTICS: true
INSTALLATION_ID: gh-action

steps:
- name: Checkout code
uses: actions/checkout@v3
Expand Down
File renamed without changes.
27 changes: 11 additions & 16 deletions analytics/utils.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
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 { prisma } from '~/utils/db';

export const getInstallationId = cache(async () => {
const installationId = await api.appSettings.getInstallationId.query();

if (installationId) {
return installationId;
if (env.INSTALLATION_ID) {
return env.INSTALLATION_ID;
}

return 'Unknown';
});
// eslint-disable-next-line local-rules/require-data-mapper
const appSettings = await prisma.appSettings.findFirst();

// eslint-disable-next-line no-process-env
const globalAnalyticsEnabled = process.env.NEXT_PUBLIC_ANALYTICS_ENABLED;
return appSettings?.installationId ?? 'Unknown';
});

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 };
14 changes: 8 additions & 6 deletions env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export const env = createEnv({
*/
server: {
DATABASE_URL: z.string().url(),
MAXMIND_ACCOUNT_ID: z.string(),
MAXMIND_LICENSE_KEY: z.string(),

INSTALLATION_ID: z.string().optional(),
},

/**
Expand All @@ -23,9 +27,7 @@ export const env = createEnv({
NODE_ENV: z
.enum(['development', 'test', 'production'])
.default('development'),
MAXMIND_ACCOUNT_ID: z.string(),
MAXMIND_LICENSE_KEY: z.string(),
NEXT_PUBLIC_DISABLE_ANALYTICS: z.string().optional(),
DISABLE_ANALYTICS: z.boolean().optional(),
},
/**
* You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
Expand All @@ -38,12 +40,12 @@ 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,
INSTALLATION_ID: process.env.INSTALLATION_ID,
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
* useful for Docker builds.
*/
// skipValidation: !!process.env.SKIP_ENV_VALIDATION,
skipValidation: true,
skipValidation: !!process.env.SKIP_ENV_VALIDATION,
});
1 change: 1 addition & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const config = {
reactStrictMode: true,
experimental: {
typedRoutes: true,
webpackBuildWorker: true
},
webpack: (config) => {
config.module.rules.push({
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 e501abd

Please sign in to comment.