Skip to content

Commit

Permalink
Merge pull request #18 from complexdatacollective/feature/replace-max…
Browse files Browse the repository at this point in the history
…mind

Replace Maxmind with IP-API
  • Loading branch information
jthrilly authored Feb 20, 2024
2 parents bec4603 + fa33259 commit ffa6b54
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 100 deletions.
3 changes: 1 addition & 2 deletions packages/analytics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codaco/analytics",
"version": "5.0.0",
"version": "5.1.0",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand All @@ -12,7 +12,6 @@
"dev": "npm run build -- --watch"
},
"peerDependencies": {
"@maxmind/geoip2-node": "^5.0.0",
"next": "13 || 14"
},
"devDependencies": {
Expand Down
12 changes: 7 additions & 5 deletions packages/analytics/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { type NextRequest } from "next/server";
import { WebServiceClient } from "@maxmind/geoip2-node";
import { ensureError, getBaseUrl } from "./utils";
import z from "zod";

Expand Down Expand Up @@ -77,11 +76,9 @@ export type analyticsEvent = z.infer<typeof AnalyticsEventSchema>;
export const createRouteHandler = ({
platformUrl = "https://analytics.networkcanvas.com",
installationId,
maxMindClient,
}: {
platformUrl?: string;
installationId: string;
maxMindClient: WebServiceClient;
}) => {
return async (request: NextRequest) => {
try {
Expand Down Expand Up @@ -113,8 +110,13 @@ export const createRouteHandler = ({
throw new Error("Could not fetch IP address");
}

const { country } = await maxMindClient.country(ip);
countryISOCode = country?.isoCode ?? "Unknown";
const geoData = await fetch(`http://ip-api.com/json/${ip}`).then((res) => res.json());

if(geoData.status === "success") {
countryISOCode = geoData.countryCode;
} else {
throw new Error(geoData.message);
}
} catch (e) {
console.error("Geolocation failed:", e);
}
Expand Down
114 changes: 21 additions & 93 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 ffa6b54

Please sign in to comment.