Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Maxmind with IP-API #18

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we probably want to specifically check for the 429 response code so we can log something else if we are throttled.

}
} 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.

Loading