viakite
Dashboard

Guides/Privacy and setup/Proxy

Proxy

Serve it through your own domain

Some ad blockers drop requests to analytics hosts, so a share of your visitors never shows up. You can serve the script and send its events through your own domain instead: everything under /va/ on your site forwards to Viakite, and the requests stay first-party.

The quickest way: open Analytics → Install in the dashboard and turn on Use a proxy under Serve from your domain. Pick your host, and the page gives you the config with your region's address filled in, and the snippet that loads from your domain.

How it works

Your region's API host answers the two proxy paths itself, so your proxy forwards them unchanged:

Your pathForwards toWhat
/va/a.jshttps://us.api.vk.viabio.me/va/a.jsThe script
/va/ehttps://us.api.vk.viabio.me/va/eThe events

Use the address the Install page shows: it is your region's.

Then load the script from your domain and point data-api at the events path:

index.html
1<script defer src="/va/a.js" data-project="pk_live_us_…" data-api="/va/e"></script>

With the npm package or a framework component, pass the same address as api:

app/layout.tsx
1<Analytics project="pk_live_us_…" api="/va/e" />
src/main.ts
1import { init } from "@viakite/sdk";
2
3init({ project: "pk_live_us_…", api: "/va/e" });

The package is bundled with your app, so with npm or a component only /va/e is used.

Pass the visitor on

Viakite counts visitors from a hash of their IP address and user agent (see Privacy). Through a proxy, the request comes from your proxy, so it must pass the visitor's IP address in the X-Forwarded-For header and keep their User-Agent. Viakite reads the first address in X-Forwarded-For. Without it, every visitor looks like your proxy and is counted as one.

Countries and cities come from the edge in front of Viakite, which through a proxy sees your proxy, not the visitor. A proxy that knows the visitor's location can send it, and Viakite uses it first:

HeaderValue
X-Viakite-CountryTwo-letter country code, IN
X-Viakite-RegionRegion name, URL-encoded
X-Viakite-CityCity name, URL-encoded

The Cloudflare Worker below sends them. On the other hosts, locations are where your proxy's requests come from.

Next.js

Rewrites in next.config.ts forward both paths, on Vercel and with next start:

next.config.ts
1import type { NextConfig } from "next";
2
3const nextConfig: NextConfig = {
4 async rewrites() {
5 return [
6 { source: "/va/a.js", destination: "https://us.api.vk.viabio.me/va/a.js" },
7 { source: "/va/e", destination: "https://us.api.vk.viabio.me/va/e" },
8 ];
9 },
10};
11
12export default nextConfig;

Vercel

For any framework on Vercel, in vercel.json at the root of the project:

vercel.json
1{
2 "rewrites": [
3 { "source": "/va/a.js", "destination": "https://us.api.vk.viabio.me/va/a.js" },
4 { "source": "/va/e", "destination": "https://us.api.vk.viabio.me/va/e" }
5 ]
6}

Cloudflare Workers

Create a Worker (Workers & Pages → Create → Worker), replace its code with this and deploy it. Then add a route for your-site.com/va/* on your site's zone (Settings → Domains & Routes).

worker.js
1const VIAKITE = "https://us.api.vk.viabio.me";
2
3export default {
4 async fetch(request) {
5 const url = new URL(request.url);
6 if (url.pathname !== "/va/a.js" && url.pathname !== "/va/e") return fetch(request);
7 const headers = new Headers(request.headers);
8 headers.delete("cookie");
9 headers.set("X-Forwarded-For", request.headers.get("CF-Connecting-IP") || "");
10 const cf = request.cf || {};
11 if (cf.country) headers.set("X-Viakite-Country", cf.country);
12 if (cf.region) headers.set("X-Viakite-Region", encodeURIComponent(cf.region));
13 if (cf.city) headers.set("X-Viakite-City", encodeURIComponent(cf.city));
14 const init = { method: request.method, headers, body: request.body };
15 return fetch(VIAKITE + url.pathname, init);
16 },
17};

Nginx

In your site's server block, then reload Nginx:

nginx.conf
1location /va/ {
2 proxy_pass https://us.api.vk.viabio.me;
3 proxy_set_header Host us.api.vk.viabio.me;
4 proxy_ssl_server_name on;
5 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
6 proxy_set_header Cookie "";
7}

Nginx passes the visitor's User-Agent on by default.

CloudFront

In your distribution:

  1. Origins → Create origin: origin domain us.api.vk.viabio.me, protocol HTTPS only.
  2. Behaviors → Create behavior: path pattern /va/*, the Viakite origin, all HTTP methods allowed, cache policy CachingDisabled, origin request policy AllViewerExceptHostHeader.

CloudFront adds the visitor's address to X-Forwarded-For on its own.

Check it

  1. See the requests

    Open /va/a.js on your site: it shows the script. Then open a page with the browser's network panel: each event is a POST to /va/e that answers 202.

  2. See the events arrive

    Open Analytics → Install in the dashboard. Last events shows your visit within a few seconds, with your browser. Visit from a second device: Realtime should show 2 visitors, not 1.

Next steps